Prerequisites

Install Ruby

The easiest way to install Ruby on your Ubunty system is through the apt package manager. At the time of writing, the version in the Ubunty repositories is 2.5.1 which is the the latest stable version of Ruby.

Refresh the packages list and install the ruby-full package with the following commands:

sudo apt update
sudo apt install ruby-full

Once the installation is completed, you can verify that it was successful by printing the Ruby version:

ruby --version

The output will look something like this:

ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]

Install Ruby using Rbenv

Rbenv is a lightweight Ruby version management tool which allows you to easily switch Ruby versions.

By default Rbenv doesn’t handle installing Ruby versions. ruby-build is a tool that helps you to install any version of Ruby you may need. It is available as a standalone program and as a plugin for rbenv.

Install the dependencies required for the ruby-build tool to build Ruby from source:

The script will clone both rbenv and ruby-build repositories from GitHub to ~/.rbenv directory. The installer script also calls another script which will try to verify the installation. The output of the script will look something like below:

As the output tell us, before starting using rbenv we need to add $HOME/.rbenv/bin to our PATH.

Conclusion

We have shown you three different ways to install Ruby on your Ubuntu 18.04 server. The method you choose depends on your requirements and preferences. Even though installing the packaged version from the Ubuntu repository is easier, the Rbenv and RVM methods gives you more flexibility for adding and removing different Ruby versions on a per user basis.