How to install Ruby in Ubuntu?

Ruby is a high-level, general-purpose, interpreted programming language. It is a dynamically typed language that supports multiple paradigms including procedural, functional, and object-oriented programming.

Ruby was originally designed and developed in the mid-1990s by Yukihiro “Matz” Matsumoto in Japan. It is the language behind the Ruby on Rails framework.

In this article, I will discuss different ways to install Ruby language in Ubuntu.

Prerequisites

For installing Ruby on Ubuntu you should have the following.

  • A system with the recent version of Ubuntu (we will use Ubuntu 20.04 LTS) installed on it.
  • Access to a user account with superuser privileges.

How to Install Ruby in Ubuntu

There are different ways using which you can install Ruby in Ubuntu.

Installing Ruby from Ubuntu repository

This is one of the easiest ways to install Ruby language in Ubuntu. The downside of this method is you get the default version of Ruby which means you may not have the updated software.

First, update the apt package repository using the given command –

sudo apt update

Next, use the given command to install the Ruby in Ubuntu –

sudo apt install ruby-full -y

Once it gets installed you can verify the Ruby installation by checking its version.

ruby --version

check ruby version in ubuntu

Installing Ruby using rbenv

The rbenv is a command-line tool using which you can easily switch between different versions of Ruby installed on a system. We can use ruby-build to install Ruby on our system it is available as a plugin for rbenv.

The ruby-build is a script that installs Ruby from its source. To use this you need to install the required libraries and compilers.

sudo apt install git curl autoconf bison build-essential \
    libssl-dev libyaml-dev libreadline6-dev zlib1g-dev \
    libncurses5-dev libffi-dev libgdbm6 libgdbm-dev libdb-dev

Next, use the given command to install rbenv using the rbenv installer script –

wget -q https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer -O- | bash

To start using rbenv add $HOME/.rbenv/bin to the PATH environment variable.

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

You check the installed version of rbenv by using –

rbenv -v

This will display the version of rbenv installed on your system.

Now use the given command to display the list of available Ruby versions for download.

rbenv install -l

Using rbenv you can choose a specific version and install it on your system.

For example –

rbenv install 3.0.2

To set this version default and global version, type –

rbenv global 3.0.2

Installing Ruby using RVM

Another way to install Ruby on a Ubuntu system is to use RVM (Ruby Version Manager). It is a command-line tool that allows you to easily install, manage, and work with multiple ruby environments.

Download and install that required dependencies –

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

Install the gnupg2 tool –

sudo apt install gnupg2

Next, install the GPG key by using –

gpg2 --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Use the given command to install RVM –

curl -sSL https://get.rvm.io | bash -s stable

To start using RVM load the required environment variables by using –

source ~/.rvm/scripts/rvm

Use the given command to see the list of all Ruby versions available –

rvm list known

You can install a specific version of Ruby by using RVM –

rvm install ruby-x.x.x

For example –

rvm install ruby-1.8.6

To make it default use –

rvm --default use ruby-x.x.x

If you want to switch to a different version of Ruby then use –

rvm use ruby-x.x.x

Conclusion

By using one of the given methods you can easily install Ruby on your Ubuntu system. Now if you have a query then write us in the comments below.

Leave a Comment