How To Set Up Ruby on Rails on Ubuntu 20.04 | Easy RVM

Posted on

How To Set Up Ruby on Rails on Ubuntu 20.04 | Easy RVM

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! Now, proceed to the guide steps on the Orcacore website to install and configure 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 on 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.

1. Install RVM and Rails on Ubuntu 20.04

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

sudo apt update && sudo apt upgrade -y

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

sudo apt install gnupg2 -y

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

sudo 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:

sudo 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 command below:

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

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

install the latest stable Rails version

Next, source the RVM scripts with the following command:

source /usr/local/rvm/scripts/rvm

2. Install a Specific Version of Ruby 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:

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
**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 <mark>gemset_name</mark>
**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 <mark>3.0.0</mark>@<mark>gemset_name</mark> --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. RVM simplifies managing Ruby versions, gemsets, and dependencies, making development smoother and more organized. Once installed, you’re ready to build powerful web applications with the full Rails stack in a clean, isolated setup. It’s a solid foundation for any Rails project on Ubuntu.

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

Set up Cockpit on Ubuntu 20.04

Install MySQL and MySQL Workbench on Ubuntu 20.04

Alternative Solutions for Setting Up Ruby on Rails on Ubuntu 20.04

While RVM is a popular choice for managing Ruby versions, there are other valid methods to achieve the same goal of setting up Ruby on Rails on Ubuntu 20.04. Two alternatives are presented below.

1. Using asdf Version Manager

asdf is a versatile version manager that can handle multiple languages and tools, including Ruby. It offers a unified interface for managing different versions of Ruby, Node.js, Python, and more. Here’s how you can use asdf to install Ruby and Rails on Ubuntu 20.04. This is a good alternative for How To Set up Ruby on Rails on Ubuntu 20.04

Installation:

  1. Install dependencies:

    sudo apt update
    sudo apt install -y git curl
  2. Install asdf:

    git clone https://github.com/asdf-vm/asdf.git ~/.asdf
    
    echo '. ~/.asdf/asdf.sh' >> ~/.bashrc
    echo 'alias asdf_sourceall="source ~/.asdf/asdf.sh"' >> ~/.bashrc
    source ~/.bashrc
  3. Add asdf plugins:

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

Usage:

  1. Install Ruby:

    asdf list all ruby # List available ruby versions
    asdf install ruby 3.0.0 # Install a specific version
    asdf global ruby 3.0.0 # Set the global ruby version
  2. Install Rails:

    gem install rails -v 7.0.3.1
  3. Verify Installation:

    ruby -v
    rails -v

Explanation:

  • asdf provides a clean way to manage multiple language versions, avoiding potential conflicts.
  • It uses .tool-versions files to specify the required versions of different tools for a project, making it easier to collaborate with others.
  • Compared to RVM, asdf can manage a wider range of languages and tools, making it a more versatile option for polyglot projects.

2. Using Ubuntu’s System Packages (Not Recommended for Development)

While not recommended for development due to potential version limitations and system-wide changes, you can technically install Ruby and Rails using Ubuntu’s package manager (apt). This approach is primarily suitable for very basic use cases or when specific system dependencies require a particular Ruby version.

Installation:

sudo apt update
sudo apt install ruby-full rails

Explanation:

  • This method installs Ruby and Rails from the official Ubuntu repositories.
  • The versions available through apt might not be the latest, and upgrading can be complex.
  • Installing Rails directly through apt can sometimes lead to dependency issues. Therefore, it’s generally preferable to use a version manager like RVM or asdf for more flexibility and control over the environment.
  • This method is the least recommended way to set up Ruby on Rails on Ubuntu 20.04, as it can lead to dependency and versioning issues in the future.

Why Version Managers are Preferred:

Version managers like RVM and asdf provide several advantages over using system packages for Ruby and Rails development:

  • Isolation: They allow you to install multiple Ruby versions side-by-side without conflicts.
  • Project-Specific Versions: You can specify different Ruby and Rails versions for each project, ensuring compatibility and avoiding breaking changes.
  • Easy Updates: Upgrading Ruby and Rails versions is straightforward and doesn’t affect the system-wide installation.
  • Gemset Management: RVM and asdf facilitate the creation of gemsets, isolating gems for each project to avoid dependency clashes.

In conclusion, while Ubuntu’s package manager provides a rudimentary way to install Ruby and Rails, using a version manager like RVM or asdf is highly recommended for a robust and flexible development environment when you set up Ruby on Rails on Ubuntu 20.04.