【问题标题】:Bundle install is using a different Ruby version?捆绑安装使用不同的 Ruby 版本?
【发布时间】:2013-12-13 09:50:27
【问题描述】:

我尝试使用刀独奏在 Vagrant 上安装 Ruby 2.0.0-p353。 当我以 root 和 vagrant 身份登录时,ruby -v 返回 Ruby 2.0.0-p353。

但是,当我在 Rails 项目中运行bundle install 时,会显示以下语句:

Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0

Ruby 的默认版本是 1.8.7,所以我认为bundle install 指的是这个。 我应该怎么做才能解决这个问题?

$ cat site-cookbooks/ruby/recipes/default.rb

group 'rbenv' do
  action :create
  members 'vagrant'
  append true
end

git '/usr/local/rbenv' do
  repository 'git://github.com/sstephenson/rbenv.git'
  reference 'master'
  action :checkout
  user "#{node.user}"
  group 'rbenv'
end

directory '/usr/local/rbenv/plugins' do
  owner "#{node.user}"
  group 'rbenv'
  mode 0755
  action :create
end

template '/etc/profile.d/rbenv.sh' do
  owner "#{node.user}"
  group "#{node.user}"
  mode 0644
end

git '/usr/local/rbenv/plugins/ruby-build' do
  repository 'git://github.com/sstephenson/ruby-build.git'
  reference 'master'
  action :checkout
  user "#{node.user}"
  group 'rbenv'
end

execute 'ruby install' do
  not_if "source /etc/profile.d/rbenv.sh; rbenv versions | grep #{node.ruby.version}"
  command "source /etc/profile.d/rbenv.sh; rbenv install #{node.ruby.version}"
  action :run
end

execute 'ruby change' do
  command "source /etc/profile.d/rbenv.sh; rbenv global #{node.ruby.version}; rbenv rehash"
  action :run
end

$ cat site-cookbooks/ruby/attributes/default.rb

default['user'] = 'root'
default['ruby']['version'] = '2.0.0-p353'

$ cat site-cookbooks/ruby/templates/default/rbenv.sh.rb

export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"

【问题讨论】:

  • 你用过rbenv,请从这里试试我的回答:stackoverflow.com/questions/20531782/…
  • 感谢您的回答。我试着按照那个页面做,但我做不到。例如,bundle install 返回Gem::InstallError: activesupport requires Ruby version >= 1.9.3.。
  • 但是你有 ruby​​ 版本 >= 1.9.3,你有 2.0.0 吗?还是 1.8.7?
  • ruby -v 返回ruby 2.0.0p353 (2013-11-22 revision 43784) [x86_64-linux],所以我认为它应该可以毫无问题地运行。
  • 和bundle exec ruby -v返回什么?

标签: ruby chef-infra vagrant rbenv


【解决方案1】:

请按照以下步骤解决问题:

  1. 确保以下命令返回正确版本的 ruby​​:

    $ rbenv versions
      system
      2.0.0-p353
    
    $ rbenv local
    ruby-2.0.0
    
    $ rbenv version
      2.0.0-p353
    
  2. 确保获取的 ruby​​ 版本正确:

    $ bundle exec ruby -v
    ruby 2.0.0-p353 (2013-11-22 revision 43784) [x86_64-linux]
    
  3. 如果您的 ruby​​ 版本无效,您可以通过调用 shell 来验证问题是否在 bundler 中:

    $ bundle exec ruby -v
    ruby 1.8.7
    
    $ which bundle
    /usr/bin/bundle
    

    它说它将调用系统 ruby​​ 来执行 ruby​​ 脚本。

  4. 重新安装 bundler,然后确保当前的 ruby​​ 有效:

    $ gem install bundler
    
    $ bundle exec ruby -v
    ruby 2.0.0-p353 (2013-11-22 revision 43784) [x86_64-linux]
    

另请参考如何正确搭建ruby项目,正在rbenv/rvm下开发here:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多