【问题标题】:How do I tell bundle install to use the version of Ruby on my PATH?如何告诉 bundle install 在我的 PATH 上使用 Ruby 版本?
【发布时间】:2018-02-18 07:16:34
【问题描述】:

我正在尝试在 Debian 上安装我的 Rails 5 项目。使用或不使用sudo 运行bundle install 都会导致错误,抱怨没有适当的Ruby 版本,即使在我运行ruby -v 之后,您可以看到版本是2.4。如何将bundle install 指向正确的版本?

$ sudo bundle install
[sudo] password for myuser:
Don't run Bundler as root. Bundler can ask for sudo if it is needed, and installing your bundle as root will break this application for all non-root users on this machine.
Your Gemfile lists the gem jquery-rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.6
Using minitest 5.10.3
Using thread_safe 0.3.6
Using tzinfo 1.2.3

Gem::InstallError: activesupport requires Ruby version >= 2.2.2.
An error occurred while installing activesupport (5.0.4), and Bundler cannot continue.
Make sure that `gem install activesupport -v '5.0.4'` succeeds before bundling.

$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [armv6l-linux-eabihf]

在没有sudo 的情况下运行:

$ bundle install
Your Gemfile lists the gem jquery-rails (>= 0) more than once.
You should probably keep only one of them.
While it's not a problem now, it could cause errors if you change the version of just one of them later.
Fetching gem metadata from https://rubygems.org/........
Fetching additional metadata from https://rubygems.org/..
Using rake 12.0.0
Using concurrent-ruby 1.0.5
Using i18n 0.8.6
Using minitest 5.10.3
Using thread_safe 0.3.6
Using tzinfo 1.2.3

Gem::InstallError: activesupport requires Ruby version >= 2.2.2.
An error occurred while installing activesupport (5.0.4), and Bundler cannot continue.
Make sure that `gem install activesupport -v '5.0.4'` succeeds before bundling.

$ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [armv6l-linux-eabihf]

【问题讨论】:

    标签: ruby-on-rails ruby path ruby-on-rails-5 bundler


    【解决方案1】:

    当您调用 bundle 时,首先bundle 本身会在$PATH 中解析。您可以通过键入whereis bundlewhich bundle 来检查其可执行文件的位置。就我而言(Ubuntu 16.04),它位于/usr/local/bin/bundle

    如果我们执行cat /usr/local/bin/bundle,我们会得到这个可执行文件的内容:

    $ cat /usr/local/bin/bundle
    #!/usr/bin/ruby2.4
    #
    # This file was generated by RubyGems.
    #
    # The application 'bundler' is installed as part of a gem, and
    # this file is here to facilitate running it.
    #
    
    require 'rubygems'
    
    version = ">= 0.a"
    
    if ARGV.first
      str = ARGV.first
      str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
      if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
        version = $1
        ARGV.shift
      end
    end
    
    load Gem.activate_bin_path('bundler', 'bundle', version)
    

    如您所见,它是一个普通的 Ruby 脚本,最上面的一行 (#!/usr/bin/ruby2.4) 设置解释器来执行它。

    我想,在您的情况下,使用的是旧 Ruby 版本,因为如果您在安装 2.4.0 之前在系统中有 Ruby,则 gem 的可执行文件没有更新为 2.4,并且还使用旧 Ruby版本。你可以通过which gem(对我来说/usr/bin/gem)和cat检查文件内容来检查这一点。

    之后,您可以通过键入whereis gem 来检查gem 的可用可执行文件:

    $ whereis gem
    gem: /usr/bin/gem /usr/bin/gem2.2 /usr/bin/gem2.4
    

    然后您可以通过键入 gem uninstall bundler 删除捆绑程序(这也应该删除它的可执行文件)并使用正确的 gem 再次安装它,执行:

    /usr/bin/gem2.4 install bundle
    

    这应该可以解决问题,因为在 bundler 的可执行文件中,您将获得 Ruby 2.4 作为解释器。

    update-alternatives 命令也可以用于这种情况。

    如您所见,这太让人头疼了,所以我的建议是要么使用 Ruby 版本管理器(rvmrbenv 等),要么每台机器只有一个 Ruby 版本。

    【讨论】:

    • K,所以我运行“whereis gem”并得到“gem: /usr/bin/gem /usr/bin/gem2.1 /usr/local/bin/gem /usr/share/man /man1/gem.1.gz”。看起来不像宝石的最新版本。我怎么得到它?
    • @Dave 您是如何将 ruby​​2.4 安装到您的系统中的?它是从源代码编译的吗?
    • 对于 Ubuntu,我使用 this repositoryrubygems-integration (v 1.10) 软件包也被安装并设置为在安装 Ruby 时提供 /usr/lib/gemX.X。
    • 回答你的第一个问题,是的,我是从源代码安装的。
    • @Dave 好的,我现在尝试从源代码安装 Ruby,我看到它在 /usr/local/bin/gem 中生成了 gem 可执行文件。 cat 它并检查解释器。就我而言,它是#!/usr/local/bin/ruby。然后执行/your/path/ruby -v,如果是需要的Ruby,使用这个gem可执行文件。
    【解决方案2】:
    gem install rails --version 5.0.0
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 2014-08-07
      • 2017-06-04
      • 1970-01-01
      • 2016-04-18
      • 1970-01-01
      相关资源
      最近更新 更多