Installing Ruby using rbenv

2 minute read

We have already installed rbenv. We should now install the ruby version desired by us. In my case that is 2.5.3 which happens to be the latest stable version at the time of this writing.

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

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. We have already done that when showing what rbenv does.

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.2" > .ruby-version

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

We will be using this in our final deployment script.

NOTE: Remember that the version must be installed with rbenv. Even if your system ruby version matches the version you put in .ruby-version file, it wouldn’t work.

Updated:

Leave a comment