31

My Mac OS X has a default ruby.

$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

I installed rbenv and ruby version 2.1.2.

$ rbenv versions
  system
* 2.1.2 (set by /Users/sdw/.rbenv/version)

and I tried to set the global ruby version to 2.1.2, but nothing happened.

$ rbenv global 2.1.2
$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

So I tried to do the same thing with command sudo, but the result is the same.

$ sudo rbenv global 2.1.2
$ ruby -v
ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

How can I change my global ruby version to 2.1.2? I never installed rvm or other ruby version managing system.

sawa
  • 165,429
  • 45
  • 277
  • 381
qodot
  • 401
  • 1
  • 4
  • 16
  • 1
    These questions might be useful: http://stackoverflow.com/questions/13884110/cant-change-rbenv-global-ruby-version and http://stackoverflow.com/questions/10940736/rbenv-not-changing-ruby-version – Powers Jul 14 '14 at 12:16

8 Answers8

108

Did you add the following lines to your ~/.bash_profile?

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

See: https://github.com/sstephenson/rbenv#installation

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • 3
    this answer is no longer correct for recent versions of rbenv; see more recent question https://stackoverflow.com/questions/40199156/why-do-i-need-to-add-rbenv-bin-to-my-path#40204828 – dcorking Jan 24 '18 at 08:23
  • @dcorking: I am not sure if I agree with your comment. First, at the moment rbenv's documentation still tells to add that line. Second, that folder does exist on my machine. I guess it depends on how someone installed rbenv. – spickermann Jan 24 '18 at 08:34
  • rbenv's documentation is inconsistent and does not recommend this at all for homebrew installations. At the next step of github checkout, it says "Run ~/.rbenv/bin/rbenv init" which makes prepending `bin` to the path unnecessary. Let's try without homebrew and submit a Pull Request. – dcorking Jan 24 '18 at 08:46
  • See https://www.digitalocean.com/community/tutorials/how-to-install-ruby-on-rails-with-rbenv-on-ubuntu-18-04 – prograils Dec 12 '18 at 13:58
  • 1
    This worked for me too. Added this to my ~/.zshrc – sga4 Feb 28 '23 at 11:00
  • On Mac OS High Sierra & this worked. – batman Mar 23 '23 at 04:43
13

Type $ rbenv version (without "s")

It is possible that output will show a path to the file that is locking the version

2.0.0p451 (set by /path/to/some/directory/.ruby-version)

It is even possible that the file is a hidden file named .ruby-version in your current dir. If its the case just remove that .ruby-version file

Benjamin Bouchet
  • 12,971
  • 2
  • 41
  • 73
  • In my case this was the correct answer, thank you @Benj – valfer Dec 14 '16 at 15:25
  • 3
    This is the case for me - `/.ruby-version` specifies 2.5.5. But the thing is, that's the version that I _want_. And it's also what my local version is set to, through rbenv. And yet, when I try to install gems or run `ruby -v`, I get 2.3.7 and I can't figure out why. :/ – David Kennell Aug 29 '19 at 18:41
  • Thanks a lot. For some reason, my `~` folder had a .ruby-version file. – sanjarcode Aug 14 '23 at 15:38
7

Run the below commands before change ruby version:

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

It works for me :)

Yoyo
  • 165
  • 2
  • 4
6

Uninstall ruby and then install agains:

brew uninstall -f ruby
brew install ruby

Add this two line to you ~/.bash_profile

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

Reopen the console and that's it and run

ruby -v 
JCLopez
  • 101
  • 1
  • 3
3

I realize that I occasionally set local ruby version in home folder, from which I use terminal by default :) So that's what worked for me:

rm /home/MYUSERNAME/.ruby-version
Kiryl Plyashkevich
  • 2,157
  • 19
  • 18
2

I had the same issue ("ruby -v" showing the initial version) but reloading (exit/start) my terminal made the trick.

Did you try that first?

0

If you are installing ruby on rails on ubuntu,and you rbenv doesn't change your ruby version

> rbenv list # This lists all the rbenv versions available
  • 2.6.8
  • 2.7.4
  • 3.0.2
  • jruby-9.3.1.0
  • mruby-3.0.0
  • rbx-5.0
    -truffleruby-21.3.0
    -truffleruby+graalvm-21.3.0

In my case I need to install ruby 3.0.2

rbenv install 3.0.2

After this also it shows ruby v [2.7.0-]

rbenv global 3.0.2

This will help you change your ruby version.

-2

instead of:
rbenv global 2.7.1
do:
sudo rbenv global 2.7.1