Installing ruby on Ubuntu using rbenv

Written by
Date: 2020-02-23 10:00:00 00:00

Overview

This guide will help you install ruby on Ubuntu Linux, it was tested using WSL (Linux on Windows 10) and on an Ubuntu 18.04 LTS version.

There are another ways to install ruby, being the easiest to install the Ubuntu version

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install ruby-full

But in this guide we'll use the rbenv tool, which is the most common these days, because it is ligther than RVM which is another common way to install and maintain ruby on Linux or Max OS.

Prepare your system

Because we will install ruby from sources we will be compiling it on our system prior to install it, so we need to prepare our system to do that.

sudo apt-get install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdm-dev openssl libcurl4-openssl-dev libssl-dev zlib1g-dev

Install ruby using rbenv

To install ruby using rbenv we will have to get the rbenv tool first, let's install it.

curl -sL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash -

That line will use git to install all what to need to install and maintain ruby on your Ubuntu system.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

Now that we have everything ready, just install ruby

rbenv install 2.7.0

That is the last verion of ruby at the time of this writting

rbenv global 2.7.0

That final line defines that the system will use the installed version, you can switch that if you need, of course you first need to install the other version you may want to use.

Final thoughts

The benefit of install ruby using rbenv instead of using the one from the Ubuntu repositories is the flexibility you get, as each user can have its own version of ruby, and can switch from one to another if they need to.