31

I'm trying to run

env RAILS_ENV=test bundle exec rake db:migrate

and get the following error

Your Ruby version is 2.1.7, but your Gemfile specified 2.2.3

ruby -v

gives me

ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-darwin15]

I'm using rbenv, if that matters. rbenv versions gives the following: system * 2.2.3 (set by /Users/thatsme/Projects/demoproject/.ruby-version) So I have no ruby 2.1.7 installed. Spring is not running and I've run rbenv rehash. Then bundler gem is installed. I'm going nuts on this. Can somebody please tell me why the wrong ruby version is being used? Thanks!

Linus
  • 4,643
  • 8
  • 49
  • 74
  • 1
    This link may have some options: http://stackoverflow.com/questions/23039528/your-ruby-version-is-2-0-0-but-your-gemfile-specified-2-1-0 – A Fader Darkly Oct 30 '15 at 15:12
  • 1
    Thanks for the link but it was not a missing bundler gem. I've updated my question accordingly. – Linus Oct 30 '15 at 15:15
  • I had the same issue yesterday - I [followed this articles steps](https://gorails.com/setup/osx/10.11-el-capitan#ruby) to install ruby and I forgot to add rbenv to bast - notice the `# Add rbenv to bash so that it loads every time you open a terminal` comment in the article. – Vucko Oct 30 '15 at 15:20
  • @MichalSzyndel as I wrote, `rbenv rehash` didn't solve the problem. @Vucko thanks for the hint. I'm struggling to make this work for fish shell. – Linus Nov 03 '15 at 12:33
  • what does `which bundle` tell you? – Mike Szyndel Nov 03 '15 at 20:12
  • `/Users/nandersen/.rbenv/shims/bundle` @MichalSzyndel – Linus Nov 09 '15 at 07:21
  • `/Users/nandersen/.rbenv/shims/ruby` @MichalSzyndel – Linus Nov 09 '15 at 08:38
  • Can you make sure once again that your desired ruby version is activated in this directory in rbenv? – Mike Szyndel Nov 09 '15 at 08:51
  • You mean via `rbenv local 2.2.3`? – Linus Nov 09 '15 at 08:52

4 Answers4

15

Running the command below helped me somehow: rbenv exec gem install bundler

Ricardo Green
  • 1,058
  • 1
  • 11
  • 27
6

Assumption: You are using RVM. This means there's a ruby version installed outside of RVM. Clear your rvm rubies by running

rvm uninstall <ruby version>

once you have uninstalled all rvm rubies do ruby -v, if this returns an output specifying a ruby version then thats the root of the problem. Uninstall it with

sudo apt-get remove ruby

Now install your rvm rubies with rvm install <ruby version> and set it as default rvm use <ruby version>

Now install bundler

gem install bundler

And do bundle install

kev
  • 1,148
  • 2
  • 14
  • 29
4

TLDR;

Check really carefully the content of your .bash_profile or .bashrc file.

None of the answers actually solve my problem. So here's my solution.

This is the error that I got:

$ bundle install
Your Ruby version is 3.0.1, but your Gemfile specified 2.7.1

I checked my rbenv setup:

$ rbenv versions
  system
  2.4.1
  2.5.0
  2.7.0
* 2.7.1 (set by /Users/setoelka/awesome-project/.ruby-version)
  3.0.1

I uninstall the wrong version to probably reveal a new error. I can just install it again later, I was thinking.

$ rbenv uninstall 3.0.1

It does reveal a new error:

$ bundle install
-bash: /Users/setoelka/.gem/ruby/3.0.0/bin/bundle: /Users/setoelka/.rbenv/versions/3.0.1/bin/ruby: bad interpreter: No such file or directory

Ok, that's strange. I can just remove the .gem directory there.

$ rm -rf ~/.gem/

Now another new error:

$ bundle install
-bash: /Users/setoelka/.gem/ruby/3.0.0/bin/bundle: No such file or directory

It seems like my PATH variable is messed up. So I do:

$ gem environment
RubyGems Environment:
  - RUBYGEMS VERSION: 3.2.27
  - RUBY VERSION: 2.7.1 (2020-03-31 patchlevel 83) [x86_64-darwin20]
  - INSTALLATION DIRECTORY: /Users/setoelka/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0
  - USER INSTALLATION DIRECTORY: /Users/setoelka/.local/share/gem/ruby/2.7.0
  - RUBY EXECUTABLE: /Users/setoelka/.rbenv/versions/2.7.1/bin/ruby
  - GIT EXECUTABLE: /usr/bin/git
  - EXECUTABLE DIRECTORY: /Users/setoelka/.rbenv/versions/2.7.1/bin
  - SPEC CACHE DIRECTORY: /Users/setoelka/.local/share/gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/setoelka/.rbenv/versions/2.7.1/etc
  - RUBYGEMS PLATFORMS:
     - ruby
     - x86_64-darwin-20
  - GEM PATHS:
     - /Users/setoelka/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0
     - /Users/setoelka/.local/share/gem/ruby/2.7.0
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/setoelka/.rbenv/versions/2.7.1/bin
     - /usr/local/Cellar/rbenv/1.1.2/libexec
     - /Users/setoelka/opt/anaconda3/bin
     - /Users/setoelka/opt/anaconda3/condabin
     - /Users/setoelka/.cargo/bin
     - /Users/setoelka/.gem/ruby/3.0.0/bin
     - /Users/setoelka/.nvm/versions/node/v16.5.0/bin
     - /Users/setoelka/.rbenv/shims
     - /Users/setoelka/.rbenv/shims
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Library/Apple/usr/bin

So, it's clear I missed something when checking out my .bash_profile file. That's the problem there:

     - /Users/setoelka/.gem/ruby/3.0.0/bin

That line was somehow buried under a plethora of settings inside my .bash_profile.

Seto
  • 1,234
  • 1
  • 17
  • 33
-1

I had the same problem

I needed to run

bundle

to reinstall all my gems. then

bundle exec rails c

David Chan
  • 7,347
  • 1
  • 28
  • 49