How To Set up Ruby on Rails on Ubuntu 20.04

In this guide, we want to teach you How To Set up Ruby on Rails on Ubuntu 20.04.

Ruby on Rails (sometimes RoR) is the most popular open-source web application framework. It’s built with the Ruby programming language.

You can use Rails to help you build applications, from simple to complex, there are no limits to what you can accomplish with Rails!

How To Set up Ruby on Rails on Ubuntu 20.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges. To do this, you can follow our guide the Initial Server Setup with Ubuntu 20.04.

In this tutorial, you will learn to install Ruby on Rails with RVM.

RVM, or Ruby Version Manager, is a command line tool that lets you manage and work with multiple Ruby development environments and allows you to switch between them.

Install RVM and Rails on Ubuntu 20.04

First, you need to update your local package index with the following command:

sudo apt update

Then, you need to install or update GPG to the most recent version in order to contact a public key server and request a key associated with the given ID:

sudo apt install gnupg2

Next, you must request the RVM project’s public key to verify the legitimacy of your download with the following command:

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

Now use the following command to download the RVM script installer and write it to a file named rvm.sh:

\curl -sSL https://get.rvm.io -o rvm.sh

When your download is completed, you can pipe it to bash to install the latest stable Rails version which will also pull in the associated latest stable release of Ruby by running the below command:

cat rvm.sh | bash -s stable --rails

When your download is completed, you will get the following command:

Output
35 gems installed

* To start using RVM you need to run `source /usr/local/rvm/scripts/rvm`
in all your open shell windows, in rare cases you need to reopen all shell windows.

* To start using rails you need to run `rails new <project_dir>`.

Next, source the RVM scripts with the following command:

source /usr/local/rvm/scripts/rvm

Install a Specific version of Ruby on Rails on Ubuntu 20.04

To install a specific version of ruby, you can use the RVM. First, check to see which versions of Ruby are available by listing them:

rvm list known

You will get the following output:

Output
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.0]
ruby-head

...

Then, install the specific version of Ruby that you need through RVM, replacing the highlighted version number with your version of choice, such as ruby-3.0.0 or just 3.0.0:

rvm install 3.0.0
Output
Already installed ruby-3.0.0.
To reinstall use:

rvm reinstall ruby-3.0.0

You can list available Ruby versions you have installed by typing:

rvm list
Output
=* ruby-3.0.0 [ x86_64 ]

# => - current
# =* - current && default
# * - default

Since Rails is a gem, which is a standardized format that contains Ruby programs, you can also install various versions of Rails by using the gem command.

List the valid versions of Rails with the following command on Ubuntu 20.04:

gem search '^rails$' --all
Output
*** REMOTE GEMS ***

rails (7.0.3.1, 7.0.3, 7.0.2.4, 7.0.2.3, 7.0.2.2, 7.0.2.1, 7.0.2, 7.0.1, 7.0.0, 6.1.6.1, 6.1.6, 6.1.5.1, 6.1.5, 6.1.4.7,...

Next, you can install your required version of Rails. Replace the highlighted version number with your version of choice, such as 7.0.3.1.

gem install rails -v 7.0.3.1
Output
Successfully installed rails-7.0.3.1
Parsing documentation for rails-7.0.3.1
Done installing documentation for rails after 0 seconds
1 gem installed

You can use various Rails versions with each Ruby by creating gemsets and then installing Rails within those using the normal gem commands.

To create a gemset you will use:

rvm gemset create gemset_name
Output
ruby-3.0.0 - #gemset created /usr/local/rvm/gems/ruby-3.0.0@gemset_orca
ruby-3.0.0 - #generating gemset_orca wrappers........

To specify a Ruby version to use when creating a gemset, use:

rvm 3.0.0@gemset_name --create
Output
Using /usr/local/rvm/gems/ruby-3.0.0 with gemset gemset_orca

The gemsets allow us to have self-contained environments for gems as well as multiple environments for each version of Ruby that you install.

For more information, you can visit the Ruby on Rails Guides page.

Conclusion

At this point, you learn to Set up Ruby on Rails with RVM on Ubuntu 20.04.

Hope you enjoy using it.

You may be interested in these articles:

Install and Configure Lighttpd on Ubuntu 20.04

Install Symfony PHP Framework on Ubuntu 20.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!