Bundler using wrong Ruby version

本秂侑毒 提交于 2021-02-19 16:04:54

问题


When I try to start my Rails server, I get the following error:

$ bundle exec rails s
Your Ruby version is 2.3.7, but your Gemfile specified ~> 2.3.8

I don't understand why that happens though, since I set Ruby to 2.3.8 both using Homebrew and using rbenv. 2.3.7 is the version of the system's ruby. I am using Mac OS 10.14.4.

$ ruby -v
ruby 2.3.8p459 (2018-10-18 revision 65136) [x86_64-darwin18]

$ rbenv version
2.3.8 (set by /Users/ceasar/foo/.ruby-version)

$ which -a ruby
/Users/ceasar/.rbenv/shims/ruby
/usr/local/opt/ruby@2.3/bin/ruby
/usr/local/bin/ruby
/usr/bin/ruby

$ which -a bundle
/Users/ceasar/.rbenv/shims/bundle
/usr/local/bin/bundle

$ /usr/bin/xcodebuild -version
Xcode 10.2
Build version 10E125

$ brew list ruby@2.3
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/erb
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/gem
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/irb
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/rake
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/rdoc
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/ri
/usr/local/Cellar/ruby@2.3/2.3.8_1/bin/ruby
/usr/local/Cellar/ruby@2.3/2.3.8_1/include/ruby-2.3.0/ (25 files)
/usr/local/Cellar/ruby@2.3/2.3.8_1/lib/libruby.2.3.0.dylib
/usr/local/Cellar/ruby@2.3/2.3.8_1/lib/pkgconfig/ruby-2.3.pc
/usr/local/Cellar/ruby@2.3/2.3.8_1/lib/ruby/ (1211 files)
/usr/local/Cellar/ruby@2.3/2.3.8_1/lib/ (3 other files)
/usr/local/Cellar/ruby@2.3/2.3.8_1/share/emacs/ (7 files)
/usr/local/Cellar/ruby@2.3/2.3.8_1/share/man/ (4 files)
/usr/local/Cellar/ruby@2.3/2.3.8_1/share/ri/ (13487 files

$ bundle env | head -n 40
## Environment

```
Bundler       2.0.1
  Platforms   ruby, x86_64-darwin-18
Ruby          2.3.8p459 (2018-10-18 revision 65136) [x86_64-darwin18]
  Full Path   /Users/ceasar/.rbenv/versions/2.3.8/bin/ruby
  Config Dir  /Users/ceasar/.rbenv/versions/2.3.8/etc
RubyGems      2.5.2.3
  Gem Home    /Users/ceasar/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0
  Gem Path    /Users/ceasar/.gem/ruby/2.3.0:/Users/ceasar/.rbenv/versions/2.3.8/lib/ruby/gems/2.3.0
  User Path   /Users/ceasar/.gem/ruby/2.3.0
  Bin Dir     /Users/ceasar/.rbenv/versions/2.3.8/bin
Tools
  Git         2.19.2
  RVM         not installed
  rbenv       rbenv 1.1.2
  chruby      not installed
```

## Bundler Build Metadata

```
Built At          2019-01-04
Git SHA           d7ad2192f
Released Version  true
```

## Bundler settings

```
build.libv8
  Set for the current user (/Users/ceasar/.bundle/config): "--with-system-v8"
build.mysql2
  Set for the current user (/Users/ceasar/.bundle/config): "--with-mysql-config=/usr/local/Cellar/mysql@5.7/5.7.24/bin/mysql_config"
path
  Set for your local app (/Users/ceasar/Desktop/beacon/.bundle/config): "vendor/bundle"
disable_shared_gems
  Set for your local app (/Users/ceasar/Desktop/beacon/.bundle/config): true
```

I don't see anything here which would make me think it shouuld be using 2.3.7.

I'm not sure what else to try.

How does Bundler decide which version of Ruby to use, and how can I configure it to use either rbenv or my homebrew Ruby installation?


回答1:


How about to try to use 'rvm'?

rvm install 2.4.2

This command will install another version of ruby, 2.4.2

And you can simply check the versions which all you have as

rvm list.

After this, run the version which you wish as the below,

rvm <version> or rvm use <version>

Hope this will help you!




回答2:


I don't suggest using RVM instead of RBEnv. RBEnv is one of the most up-to-date solutions around, and most people stick with it.

Can you please try running gem update --system to update your bundler first.




回答3:


Install Rbenv and run the following commands in your project directory to install 2.3.8:

rbenv install 2.3.8
rbenv local 2.3.8
bundle install --path=vendor/cache



回答4:


Summary: The wrong version of bundler causes the problem. Use a ruby version manager to install the needed ruby version. Then install the correct version of the bundler gem for your project.

Details: In my case I'm running a rails example on github that required an older version of ruby and bundler and got the error described.

To manage my different ruby versions, I'm using ruby-install and chruby both installed with homebrew.

I followed these steps to resolve the problem on macOS Catalina v10.15.7:

Resolution steps

% ruby-install 2.4.1
# open a new shell so chruby will find  2.4.1
% chruby 2.4.1

% which ruby
/Users/richardlogwood/.rubies/ruby-2.4.1/bin/ruby

% gem install bundler:1.16.1
% which bundle
/Users/richardlogwood/.gem/ruby/2.4.1/bin/bundle

% cd (to the rails project directory)

# Note: it's a rails project so there was a binstub 
# for bundler, hence `bin/bundler` in the project directory*

% bin/bundler install
% rake db:{create,migrate}
% bin/rails s

Before running the above steps, the error messages told me which version of ruby and bundler I needed to install. The system bundler was the wrong version (I'm on a mac) and the initial error message using that version gave me the error below. Only after installing the needed bundler gem (see above) was the correct ruby version resolved (see above).

Errors before resolution

% bundle install
Your Ruby version is 2.6.3, but your Gemfile specified 2.4.1
% which ruby
/Users/richardlogwood/.rubies/ruby-2.4.1/bin/ruby
% which bundle
/usr/bin/bundle

Note, the required bundler version was indicated by this error message:

Could not find 'bundler' (1.16.1) required by your ... /Gemfile.lock. (Gem::GemNotFoundException)
To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:1.16.1`


来源:https://stackoverflow.com/questions/55427016/bundler-using-wrong-ruby-version

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