Which Ruby Version am I Really Running

Which Ruby version am I really running?

Run this command:

rvm get stable --auto-dotfiles

and make sure to read all the output. RVM will tell you if something is wrong, which in your case might be because GEM_HOME is set to something different then PATH.

Which ruby version am I using?

If ruby -v shows 2.6.3, then you are using the ruby which ships with the system by default. You can confirm this in several ways. Running the shell command which ruby will show /usr/bin/ruby. Checking your PATH will show /usr/bin earlier than the location of the homebrew or rbenv installations.

If you wish to run one of the others, you can put it earlier on the PATH or invoke it explicitly by giving the fully qualified name such as /usr/local/opt/ruby/bin/ruby myscript.rb. Another alternative which avoids twiddling the PATH variable is to use a shebang line at the beginning of different scripts pointing explicitly to the version to use with that script.

How to change which version of Ruby I am using

You should be using a Ruby version manager to manage multiple versions of Ruby. I prefer using rbenv. Following are the steps to install it on a mac (they are explained in detail about what is being done and why; if you want a shortcut, try running all the commands in sequence but I'd still insist you read through the steps).

rbenv

Before proceeding with the actual installation, remember these points:

  1. rbenv in itself does not include the ability to install ruby versions. It only changes ruby version per directory. For installing rubies, you need to install the ruby-build tool (which is part of the rbenv project). It is the same for chruby as well which uses another tool to build ruby. EDIT (June 2021): It looks like rbenv installations now come with ruby-build and are capable of compiling ruby right on your machine. I will leave the answer as it is for so that it makes sense for someone on an older setup (or in case they revert that decision later).
  2. ruby-build has to be installed as a plugin to rbenv.
  3. In case you need to learn about the tools in detail, here are the links for rbenv and ruby-build.
  4. The installation procedure for both tools (and a lot of other help) is available in the README files for the projects. Refer to those if things don't work for you.

Installing rbenv

Run the following command to clone rbenv repo into .rbenv directory in your home directory.

$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv

Your system still does not know where rbenv is. Add it to your path by running:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile

To initialize rbenv so that it can help you with changing rubies when you change directories, run this:

~/.rbenv/bin/rbenv init

This should tell you something like this:

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

eval "$(rbenv init -)"

So run this:

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

By this point rbenv should be installed. When you run rbenv on the command line, you should get something like this:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

NOTE: If you get a warning saying that rbenv is not installed, just run source ~/.bash_profile. That will rerun the ~/.bash_profile script and get rbenv in your path. You should be able to run rbenv after that without trouble.

Do notice that rbenv does not give an option to install or uninstall rubies yet. For this we need to install ruby-build.

Installing ruby-build

We need to add the ruby-build package as a rbenv plugin so that we can type rbenv install <ruby version> to install rubies. All you need to do is to create the plugins directory and checkout the git repo for ruby-build in the plugins directory. Run the following:

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

Testing if rbenv and ruby-build have been installed

Run rbenv without any arguments on the terminal should now show the install and uninstall commands being available. Something like this:

$ rbenv
rbenv 1.1.1-39-g59785f6
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
commands List all available rbenv commands
local Set or show the local application-specific Ruby version
global Set or show the global Ruby version
shell Set or show the shell-specific Ruby version
install Install a Ruby version using ruby-build
uninstall Uninstall a specific Ruby version
rehash Rehash rbenv shims (run this after installing executables)
version Show the current Ruby version and its origin
versions List all Ruby versions available to rbenv
which Display the full path to an executable
whence List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/rbenv/rbenv#readme

If you see that output, your rbenv is installed properly.

Installing ruby

To install ruby 2.5.3, you could run (wait, don't run it yet):

rbenv install 2.5.3

It should output a few lines, take some time and then tell you that version 2.5.3 was installed. However, there is a problem - if the installation fails, especially during compilation, sometimes, the terminal gets stuck and there is no output on the terminal. It just appears to be installing for a long time (forever). To get more info about what is happening, run the following:

rbenv install -f -v 2.5.3

The -f argument tells rbenv to force-install the given version. So if it is already installed, rbenv will re-install (basically overwrite) the given version. So if an installation failed, -f will make sure of the installation.

The -v argument tells rbenv to output verbose messages. So everything that ruby-build does (including the compilation process) will be shown to you. Don't be afraid by the word compilation here. It normally compiles fine without troubles and does not alter your system ruby (the one installed with sudo apt install ruby on Linux or the one you get by default on macOS) whether it succeeds or fails.

Test installation

Once the installation succeeds, you can run the command below to check which versions are installed (output included in the snippet below):

$ rbenv versions
system
* 2.5.3 (set by /home/ubuntu/.rbenv/version)

Note: On mac, the newly installed ruby will have a different path.

The one with the * in front of it is the one which is active right now. If you run which ruby, you should get a path with a ruby shim. If you are curious, read the rbenv documentation to know what shims are, though you wouldn't have to worry about them.

$ which ruby 
/home/ubuntu/.rbenv/shims/ruby

Configure & Forget

rbenv is a cool thing to have but to keep writing rbenv shell 2.5.3 and rbenv shell 2.4.5 every single time is a problem. What you should instead do is set a version of ruby for the directory and forget about rbenv.

You can just create a file named .ruby-version containing one line - the version number of ruby you want to use for all ruby scripts within this directory (and subdirectories). Just cd to the required directory and run:

echo "2.5.3" > .ruby-version

All ruby scripts within that directory and subdirectories will then use version 2.5.3.

Trouble in understanding what Ruby version I am actually / effectively running

as @sarnold suggests you will have to restart any long running processes such as daemons.

Another thing is that if it is running in a cron tab, make sure the cron task is running as the right user. If you set up RVM for a user, and not the system, then root, or any other user than you didn't set up RVM for, will be using system ruby.

You carefully snipped out ""

Note this can easily happen with

sudo rake some:task # that has to do something as root like open a low port or move a file

this will run as root and not do the right RVM sourcing. To fix this, there is

rvmsudo rake some:task

Ruby version not same as installed version

The output from this command brew install ruby is not telling you that you're running ruby 2.7.1. That just says you already have ruby 2.7.1 installed for brew. But you could have other ruby versions installed in other ways. When you do

ruby -v

ruby executable is looked for in paths listed inside your environment variable PATH, in order. You can see those paths with

echo $PATH

So, managing different versions of ruby is hard and version managers exist for this reason. I suggest you to install rvm

Need to set ruby version each time a terminal session is started

Check in your gemfile if there is a references to 1.9.3

If not, check this answer and follow the instructions How to set default Ruby version with RVM?



Related Topics



Leave a reply



Submit