ChatGPT解决这个技术问题 Extra ChatGPT

rbenv not changing ruby version

I installed rbenv according to the github directions. I am running OSX but I have tried this on a Ubuntu 12.04 VM and got the same results. The following is what i get in my terminal when I try to change ruby versions:

rbenv versions
* 1.9.3-p0 (set by /Users/user/.rbenv/version)
1.9.3-p125

rbenv global
1.9.3-p0

rbenv rehash

ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [universal-darwin11.0]

which ruby
/usr/bin/ruby

Anyone have any ideas as to why rbenv isn't switching the ruby version like it thinks it is? Also there is no .rbenv file in the local directory that would be causing the ruby version to default to 1.8.7

rbenv local
rbenv: no local version configured for this directory
Have you definitely put the init in your bashrc correctly? What does your $PATH look like?
None of those commands change the Ruby version rbenv is using, but it appears it's not using your default Ruby.
@d11wtq was correct. it wasnt in my ~/.bash_profile correctly apparently.
make sure you follow ALL the installation steps for rbenv. See here or my answer
If ruby was installed through brew, uninstall that first and then use rbenv.

J
Jett

Check that PATH contains $HOME/.rbenv/shims and $HOME/.rbenv/bin

$ env | grep PATH

Also check that you have the following in your ~/.bash_profile if using bash or ~/.zshenv if using zsh

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

NOTE: Make sure it's the last setting in your ~/.bash_profile . I ran into an issue where I installed a program that updated my .bash_profile and reset PATH.

Finally, make sure your $HOME folder doesn't have a .ruby-version file that you may have created by accident if you were to have done $ rbenv local <ruby-version> in your $HOME folder. Doing $ rbenv global <ruby-version> modifies the $HOME/.rbenv/version file, and the existence of a .ruby-version file in the $HOME folder would override the version set by $HOME/.rbenv/version.

From the docs:

Choosing the Ruby Version When you execute a shim, rbenv determines which Ruby version to use by reading it from the following sources, in this order:

The RBENV_VERSION environment variable, if specified. You can use the rbenv shell command to set this environment variable in your current shell session.

The first .ruby-version file found by searching the directory of the script you are executing and each of its parent directories until reaching the root of your filesystem.

The first .ruby-version file found by searching the current working directory and each of its parent directories until reaching the root of your filesystem. You can modify the .ruby-version file in the current working directory with the rbenv local command.

The global ~/.rbenv/version file. You can modify this file using the rbenv global command. If the global version file is not present, rbenv assumes you want to use the "system" Ruby—i.e. whatever version would be run if rbenv weren't in your path.


On OSX, don't forget to log out of your OSX session and then log back in for the changes to your bash profile to take effect.
Or do exec $SHELL -l (lower L)
Where do I have to add the "$HOME/.rbenv/shims" and "$HOME/.rbenv/bin"?
I made like this: export RBENV_ROOT="$HOME/.rbenv" if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi Working pretty good :) Also check for PATH init's after rbenv declaration.
For my case, I need to append these two lines into .bashrc (Bash in Ubuntu) and then I got correct ruby version. It does not work if append it into .bash_profile.
m
mfaani

I fixed this by adding the following to my ~/.bash_profile:

#PATH for rbenv
export PATH="$HOME/.rbenv/shims:$PATH"

This is what is documented at https://github.com/sstephenson/rbenv.

From what I can tell there isn't ~/.rbenv/bin directory, which was mentioned in the post by @rodowi.


+1 for the link. +1 for the answer which does contradict a better voted answer. Which raises a further question - and if anyone can chime in - how can export PATH="$HOME/.rbenv/bin:$PATH work (as it was failing for me)?
There won't be a ~/.rbenv/bin/ if you installed via homebrew or linuxbrew. And you shouldn't have to set up the shims path yourself, as rbenv init does this (and more) for you.
This fixed my problem.
This should be in the README
Worked for me after hours of trying other stuff. Thanks!
a
alphadogg

This may be an old question, but Google led me here and, for posterity sake, thought I'd share.

My problem persisted after many of the recommended solutions above. Like the OP, I installed rbenv and then a ruby version, but my laptop defaulted to system. What I had overlooked was that when I ran:

[~/.rbenv] $ rbenv versions
* system (set by /Users/alphadogg/.rbenv/version)
  2.0.0-p247

IOW, it was still defaulting to system. A quick

[~/.rbenv] $ rbenv local 2.0.0-p247

switched it to the new version.


Yes - funny how rbenv install guides don't mention running the local command at all. Lots of blogs remind you to run rbenv global but not local. Great answer.
Same problem. Same solution. (May have to look at submitting a PR to advise when global Ruby version is overruled by a ~/.ruby-version...)
I had to get rid of system by removing with YUM in Centos 7, only after that I could see the correct version selected in rbenv
I'm shocked I had to dig this far for an answer, this is the correct one for me.
I found that the rbenv versions command said that 2.3.1 was set in .ruby-versions by rvm perhaps. I had stopped using rvm awhile ago and changed to rbenv. Deleted .ruby-versions and ran rbenv versions again and it had the latest selected by rbenv ( be sure you have the eval statement above in .bash_profile!). Now, all works as before. Thanks for helping me get this solved.
J
Jett

First step is to find out which ruby is being called:

$ which ruby

Your system says:

/usr/bin/ruby

This is NOT the shim used by rbenv, which (on MacOS) should look like:

/Users/<username>/.rbenv/shims/ruby

The shim is actually a script that acts like a redirect to the version of ruby you set.

I recommend that for trouble shooting you unset the project specific "local" version, and the shell specific "shell" version and just test using the "global" version setting which is determined in a plain text file in ~/.rbenv/version which will just be the version number "1.9.3" in your case.

$ rbenv global 1.9.3
$ rbenv local --unset
$ rbenv shell --unset

You can do ls -laG in the root of your project folder (not the home folder) to make sure there is no longer a ".ruby-version" file there.

You can use rbenv versions to identify which version rbenv is set to use (and the location and name of the file that is setting that):

$ rbenv versions

NONE OF THAT MATTERS until you set the path correctly.

Use this to make sure your *MacOS will obey you:

$ rbenv init -

Followed by:

$ which ruby

To make sure it looks like:

/Users/<username>/.rbenv/shims/ruby

Then run this to add the line to your profile so it runs each time you open a new terminal window:

$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile

There are other ways to modify the path, feel free to substitute any of them instead of running the rbenv init.

NOTE: reinstall Rails with:

$ gem install rails

If you were trying to run Ruby on Rails, then you need to have this all working first, then install the rails gem again. A previous install of Rails will use a hard coded path to the wrong ruby and several other things will be in the wrong place, so just install the gem again.

P. S. If your MacOS won't obey you (*mentioned above) then you may have to find another way to modify your path, but that's unlikely to be a problem because "Macs just work" ;)


Ah mate, finally! Tried about 25 different solutions before I found this one, thank you
Well explained solution. Thank you.
this response is the most helpful (and complete) one. One small variant: if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi >> ~/.bash_profile followed by source ~/.bash_profile
P
Peter Piper

In my case changing the ~/.zshenv did not work. I had to make the changes inside ~/.zshrc.

I just added:

# Include rbenv for ZSH
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

at the top of ~/.zshrc, restarted the shell and logged out.

Check if it worked:

➜  ~ rbenv install 2.4.0
➜  ~ rbenv global 2.4.0
➜  ~ rbenv global
2.4.0
➜  ~ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]

This perfectly works. I added that into .bash_profile not /zshenv.
Probably it will be necessary to run source ~/.zshrc, but it worked for me . Thanks you.
N
Neal

I just found this same problem. What I did was uninstall rbenv (via homebrew) and reinstall it. I also added

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

into ~/.bash_profile when I reinstalled rbenv. Works perfectly now.


Note for others that came to the same issue as me: For some reasons, which rbenv was working prior so this line was never executed. I fixed it using directly eval "$(rbenv init -)".
Nice little eval statement for this use case!
R
Rob Bednark

Run this command

Add rbenv to bash so that it loads every time you open a terminal

echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile

this will solve your problem Reference


I spent hours to fix this. Finally your solution helped. Thanks!!
I also tried all the answers above, none worked for me except this one. Thanks
P
Promise Preston

I had this issue when setting up Ruby using rbenv on MacBook Pro Catalina OS and MacBook Pro Big Sur.

Here's how I fixed:

First run the command below to initialize rbenv:

rbenv init

This will give you some instruction on what to do. Basically you will have to open the ~/.zshrc file and add this to the file eval "$(rbenv init -)". You can accomplish this by running the command below:

echo 'eval "$(rbenv init -)"' >> ~/.zshrc

Next, run the command below to set your ruby version:

rbenv local your-desired-ruby-version

In my case, my desired ruby version was 3.0.1, so it was:

rbenv local 3.0.1

When you are done, quit your terminal using Command + Q, and then open a new terminal, this time when you run the command:

ruby -v 
rbenv versions

You will see that your desired ruby version has already been set up for you.

That's all.

I hope this helps


You are a 🌟. Worked on macOS Monterey.
d
drjorgepolanco

If you are using bash, go to

~/.bash_profile

and add the following line (if it's not already there)

if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

If you are using Zsh, go to

~/.zshrc

and add the same line of code, at the end of the .zshrc file.

Then simply restart your terminal and it should be fine now.


People on Mac os Catalina, should follow the zsh instruction
h
htmldrum

rbenv help shell

"Sets a shell-specific Ruby version by setting the 'RBENV_VERSION' environment variable in your shell. This version overrides localapplication-specific versions and the global version. should be a string matching a Ruby version known to rbenv.The special version string 'system' will use your default system Ruby. Run rbenv versions' for a list of available Ruby versions."

Provided rbenv was installed correctly, ruby -v will correspond to

rbenv shell 1.9.3-p125


'rbenv shell x.y.z' is what worked for me. The 'rbenv local x.y.z' described in the higher ranked answer above did not work on OS X.
d
djb

I had the same problem, but caused by Homebrew:

[~]$ rbenv version
2.3.0 (set by /Users/user/.rbenv/version)
[~]$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]
[~]$ which ruby
/usr/local/bin/ruby

Somehow I had installed Ruby via Homebrew too, and the Homebrew path was ahead of the rbenv path in my $PATH. Running brew uninstall ruby fixed it for me.


If ruby was installed through brew, it must be uninstalled to use rbenv properly
B
Basil Mariano

for fish shell user

set --universal fish_user_paths $fish_user_paths ~/.rbenv/shims/

It worked, how did you come about this solution?
it works thank you, same question how did you comes with this ?????
To be honest, I don't remember anymore lol That was 2017 answer and I wonder why this still works though focused on mobile development now so i never use this stuff anymore.
A
AmitF

run:

rbenv init

After I ran that, when i set my local rbenv version:

rbenv local 2.4.0

then my ruby -v and my rbenv local versions coincided.

Note: You might also want to exit the directory you're in and then go back into it, i've noticed that was necessary for me in order to get things to work.


R
Rootical V.

As for me the easiest way to use rbenv along with zsh is adding rbenv to plugins section in .zshrc config. In my case it looks similar to:

# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git bower rails ruby rbenv gulp npm node nvm sublime)

After that there're no problems when installing, switching, using ruby versions with help of rbenv.

Mind to restart your terminal session after made changes.


D
David

When I had these symptoms, the problem turned out to be that install had failed halfway through for the new Ruby version I was trying to switch to, without me noticing. The fix was to delete & reinstall it.

(This meant that even though ruby 1.9.3 showed up in the rbenv list of available versions, it didn't have an executable on the path where rbenv assumed it would. Since rbenv tries to change your ruby version just by prepending a location to your path, if there's nothing actually in the location it prepends, your OS will happily continue searching your default path and find your system version, in my case like the question's 1.8.7.)


j
janos

Make sure the last line of your .bash_profile is:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

D
Daniel Viglione

The accepted answer suggests to add the following:

export PATH="$HOME/.rbenv/bin:$PATH"

This will not work on Mac OSX, which the OP references. In fact, if you install rbenv via brew install rbenv, which is really the only installation method in Mac OSX, since curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash will FAIL in OSX, then the rbenv executable will be installed in:

$ which rbenv
/usr/local/bin/rbenv

However, even in OSX, the rbenv root will remain in the $HOME directory:

~ viggy$ rbenv root
/Users/viggy/.rbenv

What does this mean? It means when you install rubies, they will install in the given home directory under .rbenv:

$ rbenv install 2.6.0
$ ls ~/.rbenv/versions
2.6.0

Now the brew installation did some work that you would have to perform manually in Linux. For example, in Linux, you would have to install ruby-build manually as a plugin:

$ mkdir -p "$(rvbenv root)/plugins"
$ git clone https://github.com/rbenv/ruby-build.git "(rbenv root)"/plugins/ruby-build

This is already done with the homebrew installation. There is one important step that must be done in the homebrew installation, as in the Linux installation. You must add the rbenv shims to your path. In order to do that, when your shell starts, you have to evaluate the following command (which will in turn add the rbenv shims to the beginning of your $PATH):

$ vim ~/.bash_profile
eval "$(rbenv init -)"
$ source ~/.bash_profile

Now when you run echo $PATH, you will see the rbenv shims:

$ echo $PATH
/Users/viggy/.rbenv/shims:

Now check your ruby version and it will reflect the rbenv ruby installed:

ruby -v
ruby 2.6.0p0 

T
Tomer Shetah

I came to the same problem. Fixed this by run the rbenv global command with sudo. I think it was something permission problem.

update: I finally found the solution. There was one same file "version" on my mac, which is under /usr/local/Cellar/rbenv/0.3.0/. I think it was created by mistake occasionally. you should remove it.


J
John K. Chow

This happened to me right after I reinstalled rbenv. Apparently I had a .ruby-version file in my home directory, specifying a version that no longer existed. Once I deleted the file, everything worked.


m
mfaani

There a lot of of misleading answers that work. I thought it was worth mentioning the steps from the rbenv README.

$ brew install rbenv $ rbenv init and follow the instructions it gives you. This is what I got:

~ $ rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:

eval "$(rbenv init -)"

I updated my ~/.bash_profile...

Close terminal and open it again Verify it's working correctly by running:

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

Now just install the version you want by doing: rbenv install


O
O'seun Iyadi

Linux / Ubuntu Users Step 1:

$ rbenv versions
  system
  2.6.0
* 2.7.0 (set by /home/User/Documents/sample-app/.ruby-version) #Yours will be different
  2.7.2

Step 2:

$ nano /home/User/Documents/sample-app/.ruby-version

Step 3:

$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

p
postmodern

You could try using chruby? chruby does not rely on shims, instead it only modifies PATH, GEM_HOME, GEM_PATH.


a
ajain

I forgot to delete rvm before installing rbenv. I deleted rvm and re-installed rbenv, but system still wasn't using the version of Ruby being designated as global by rbenv. I tried shutting Terminal, but it only worked once I restarted the whole machine.


D
David Hempy

All the other answers here give good advice for various situations, but there is an easier way.

The rbenv docs point us to the rbenv-doctor diagnostic tool that will quickly verify all these potential pitfalls on your system:

curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash

When all is well, you'll see this:

$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash                                     <aws:hd-pmp-developer>
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20201005)
Counting installed Ruby versions: 1 versions
Checking RubyGems settings: OK
Auditing installed plugins: OK

Now, if we break one of those expectations (e.g. remove rbenv-install), the tool will point us directly to the problem, with a link to how to fix it:

$ mv /usr/local/bin/rbenv-install rbenv-install-GONE
                                                                      
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash                                     
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK

===> Checking `rbenv install' support: not found <===
  Unless you plan to add Ruby versions manually, you should install ruby-build.
  Please refer to https://github.com/rbenv/ruby-build#installation

Counting installed Ruby versions: 1 versions
Checking RubyGems settings: OK
Auditing installed plugins: OK

T
Tomer Shetah

Strangely, rbenv version did not set the .rbenv file.

Check with: ls -ltra --> to see that a rbenv was written.


S
Sachin D Naik

Adding eval "$(rbenv init -)" to the .bash_profile on my mac resolved this issue. rbenv local ruby -v gave the set


R
Raja Bose

I will suggest do not use rbenv has couple of issues.

it does not come with bundle version once you install ruby most of time create bundler version pickup issue while running bundle install

To cut down this much effort,I would suggest to use rvm, make life easier. follow this link to install rvm https://nrogap.medium.com/install-rvm-in-macos-step-by-step-d3b3c236953b


This doesn't solve the OP's problem if their organization is using rbenv or if they must use rbenv for some other reason. It's unrelated to the question