Install Ruby on Ubuntu



Ruby is one of the most popular programming languages, known for its stability and simplicity. It has been powering applications for a long time.

You may have heard of Ruby on Rails, one of the most well-known web frameworks used to build web applications. It's used by large companies like GitHub, Shopify, and others. It's a powerful and developer friendly framework, and it is built using Ruby.

In this tutorial, we will learn how to install and use Ruby on Ubuntu. There are several ways to get Ruby set up on an Ubuntu machine, and you can choose the right solution based on your needs.

Before getting started, ensure you have an updated system and root access to proceed with the installation.

Install Ruby Using APT

One of the simplest ways to install packages on Ubuntu is by using the apt package manager. However, this approach does not always provide the latest version. First, check the version available in the repository using the command ?

apt show ruby

This will display information about the Ruby package on Ubuntu. If you only need the version, you can use the command ?

apt-cache policy ruby

Using these commands helps us get information about the available versions in the repository before proceeding with the installation. This way, we avoid installing a version we don't need and having to remove it to install another one. This approach lets us gather information first and decide whether to continue with the installation.

As usual, first update your system using the following command ?

sudo apt update

Next, you are ready to install Ruby. Use the following command ?

sudo apt install ruby

This will download and install the Ruby language on your machine with the version 3.2.

Now, you are ready to use Ruby. You can get the Ruby version installed on your system using the command ?

ruby --version 

This will display the Ruby version installed, which may change as new versions and bug fixes are released.

Again, the installation method depends on your environment and the projects you are working on.

Install Ruby Using RVM

At the time of writing this guide, the latest version of Ruby is 3.3.5, while the Ubuntu repository offers version 3.2. If you use the apt command, you'll get this older version, which is fine unless you specifically need the latest version.

RVM, or Ruby Version Manager, is a tool that allows you to install any Ruby version of your choice. It also enables you to install and switch between multiple versions as needed.

Generally, Ruby developers use RVM instead of a standard installation because it provides more flexibility for a Ruby environment, such as managing gems to resolve dependency conflicts between Ruby versions.

To use RVM on Ubuntu, you first need to install it, as it does not come preinstalled by default. First, update your system ?

sudo apt update

Then, install the GPG keys, which help secure and encrypt data and verify signatures ?

gpg2 --keyserver keyserver.ubuntu.com --recv-keys 
409B6B1796C275462A1703113804BB82D39DC0E3 
7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Then install RVM using the command ?

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

This should install RVM on your Ubuntu machine. Close the terminal and reopen it again, and you are ready to use RVM.

To install a Ruby version, use the command:

rvm install 3.3.5

Here, the version is 3.3.5; you can choose the right version for your needs. In case you need the latest version, simply run the command ?

rvm install ruby -latest

To set an installed Ruby version as the default, use the command ?

rvm use 3.3.5 -default

Here, we make version 3.3.5 the default version.

Install Ruby using rbenv

RVM itself is a large tool, and its goal is not just to install Ruby but also to manage gemsets, which involves making changes to the shell environment. This is useful if you want to manage dependencies for different Ruby versions, but it may be more than you need if you only want to install multiple versions without focusing heavily on dependency management. For this, a solution like rbenv is a lightweight command line tool that helps manage multiple Ruby versions without gem management, as it only relies on the installation.

rbenv is a small and powerful tool that eliminates complexity and heavy work behind the scenes. It also has plugin support to add more functionality and capability if needed.

Like RVM, rbenv is not installed by default on Ubuntu, so you should first install it using this command:

sudo apt install rbenv

This will install all dependencies needed for the tool to work.

After the installation is complete, set rbenv to load in the shell using this command

rbenv init

Close your terminal, reopen it, and you will be ready to install Ruby.

After this, installing Ruby is straightforward. If you need the stable version, use the command ?

rbenv install -l

To install a specific version, use the install command with the specific version, like this ?

rbenv install 3.3.5

To get a list of installed versions, use this command ?

rbenv install -L

Install Ruby using ASDF

ASDF is a tool that does almost the same as RVM, except it is more powerful and can be used for all other programming languages, not just Ruby.

Using one tool to manage all your environments for programming languages and tools is definitely a better option than using a separate tool for each programming language or developer tool.

ASDF is simple to install and use on Ubuntu. First, install it using the command ?

git clone https://github.com/asdf-vm/asdf.git ~/.asdf --branch v0.12.0
echo '. $HOME/.asdf/asdf.sh' >> ~/.bashrc
source ~/.bashrc

To install Ruby using ASDF, use the command ?

asdf plugin add ruby https://github.com/asdf-vm/asdf-ruby.git

ASDF uses plugins to manage environments; here, we add the Ruby plugin.

Then, you can list all Ruby versions that are available using the command ?

 asdf list-all ruby

You can choose the version based on your needs and install it using the command ?

 asdf install ruby 3.3.5

In this example, we install version 3.3.5.

ASDF allows you to specify the version for every project. In case you are a Ruby developer with projects that each use a specific version, ASDF lets you use a specific version for each project.

Conclusion

Ruby is a fantastic and powerful language that many developers use. Even if you are not interested in Ruby itself, you may still need a Ruby environment to run some Linux tools that are based on Ruby.

In this tutorial, we covered different ways to install Ruby on Ubuntu. We showed almost all the methods available to install and use Ruby, and depending on your choice and needs, you can decide which method to use.

Updated on: 2024-11-21T11:59:00+05:30

95 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements