【问题标题】:Bundler using wrong Ruby versionBundler 使用了错误的 Ruby 版本
【发布时间】:2019-03-30 00:28:37
【问题描述】:

当我尝试启动我的 Rails 服务器时,我收到以下错误:

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

我不明白为什么会这样,因为我使用 Homebrew 和 rbenv 都将 Ruby 设置为 2.3.8。 2.3.7是系统的ruby版本。我使用的是 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
```

我在这里看不到任何让我认为它应该使用 2.3.7 的东西。

我不确定还可以尝试什么。

Bundler 如何决定使用哪个版本的 Ruby,以及如何将其配置为使用 rbenv 或我的自制 Ruby 安装?

【问题讨论】:

  • 可能是你的shell缓存了旧的bundle命令,尝试运行hash -r
  • 不幸的是,我设法解决了我的问题,但没有确定真正解决了什么问题。如果有人开始获得多数赞成票,我会留下这个问题并接受答案。
  • 我尝试了@matt 建议的方法,它有效! geeksforgeeks.org/hash-command-in-linux-with-examples
  • @matt 请添加它作为答案 - 它对我有用,我想投票。

标签: ruby-on-rails ruby bundler rbenv


【解决方案1】:

试试用'rvm'怎么样?

rvm install 2.4.2

此命令将安装另一个版本的 ruby​​,2.4.2

您可以简单地检查您拥有的所有版本

rvm list.

在此之后,运行您想要的版本,如下所示,

rvm <version>rvm use <version>

希望对您有所帮助!

【讨论】:

    【解决方案2】:

    总结: 错误版本的 bundler 会导致问题。使用 ruby​​ 版本管理器安装所需的 ruby​​ 版本。然后为您的项目安装正确版本的 bundler gem。

    详细信息:在我的例子中,我在 github 上运行一个 Rails 示例,它需要旧版本的 ruby​​ 和 bundler,并得到了描述的错误。

    为了管理我的不同 ruby​​ 版本,我使用 ruby-installchruby 都安装了 homebrew

    我按照以下步骤解决了 macOS Catalina v10.15.7 上的问题:

    解决步骤

    % 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
    

    在运行上述步骤之前,错误消息告诉我需要安装哪个版本的 ruby​​ 和 bundler。系统捆绑程序是错误的版本(我在 Mac 上),使用该版本的初始错误消息给了我以下错误。只有在安装了所需的 bundler gem(见上文)后,才能解析正确的 ruby​​ 版本(见上文)。

    解决前的错误

    % 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
    

    请注意,此错误消息指示了所需的捆绑程序版本:

    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`
    

    【讨论】:

      【解决方案3】:

      我不建议使用 RVM 代替 RBEnv。 RBEnv 是最新的解决方案之一,大多数人都坚持使用它。

      您能否先尝试运行gem update --system 来更新您的捆绑器。

      【讨论】:

        【解决方案4】:

        安装Rbenv并在你的项目目录下运行以下命令安装2.3.8:

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

        【讨论】:

          猜你喜欢
          • 2021-08-17
          • 1970-01-01
          • 2022-08-14
          • 1970-01-01
          • 2014-03-20
          • 1970-01-01
          • 2013-01-13
          • 2013-10-10
          • 1970-01-01
          相关资源
          最近更新 更多