We have already installed rbenv. We should now install the ruby version desired by us. In my case that is 2.5.3.


Installing ruby with rbenv is fairly simple. However, it is worth learning that ruby-build is a build tool. What it actually does is that it downloads the source code of ruby and compiles it on your system. It has an advantage that ruby will run faster for you; any C program/application that is compiled on your system is normally faster than a generic package because it gets generated considering the capabilities of your CPU (the details of this topic are too vast to accomodate in this tutorial).

So for rbenv to be able to install a new ruby version on your system, you need to have the essential tools. ruby-build requires you to have a C compiler and a few libraries which would be used for compiling ruby. To make sure everything is buckled up before installation, run this command:

sudo apt install -y build-essential libssl-dev libreadline-dev zlib1g-dev

If you already have those packages, nothing will happen; if you have some of them missing, they will be installed, thus filling the gap for building ruby.

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

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

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

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. {: .notice—info}