Bundle install是使用不同的Ruby版本吗?

时间:2013-12-13 09:50:27

标签: ruby chef vagrant rbenv

我尝试使用knife-solo在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 -)"

1 个答案:

答案 0 :(得分:3)

请执行以下步骤来解决问题:

  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]
    
  5. 另请参阅如何正确设置正在rbenv / rvm here下开发的ruby项目: