Bundle install is using a different Ruby version?

半世苍凉 提交于 2019-12-30 20:11:35

问题


I tried to install Ruby 2.0.0-p353 on Vagrant using knife-solo. When I logged in as root and vagrant, ruby -v returns Ruby 2.0.0-p353.

However, when I run bundle install in a Rails project, the following statement is displayed:

Your Ruby version is 1.8.7, but your Gemfile specified 2.0.0

The default version of Ruby is 1.8.7, so I think bundle install is referring to this. What should I do to solve this problem?

$ 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:


Please do the following steps to fix the problem:

  1. Make sure that the following commands returns the proper version of ruby:

    $ rbenv versions
      system
      2.0.0-p353
    
    $ rbenv local
    ruby-2.0.0
    
    $ rbenv version
      2.0.0-p353
    
  2. Make sure that getting the version of ruby is correct:

    $ bundle exec ruby -v
    ruby 2.0.0-p353 (2013-11-22 revision 43784) [x86_64-linux]
    
  3. If you got invalid version of ruby, you shell to verify that problem is in bundler by calling:

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

    It says that it will call system ruby to proceed ruby scripts.

  4. Reinstall bundler, and then make sure that now current ruby is valid:

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

Also refer to how to properly set up a ruby project, which is being developed under rbenv/rvm here:



来源:https://stackoverflow.com/questions/20563439/bundle-install-is-using-a-different-ruby-version

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!