How to Install Ruby 3 on Ubuntu 20.04

Ruby is an open-source, object-oriented, and general-purpose programming language which is one of the most popular programming languages. Ruby programming language is the reason behind the powerful Ruby and Rails framework.

There are multiple ruby managers available to install Ruby. Ruby managers allow to use multiple versions and help to switch between ruby versions. The most commonly used ruby managers are rbenv and rvm. Ruby is also available in the Ubuntu repository.

In this article we will be learning three different ways to install Ruby 3 on Ubuntu 20.04.

Pre-Requisites

  1. Ubuntu 20.04 installed system
  2. Sudo privileges account
  3. Proper internet connection to download applications.

Method 1- Install Ruby Using Rbenv

Many Rubyists in the community prefer installing ruby using rbenv. Rbenv is a tool that can be used to switch between different versions of Ruby. However, this tool can not be used to install Ruby.

To install Ruby, you will need another tool ruby-build. Use the following command to update Ubuntu 20.04 repository to the latest index.

$ sudo apt update

Run the following command to install dependencies.

$ 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

After installing dependencies, run the following command to clone both Rbenv and Ruby-build repositories at Github.

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

Now, run the following commands to set PATH in .bashrc:

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

Now you can install any available version of Ruby using Rbenv. Run the following command to list available versions.

$ rbenv install -l

You will get the output as:

Listing Available Ruby versions

You can install the version you want by running the following command. For this article, I am installing Ruby 3.0.0.

$ rbenv install 3.0.0

To set the global variable run the following command:

$ rbenv global 3.0.0

Replace the version number with your environment-compatible version.

To verify the installed Ruby version, run the following command:

$ ruby -v

You will get output similar as:

output:

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

Method 2- Installing Ruby from Ubuntu Repositories

The simplest and easiest way to install Ruby is using Ubuntu's built-in apt package manager. The Ruby version installed using the default apt package manager is usually the latest one.

Run the following command to update ubuntu repositories to latest index.

$ sudo apt update

Use following command to install Ruby.

$ sudo apt install ruby-full

After completing installation, run the following command to verify if Ruby is installed:

$ ruby --version

You will get the output similar as:

output:

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

The output verifies the installation of Ruby version 3.0.0

Method 3 - Installing Ruby using RVM

Another tool for installing and managing Ruby on Ubuntu and other Linux system is RVM.

To install RVM on ubuntu 20.04 , follow the below steps:

Update the Ubuntu Repositories to latest index using following command:

$ sudo apt update

To install RVM dependencies, run the following command:

$ 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

Now run the following command to install RVM:

$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

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

Run the following command to activate RVM:

$ source ~/.rvm/scripts/rvm

Now you can install Ruby using following command:

$ rvm install 3.0.0

You can replace version number with your compatible version

To use installed Ruby as default, run the following command:

$ rvm use 3.0.0 --default

To verify your installation, run the following command:

$ ruby -v

You will get output in your screen similar as:

output:

ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

From the output the installed Ruby version is 3.0.0

Conclusion

In this article, you learned different methods to install Ruby 3 on Ubuntu 20.04. Also, you learned how to verify your Ruby installation and how to install different versions compatible with your environment.

Leave a Comment