修复rbenv和rails 5.2的“您的Ruby版本为2.4.4,但您的Gemfile指定了2.5.1”的错误

时间:2018-07-31 21:24:26

标签: ruby-on-rails ruby bundler rbenv ruby-on-rails-5.2

我知道各种红宝石版本对此都有很多问题。我相信我已经用尽了所有正常的修复方法(通常可以使用)。

这是一个已经在与我现在已经有几个月(在其他人之前已经有几年)的环境中工作的应用程序。以前它可以同时使用ruby 2.4.4和ruby 2.5.1(使用rbenv切换)。

我想要ruby 2.5.1,并且捆绑程序抱怨我的ruby版本是2.4.4

$ bundle exec rspec
Your Ruby version is 2.4.4, but your Gemfile specified 2.5.1

$ rbenv exec bundle exec rspec
Your Ruby version is 2.4.4, but your Gemfile specified 2.5.1

注意:示例为rspec,但bundle exec

之前的任何命令均会发生这种情况

我检查了我所知道的所有地方,以及互联网建议我应该看的所有地方,但是找不到将ruby设置为2.4.4的内容(请参见下面的详细信息)。

所以真正的问题是-在哪里寻找rbenvbundler或两者都配置错误的文件?

所有这些都为我提供了正确的红宝石版本

基本知识:

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]

$ which ruby
/home/vagrant/.rbenv/shims/ruby

$ rbenv which ruby
/home/vagrant/.rbenv/versions/2.5.1/bin/ruby

$ bundle -v
Bundler version 1.16.3

$ which bundle
/home/vagrant/.rbenv/shims/bundle

$ rbenv which bundle
/home/vagrant/.rbenv/versions/2.5.1/bin/bundle

$ bundle platform --ruby
ruby 2.5.1p57

$ rbenv version
2.5.1 (set by /path/to/app/.ruby-version)

$ rbenv local
2.5.1

$ rbenv global
2.5.1

$ head Gemfile -n 6
source 'https://rubygems.org'
git_source(:github) { |name| "https://github.com/#{name}.git" }

ruby '2.5.1'

gem 'rails', '~> 5.2.0'

$ tail Gemfile.lock -n 5
RUBY VERSION
   ruby 2.5.1p57

BUNDLED WITH
   1.16.3

捆绑包环境

$ bundle env

## Environment

```
Bundler       1.16.3
  Platforms   ruby, x86_64-linux
Ruby          2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
  Full Path   /home/vagrant/.rbenv/versions/2.5.1/bin/ruby
  Config Dir  /home/vagrant/.rbenv/versions/2.5.1/etc
RubyGems      2.7.6
  Gem Home    /home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0
  Gem Path    /home/vagrant/.gem/ruby/2.5.0:/home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0
  User Path   /home/vagrant/.gem/ruby/2.5.0
  Bin Dir     /home/vagrant/.rbenv/versions/2.5.1/bin
Tools
  Git         1.9.1
  RVM         not installed
  rbenv       rbenv 1.1.1-37-g1c772d5

  chruby      not installed
```

## Bundler Build Metadata

```
Built At          2018-07-17
Git SHA           12e73cab7
Released Version  true
```

... (snip)

RUBY VERSION
   ruby 2.5.1p57

BUNDLED WITH
   1.16.3

宝石环境

RubyGems Environment:
  - RUBYGEMS VERSION: 2.7.6
  - RUBY VERSION: 2.5.1 (2018-03-29 patchlevel 57) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0
  - USER INSTALLATION DIRECTORY: /home/vagrant/.gem/ruby/2.5.0
  - RUBY EXECUTABLE: /home/vagrant/.rbenv/versions/2.5.1/bin/ruby
  - EXECUTABLE DIRECTORY: /home/vagrant/.rbenv/versions/2.5.1/bin
  - SPEC CACHE DIRECTORY: /home/vagrant/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/vagrant/.rbenv/versions/2.5.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/vagrant/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0
     - /home/vagrant/.gem/ruby/2.5.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/vagrant/.rbenv/versions/2.5.1/bin
     - /home/vagrant/.rbenv/libexec
     - /home/vagrant/.rbenv/plugins/ruby-build/bin
     - /home/vagrant/.rbenv/shims
     - /home/vagrant/.rbenv/shims
     - /home/vagrant/.rbenv/shims
     - /home/vagrant/.rbenv/bin
     - /home/vagrant/.rbenv/shims
     - /home/vagrant/.rbenv/bin

$ cat /home/vagrant/.rbenv/versions/2.5.1/bin/bundler
#!/home/vagrant/.rbenv/versions/2.5.1/bin/ruby
#
# This file was generated by RubyGems.
#
# The application 'bundler' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require 'rubygems'

version = ">= 0.a"

if ARGV.first
  str = ARGV.first
  str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
  if str =~ /\A_(.*)_\z/ and Gem::Version.correct?($1) then
    version = $1
    ARGV.shift
  end
end

if Gem.respond_to?(:activate_bin_path)
load Gem.activate_bin_path('bundler', 'bundler', version)
else
gem "bundler", version
load Gem.bin_path("bundler", "bundler", version)
end

2 个答案:

答案 0 :(得分:0)

您是从项目目录中执行命令ruby -v吗?另外,如果您安装了新的Ruby版本,则还应该更新捆绑程序。 gem install bundlergem update bundler会为您完成此操作。

答案 1 :(得分:0)

游戏晚了一点,但这也许对某些人有帮助:

TLDR;您可能有冲突的版本管理器。

更多背景信息:

我使用Thoughtbot的笔记本电脑脚本来管理安装在计算机上的软件。我已经很长时间没有更新了。我终于更新了,但是在更新后发现我的应用程序不再起作用。在花了很多时间遵循该问题的通常建议之后,我终于更加注意了我收到的实际错误,并注意到对asdf的引用。事实证明asdf是一个通用的语言版本管理器,并且脚本已在一段时间前开始使用它。在那之前我从未听说过。我阅读了一些文档,听起来还不错,所以我删除了rbenv,进行了基本设置,瞧,我的应用程序又可以正常工作了!如果一个人有其他的ruby版本管理器,这可能也很重要。

长话短说,拥有多个版本管理器可能不是一个好主意,它可能导致运行应用程序时出现各种问题。