如何从GitHub源码安装gem?

472

我想从最新的GitHub源代码中安装gem。

我该如何操作?

11个回答

414

这取决于具体的项目。一些项目在其根目录中有一个 *.gemspec 文件。在这种情况下,命令应为:

gem build GEMNAME.gemspec
gem install gemname-version.gem

其他项目有一个名为gembuild或类似名称的rake任务。在这种情况下,您必须调用rake <taskname>,但这取决于项目。

无论哪种情况,您都必须下载源代码。


21
只是一个提示,让人们知道实际发生的情况。当调用 gem build 时,会创建 gemname-version.gem 文件。 - Waiting for Dev...
gem install gemname-version.gem命令会将Git Gem安装到本地的哪里?我在我的本地机器上找不到以这种方式安装的引擎Gem的任何位置。bundler把它藏在哪里了? - Green
3
我认为“gem install gemname-version.gem”这行应该改成“gem install --local gemname-version.gem”,以便本地安装gem包。 - JonB
@Green - gem which gemname 应该能够告诉你特定 gem 的位置,这个方法对你不起作用吗? - sameers
嗨,我只有Rakefile文件,不知道如何安装它。可以帮忙吗? - eldi

345

如果您使用bundler,则需要将以下内容添加到您的Gemfile中:

gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git'

如果存在 .gemspec 文件,在运行 bundle install 时应该能够获取并安装 gem。

更新。 如评论所示,为了使 Bundler 正常工作,您还需要将以下内容添加到 config.ru 中:

require "bundler" 
Bundler.setup(:default)

9
我还需要添加以下内容(添加到我的config.ru文件中):require "bundler" Bundler.setup(:default)有关更多详细信息,请参阅bundler文档 - Louis Sayers
44
此外,你还可以指定ref、branch或tag选项,例如 gem 'redcarpet',:git => 'git://github.com/tanoku/redcarpet.git',:branch => 'yourbranch' - Amit Patel
26
还有一行代码:gem 'redcarpet', github: 'tanoku/redcarpet'。该代码的来源是http://akash.im/2012/06/05/bundler-new-github-option.html。 - Andrew
@AmitPatel 非常非常感谢你!!!:branch => 'yourbranch',你的这行代码刚刚解决了我一个大问题。非常感激你。 - K M Rakibul Islam
@gryzzly,如何指定git的标签? - gaussblurinc
4
gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git', :tag => 'v2.3.5'中的:tag => ''部分表示所使用的Git存储库中的标签版本号是v2.3.5。 - Misha Reyzlin

284

可以尝试使用specific_install gem,它可以让你从 Github 存储库(如 'edge')或任意 URL 安装 gem。非常适用于在多台机器上分叉 gem 并对其进行修改等操作。

gem install specific_install
gem specific_install -l <url to a github gem>

例如。

gem specific_install https://github.com/githubsvnclone/rdoc.git 

3
你能否对 specific_install gem 提供更详细的解释? - microspino
2
这正是我一直在寻找的,类似于Python的pip git支持。gem specific_install -l <github上项目的url>非常好用! - Murph
1
错误:执行 gem 时出错...(NoMethodError)Gem::Package:Module 没有定义 'build' 方法。听起来很酷,但我不会进一步研究它。只是想发帖说明它对我无效,以防有人根据 SO 的建议尝试。 - isomorphismes
1
@isomorphismes 对你的评论点赞。我在这里创建了一个关于那个错误的单独问题:https://dev59.com/KoXca4cB1Zd3GeqPHV4E - La-comadreja
有没有一种方法可以指定要使用的分支? - 编辑回复自己,从文档中:“$ gem specific_install http://github.com/githubsvnclone/rdoc.git edge”或“$ gem specific_install -l http://github.com/githubsvnclone/rdoc.git -b edge”。 - KumZ

43

Bundler 允许你直接从 Git 仓库使用 Gem 包。 在你的 Gemfile 文件中:

# Use the http(s), ssh, or git protocol
gem 'foo', git: 'https://github.com/dideler/foo.git'
gem 'foo', git: 'git@github.com:dideler/foo.git'
gem 'foo', git: 'git://github.com/dideler/foo.git'

# Specify a tag, ref, or branch to use
gem 'foo', git: 'git@github.com:dideler/foo.git', tag: 'v2.1.0'
gem 'foo', git: 'git@github.com:dideler/foo.git', ref: '4aded'
gem 'foo', git: 'git@github.com:dideler/foo.git', branch: 'development'

# Shorthand for public repos on GitHub (supports all the :git options)
gem 'foo', github: 'dideler/foo'

更多信息请参见https://bundler.io/v2.0/guides/git.html


1
可能是最好的答案 - Anton Semenichenko
请注意,如果您在使用Passenger和Apache / Nginx时采用此方法可能会遇到麻烦。运行bundle命令时,这些git-gem依赖项将不会全局安装,而是安装在当前用户的主目录中。Passenger将以您的Web服务器用户(例如www-data)身份运行Ruby,该用户无法访问此目录,因此此“git-gem”将无法加载。您将收到错误消息...尚未签出,请先运行bundle install - fishbone

16

已过时(请参见评论)

如果项目来自Github,并且包含在http://gems.github.com/list.html上的列表中,则可以将Github存储库添加到gem源中以安装它:

$ gem sources -a http://gems.github.com
$ sudo gem install username-projectname

1
真的吗?我刚刚做了这个,它确实起作用了...在将其添加到您的源之前,请先访问gems.github.com吧?(但不要使用sudo) - esharp
7
@esharp,他们托管了自己建立的gem,但他们不再建立新的了。如果这个gem自2009年以来有更新,那么gems.github.com上的副本将会过时。 - Tobu

12

如果您从公共 GitHub 存储库获取宝石,您可以使用简写

gem 'nokogiri', github: 'tenderlove/nokogiri'

5

你还可以使用 rdp/specific_install gem:

gem install specific_install
gem specific_install https://github.com/capistrano/drupal-deploy.git

4
您可以使用以下命令进行安装:gem install username-projectname -s http://gems.github.com

6
过时,请参考其他答案的评论。 - Lee Hambley
1
仍然帮助我解决了旧代码库的一个问题。是的,现在已经是2013年了,我正在处理一个Rails 2.3.4项目。 - Vivek Sampara

3
在您的Gemfile中添加以下内容:
gem 'example', :git => 'git://github.com/example.git'

你还可以添加ref、branch和tag选项,例如,如果你想从特定分支下载:
gem 'example', :git => "git://github.com/example.git", :branch => "my-branch"

然后运行:

bundle install

3
如果您按照gryzzly的建议使用bundler进行安装,并且宝石创建了一个二进制文件,则请确保使用bundle exec mygembinary运行它,因为该宝石存储在bundler目录中,该目录在普通宝石路径上不可见。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接