簡體   English   中英

如何告訴bundle install在PATH上使用Ruby版本?

[英]How do I tell bundle install to use the version of Ruby on my PATH?

我正在嘗試在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]

調用捆綁軟件時,首先, 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

這應該可以解決問題,因為在Bundle的可執行文件中,您將獲得Ruby 2.4作為解釋器。

update-alternatives命令在這種情況下也很有用。

如您所見,這令人頭疼,所以我的建議是使用Ruby版本管理器( rvmrbenv等),或者每台計算機僅使用一個Ruby版本。

gem install rails --version 5.0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM