438

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
5
  • Have you definitely put the init in your bashrc correctly? What does your $PATH look like?
    – d11wtq
    Jun 7, 2012 at 22:30
  • None of those commands change the Ruby version rbenv is using, but it appears it's not using your default Ruby. Jun 7, 2012 at 23:14
  • @d11wtq was correct. it wasnt in my ~/.bash_profile correctly apparently.
    – ajorgensen
    Jun 7, 2012 at 23:19
  • make sure you follow ALL the installation steps for rbenv. See here or my answer
    – mfaani
    Dec 26, 2020 at 22:32
  • 1
    If ruby was installed through brew, uninstall that first and then use rbenv. Apr 20, 2022 at 13:50

31 Answers 31

849

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.

17
  • 46
    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. Mar 21, 2013 at 2:22
  • 22
    Or do exec $SHELL -l (lower L)
    – pdu
    Jun 6, 2013 at 5:54
  • 5
    Where do I have to add the "$HOME/.rbenv/shims" and "$HOME/.rbenv/bin"?
    – Benedikt
    Jul 18, 2013 at 15:35
  • 5
    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.
    – merqlove
    Oct 3, 2013 at 17:47
  • 1
    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.
    – alexhilton
    Oct 24, 2014 at 10:18
194

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.

6
  • +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)?
    – Jerome
    Apr 17, 2016 at 9:28
  • 7
    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.
    – Dennis
    Jan 10, 2017 at 4:20
  • 2
    This fixed my problem. Jan 22, 2017 at 1:49
  • 1
    This should be in the README
    – onmyway133
    Sep 11, 2018 at 7:55
  • 1
    Worked for me after hours of trying other stuff. Thanks!
    – cjs1978
    Oct 2, 2018 at 10:43
96

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.

5
  • 9
    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. Nov 26, 2014 at 6:27
  • Same problem. Same solution. (May have to look at submitting a PR to advise when global Ruby version is overruled by a ~/.ruby-version...) Jun 12, 2015 at 6:40
  • 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 Jun 16, 2016 at 22:26
  • I'm shocked I had to dig this far for an answer, this is the correct one for me.
    – Ian Hyzy
    Feb 25, 2019 at 20:54
  • 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. Jul 11, 2019 at 14:44
85

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" ;)

4
  • 3
    Ah mate, finally! Tried about 25 different solutions before I found this one, thank you Jun 8, 2018 at 16:09
  • Well explained solution. Thank you. Mar 19, 2020 at 18:55
  • 2
    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
    – Jerome
    Aug 25, 2020 at 13:42
  • Don't know why, but running $ rbenv local --unset; rbenv shell --unset and setting the global version worked for me. Even after restoring the .ruby-version file with the same version number, rbenv started working as expected. Sep 7, 2022 at 20:54
71

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.

5
  • 1
    You are a 🌟. Worked on macOS Monterey. Jun 13, 2022 at 14:22
  • @PHPNoob The OP mentions that AFAICS. Right near the bottom, it says "quit your terminal using Command + Q, and then open a new terminal." Unless I'm missing something, that's specific directions on how to restart the terminal.
    – BobRodes
    Nov 13, 2022 at 19:30
  • 1
    Worked on macOS Venutra 🔥
    – rubeonline
    Dec 6, 2022 at 8:15
  • 1
    Thanks 🙏🏻 works on Ventura too Dec 30, 2022 at 1:45
  • 1
    Thanks a lot, it worked on Ventura M1 Mac too Feb 12, 2023 at 15:06
46

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]
4
  • 2
    This perfectly works. I added that into .bash_profile not /zshenv.
    – Pokemon
    Jan 28, 2020 at 13:07
  • Probably it will be necessary to run source ~/.zshrc, but it worked for me . Thanks you. Mar 16, 2022 at 17:17
  • Works for me, but I don't understand what is the difference. .zshenv was also executed (checked it with echo inside. Oh, and not with bin, but with shims in a PATH. Dec 26, 2022 at 15:05
  • 1
    After trying many suggestions, finally this one works for me. Probably this needs to be added to the official document.
    – keylogger
    Oct 2, 2023 at 22:59
38

Update 2023 (Macbook Ventura+)

Even if you have rbenv installed, your computer could still serve the default ruby version. To fix this, just run the following:

rbenv init

This might give an output like this:

# Load rbenv automatically by appending
# the following to ~/.zshrc:

eval "$(rbenv init - zsh)"

Just open your .zshrc file:

sudo nano ~/.zshrc

(Or you can also open this file with any other text editor)

Add this line:

eval "$(rbenv init - zsh)"

Save the file (if used sudo nano, you can do so like this:) CTRL + X then Y then Enter (For Mac)

Now, refresh your .zshrc profile like this:

source ~/.zshrc

Now type ruby --version, this must give you the ruby version that was provided by rbenv.

2
  • it was CONTROL X instead of CMD X for me. Feb 13, 2023 at 8:35
  • Indeed, it was CTRL + X for me as well. Updating the answer! Feb 13, 2023 at 9:07
32

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

3
  • 3
    I spent hours to fix this. Finally your solution helped. Thanks!! Oct 27, 2018 at 1:37
  • I also tried all the answers above, none worked for me except this one. Thanks Feb 19, 2021 at 19:53
  • thhhhhhhhhhhhhanks Mar 14, 2023 at 21:56
31

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.

2
  • 1
    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 -)".
    – dadoonet
    Jan 25, 2013 at 17:37
  • Nice little eval statement for this use case! Mar 19, 2020 at 18:54
10

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.

1
  • People on Mac os Catalina, should follow the zsh instruction
    – Abhinay
    Nov 22, 2019 at 6:36
8

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.

1
  • If ruby was installed through brew, it must be uninstalled to use rbenv properly Apr 20, 2022 at 13:49
8

I tried every solution, but nothing worked for me. Even I was including the path in the .zshrc file.

In my case, I'm using MacBook Pro M2 with macOS Ventura 13.2. And I'm using React Native 0.71.2, which needed Ruby 2.7.6 version.

I simply placed the following code in the .zprofile instead of .zshrc, and it worked:

export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"
2
  • I was only missing the second line. Once added, closed and reopened the terminal and it worked fine
    – stevec
    Feb 10, 2023 at 6:03
  • both of these lines are mandatory. using only single won't work Aug 8, 2023 at 13:56
7

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

1
  • '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.
    – Jeremy Rea
    May 15, 2016 at 15:59
6

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.

5

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.

1
  • 1
    Only thing that worked for me
    – 0hio
    Oct 9, 2023 at 5:00
5

for fish shell user

set --universal fish_user_paths $fish_user_paths ~/.rbenv/shims/
3
  • It worked, how did you come about this solution? Mar 1, 2021 at 18:12
  • it works thank you, same question how did you comes with this ?????
    – Almokhtar
    Sep 22, 2021 at 11:38
  • 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. Sep 30, 2021 at 1:10
5

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 
5

Adding the following to .bashrc works for me -

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

1
  • 2
    this is exactly what the accepted answer of 2020 says .
    – ruud
    Dec 1, 2022 at 13:10
3

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.)

3

Make sure the last line of your .bash_profile is:

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

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.

2

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.

1

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

  1. $ brew install rbenv
  2. $ 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...

  1. Close terminal and open it again
  2. Verify it's working correctly by running:
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
  1. Now just install the version you want by doing: rbenv install <version>
1

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]
0

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

0

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.

0

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
0

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

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

0

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

-1

enter image description here

Apparently, it worked for me when I used the Rosetta terminal

You can try doing the same thing using Rosetta terminal

by going into

Finder > Utilities > Terminal > Get Info

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.