How to Install the Latest Version of Ruby in Ubuntu 22.04

This is best way to install Ruby.

Introduction

Naturally, Rubies are one of the four precious gemstones and the name comes from the Latin word rubens meaning red. Rubies are beautiful, hard and durable - second only to moissanite and diamonds on the Mohs Scale of Hardness.

In the programming world, Ruby is a multi-paradigm programming language. It is interpreted, high-level, and general-purpose. The development of the language was done by Yukihiro Matsumoto in Japan during the 1990s.

This tutorial will guide you on how to install the latest version of Ruby. If you are developing Ruby applications and working in multiple Ruby environments, this is the preferred way to go about it.

What is RVM ?

Ruby Version Manager(RVM) is a command-line tool that allows you to easily install, manage, and work with multiple Ruby environments.

It installs different versions of Ruby locally for the user and updates environment variables for Ruby and gems based on which version you decide to use.

RubyGems i.e. gems, are libraries that you can install and use in every project on your server.

If you make some code that you like to share, you can make a gem or plugin of it. Then publish it on version control platforms such as GitHub.com

RVM supports most UNIX-like systems and Windows (with Cygwin or Bash on Ubuntu on Windows). The basic requirements are bash, curl, gpg2 and overall GNU version of tools - but RVM tries to autodetect it and install anything that is needed.

Installing the most stable Ruby version

Install the dependencies required 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 add the GPG keys and install RVM:

curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -sSL https://rvm.io/pkuczynski.asc | gpg --import -
curl -sSL https://get.rvm.io | bash -s stable

To start using RVM, load the script environment variables using the source command:

source ~/.rvm/scripts/rvm

To get a list of all Ruby versions that can be installed with this tool, type:

rvm list known

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

rvm install ruby
rvm --default use ruby

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

ruby -v
# OUTPUT (2nd SEPTEMBER 2022)
ruby 3.0.2p107 (2021-07-07 revision 0db68f0233) [x86_64-linux-gnu]

From the Output:

  • Ruby Version: 3.0.2p107

  • Release Date: July 7, 2021

  • Architecture: x86_64-linux-gnu

This information tells us that Ruby version 3.0.2 is installed on a 64-bit Linux system.

Installing a specific Ruby Version

If you want to install a specific version of Ruby, enter the commands below:

Get a list of all Ruby versions that can be installed with RVM:

rvm list known

Replace x.x.x with the Ruby version you want to install:

rvm install ruby-x.x.x

Make the installed Ruby version default:

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

To switch to another version without setting it as your default Ruby, enter:

rvm use ruby-x.x.x

Conclusion

The RVM scripts give you more flexibility to use different Ruby versions on a per-user basis.