How to set the version of Ruby with rbenv on MacOS? - ruby

I was trying to follow the book "Agile Web Development with Rails 5.1" (on MacOS).
I want ruby -v to be 2.4.1.
Currently, ruby -v says ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-darwin19].
I want to use rbenv to manage the version of Ruby installed. So I tried rbenv install 2.4.1 then rbenv global 2.4.1. But that still did not change the output of ruby -v. What more do I have to do?
I see that which ruby returns ~/.rubies/ruby-2.6.5/bin/ruby. I can't remember how I installed that version of Ruby - it might have been homebrew.

Okay so
brew install rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc
echo 'source ~/.bashrc' >> ~/.bash_profile
rbenv install 2.4.1
rbenv global 2.4.1
rbenv rehash # This is probably what you didn't do, start a new terminal or use rehash
Then try ruby -v and which ruby
You can skip some of the beginning steps in case you have rbenv setup and everything.

I ended up using RVM instead. I'm not sure but I might have had multiple ruby version managers installed when I asked the question.

Related

Set default Ruby version in rbenv

I'm attempting to setup rbenv to use Ruby 3.0.0 for Jekyll (according to this tutorial 3.0.0 is necessary on macos w/ M1)
I installed rbenv with homebrew and ruby 3.0.0 with rbenv install 3.0.0 and set to global with rbenv global 3.0.0
I attempted to add ruby 3.0.0 to my path with:
echo 'export PATH="$HOME/.gem/ruby/3.0.0/bin:$PATH"' >> ~/.zshrc. When I restart my terminal, it seems like it's not pointing to the correct ruby version:
$ gem env home
/opt/homebrew/lib/ruby/gems/3.1.0
[EDIT]
Removing a previous point, as it's not relevant to the question regarding default Ruby version.
According to the rbenv documentation, you have to put into your .bashrc the command
eval "$(rbenv init -)"
Try this, start up a new terminal, and then do the rbenv global stuff.

Mac Ruby stuck on 2.6.3p62

I am trying to update ruby to 2.7.1 on my mac, but every time I do rbenv global 2.7.1; ruby -v always shows 2.6.3.
I even checked with rbenv global and it returned 2.7.1.
Installed ruby via homebrew.
If you are using zsh then probably you can do the following
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
then open a new terminal and check again.

Rbenv Doesn't Work Trying to Install Latest Version of Ruby

I tried updating my Ruby to the current version 2.6.3 in the terminal. When the installation is complete and I check my version it says it's still 2.3.3.
I've tried re-installing 2 times already, both times have not succeded.
Installed ruby-2.6.3 to /Users/user/.rbenv/versions/2.6.3
$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [universal.x86_64-darwin17]
Check the README.md from the project's repo. Your rbenv is not initialized.
Put the following lines into your shell. .bash_profile for Bash, .zshrc for ZSH.
if command -v rbenv &>/dev/null; then
eval "$(rbenv init -)"
fi
After which, restart your shell.
References
Install with Homebrew on macOS

OSX Installing Ruby with rbenv

I have installed Ruby on OSX using rbenv following these instructions: https://gorails.com/setup/osx/10.9-mavericks
I installed 2.2.2 however, even after adding it as the global value in rbenv using rbenv global 2.2.2, the ruby -v command still returns ruby 1.8.7.
Now, I know that in my /usr/bin, ruby is still symlinked to the default OSX version (1.8.7). And I could change the symlink. But I'm wondering what the best practice is here, and how rbenv is supposed to work in terms of multiple ruby versions?
Try rbenv rehash, which you should do after installing anything Ruby (or Gem) related.
Have you added this to .bashrc (or similar for your set up)?
export RBENV_ROOT=/usr/local/var/rbenv
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
and
PATH=/usr/local/bin:/usr/local/sbin:$PATH

rbenv can't change global ruby version

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.
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
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
Run the below commands before change ruby version:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
It works for me :)
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
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
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?
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.
instead of:
rbenv global 2.7.1
do:
sudo rbenv global 2.7.1

Resources