【问题标题】:How to install gem from GitHub source?如何从 GitHub 源安装 gem?
【发布时间】:2011-02-04 08:41:12
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: rubygems


    【解决方案1】:

    嗯,这取决于所讨论的项目。一些项目的根目录中有一个 *.gemspec 文件。在这种情况下,它会是

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

    其他项目有一个 rake 任务,称为“gem”或“build”或类似的东西,在这种情况下您必须调用“rake”,但这取决于项目。

    在这两种情况下,您都必须下载源代码。

    【讨论】:

    • 只是一个提示,让人们知道它到底发生了什么。调用gem build时会创建gemname-version.gem文件
    • gem install gemname-version.gem 命令在哪里安装 git gem 本地?我在本地机器上的任何地方都找不到以这种方式安装的引擎 gem。 bundler 把它藏在哪里了?
    • 我认为gem install gemname-version.gem 行应该是gem install --local gemname-version.gem
    • @Green - gem which gemname 应该告诉你一个特定的宝石在哪里,这对你不起作用吗?
    • 嗨,我只有 Rakefile,我不知道如何安装它。有什么帮助吗?
    【解决方案2】:

    如果您使用的是 bundler,您需要在 Gemfile 中添加类似的内容:

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

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

    UPD. 如 cmets 中所示,要使 Bundler 正常运行,您还需要将以下内容添加到 config.ru

    require "bundler" 
    Bundler.setup(:default)
    

    【讨论】:

    • 我还需要添加以下内容(添加到我的 config.ru):require "bundler" Bundler.setup(:default) 有关详细信息,请参阅 bundler docs
    • 另外可以指定参考、分支或标签选项,例如gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git', :branch => 'yourbranch'
    • 还有:gem 'redcarpet', github: 'tanoku/redcarpet'akash.im/2012/06/05/bundler-new-github-option.html
    • @AmitPatel 非常感谢! :branch => 'yourbranch',你的这条线刚刚为我解决了一个大问题。非常感谢你。
    • @gaussblurinc gem 'redcarpet', :git => 'git://github.com/tanoku/redcarpet.git', :tag => 'v2.3.5' :tag => '' 部分
    【解决方案3】:

    试试specific_install gem,它允许您从其 github 存储库(如“edge”)或任意 URL 安装 gem。对于在多台机器上分叉宝石和对它们进行黑客攻击非常有用。

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

    例如

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

    【讨论】:

    • 您能否在specific_install gem 上添加更多解释?
    • 这正是我想要的,类似于 Python 的 pip git 支持。 gem specific_install -l 就像一个魅力!
    • ERROR: While executing gem ... (NoMethodError) undefined method 'build' for Gem::Package:Module 听起来很酷,但我不会进一步研究它。只是想发布它对我不起作用,以防其他人将根据 SO 推荐试一试。
    • @isomorphismes +1 对您的评论。我在这里创建了一个关于该错误的单独问题:stackoverflow.com/questions/27045874/…
    • 这是救命稻草!谢谢先生。
    【解决方案4】:

    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

    【讨论】:

    • 可能是最好的答案
    • 请注意,如果您将这种方法与乘客和 apache / ngix 一起使用,您可能会遇到麻烦。当运行bundle 时,这样的 git-gem- 依赖项不会被全局安装,而是在当前用户的主目录中。乘客将以您的网络服务器用户(例如www-data)运行 ruby​​,该用户无权访问此目录,因此不会加载此“git-gem”。你会得到一个错误... is not yet checked out. Run bundle install first
    【解决方案5】:

    已过时(参见 cmets)

    如果项目来自 github,并且包含在 http://gems.github.com/list.html 的列表中,那么您只需将 github 存储库添加到 gems 源即可安装它:

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

    【讨论】:

    • 还是?我刚刚做了这个并且它确实有效......在将它添加到你的来源之前去 gems.github.com 我猜? (但不要使用 sudo)
    • @esharp,他们托管了他们构建的那些,但他们不再构建它们。如果 gem 得到更新 since 2009,gems.github.com 副本将被废弃。
    【解决方案6】:

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

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

    【讨论】:

      【解决方案7】:

      你也可以gem install username-projectname -s http://gems.github.com

      【讨论】:

      • 已过时,请参阅其他答案的 cmets。
      • 仍然帮助我解决旧代码库的问题。是的,它是 2013 年,我正在开发一个 rails 2.3.4 项目。
      【解决方案8】:

      在您的 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
      

      【讨论】:

        【解决方案9】:

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

        【讨论】:

          【解决方案10】:

          你也可以使用rdp/specific_installgem:

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

          【讨论】:

            【解决方案11】:

            在新的 Linux 机器上,您还需要安装 git。 Bundle 在幕后使用它。

            【讨论】:

              猜你喜欢
              • 2016-07-15
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-02-18
              • 1970-01-01
              • 2011-04-20
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多