Read in 4 minutes

last updated 

How To Install Ruby on Ubuntu 18.04

This tutorial is also available for:

centos 7

This tutorial covers how to install Ruby on a Ubuntu 18.04 system. Ruby is one of the most popular languages today. It has an elegant syntax and it is the language behind the Ruby on Rails framework.

We will show you three different ways to install Ruby on Ubuntu. Each has their own benefits, choose one of the installation method that will work best for you.

Although this tutorial is written for Ubuntu the same instructions apply for any Ubuntu based distribution, including Linux Mint and Elementary OS.

Prerequisites

Before starting with the tutorial, make sure you are logged in as a user with sudo privileges.

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:

sudo apt update
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

Next, run the following command to install both rbenv and ruby-build:

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

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.

If you are using Bash, type:

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

If you are using Zsh type:

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

Now what we have rbenv installed on our system we can easily install the latest stable version of Ruby and set it as as our default version with:

rbenv install 2.5.1
rbenv global 2.5.1

To list all available Ruby versions you can use: rbenv install -l

Verify that Ruby was properly installed by printing the version number:

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

Install Ruby using RVM

RVM is another tool for installing, managing and working with multiple Ruby environments.

First install the dependencies required for the RVM utility to build Ruby from source:

sudo apt update
sudo apt install curl g++, gcc, autoconf, automake, bison, libc6-dev, libffi-dev, libgdbm-dev, libncurses5-dev, libsqlite3-dev, libtool, libyaml-dev, make, pkg-config, sqlite3, zlib1g-dev, libgmp-dev, libreadline-dev, libssl-dev

Run the following commands to install RVM:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

To start using RVM you need to run the following command:

source ~/.rvm/scripts/rvm

Install the latest stable version of Ruby with RVM and set it as as the default version with:

rvm install 2.5.1
rvm use 2.5.1 --default

Verify that Ruby was properly installed by printing the version number:

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

For more information about how to manage your Ruby installations visit the RVM Documentation page.

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.

If you have any questions or feedback, feel free to comment below.