bundle uses wrong ruby version
Asked Answered
H

4

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!

Halation answered 30/10, 2015 at 15:8 Comment(9)
This link may have some options: #23040028Convertiplane
Thanks for the link but it was not a missing bundler gem. I've updated my question accordingly.Halation
I had the same issue yesterday - I followed this articles steps 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.Sapanwood
@MichalSzyndel as I wrote, rbenv rehash didn't solve the problem. @Sapanwood thanks for the hint. I'm struggling to make this work for fish shell.Halation
what does which bundle tell you?Bottali
/Users/nandersen/.rbenv/shims/bundle @MichalSzyndelHalation
/Users/nandersen/.rbenv/shims/ruby @MichalSzyndelHalation
Can you make sure once again that your desired ruby version is activated in this directory in rbenv?Bottali
You mean via rbenv local 2.2.3?Halation
U
17

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

Une answered 30/10, 2018 at 6:57 Comment(3)
Oh wow. I've been searching all around on dozens of similar issues with solutions that haven't worked, and this finally fixed it for me. Thanks!Crosshead
rbenv exec gem install bundler zsh: command not found: rbenvBonaire
for asdf user asdf exec gem install bundler. Seems to fix for me.Appalachian
P
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

Protectionism answered 29/1, 2017 at 11:56 Comment(2)
Seems that just running "gem install bundler" (after updating ruby version, doint it with RVM isn't a prerequisite) solves the problemStriper
This works for me, but why on earth would this make a difference in identifying the ruby to use?Equal
E
5

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.

Engadine answered 22/9, 2021 at 20:19 Comment(1)
Cool! What worked for me was rm -rf ~/.gem/ this command. Thanks!Fancy
P
-1

I had the same problem

I needed to run

bundle

to reinstall all my gems. then

bundle exec rails c

Persuader answered 1/5, 2016 at 6:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.