// Tutorial //

How To Install Ruby on Rails with RVM on Ubuntu 20.04

Published on February 16, 2022
Default avatar
How To Install Ruby on Rails with RVM on Ubuntu 20.04
Not using Ubuntu 20.04?Choose a different version or distribution.
Ubuntu 20.04

Introduction

Ruby on Rails is a popular web application framework designed to help you develop successful projects while writing less code.

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.

In this tutorial, you will install RVM, the stable release of Ruby on Rails (or the specific version of your choosing) via RVM, and Node.js as the JavaScript runtime required for some Rails features. You will also learn how to uninstall Rails at the end.

Prerequisites

  • You will need a Ubuntu 20.04 server instance with a non-root user configured with sudo privileges. Learn how to set this up by following our initial server setup guide.

When you are ready to continue, log in as your sudo user.

Step 1 – Installing RVM with the Latest Rails

First, you’ll need to install or update GPG (GNU Privacy Guard) to the most recent version in order to contact a public key server and request a key associated with the given ID:

  1. sudo apt update
  2. sudo apt install gnupg2

Next, you’ll request the RVM project’s public key to verify the legitimacy of your download:

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

You’ll use the curl command to download the RVM installation script from the project’s website:

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

The backslash that leads the command ensures that we are using the regular curl command and not any altered, aliased version. The -s flag indicates that the utility should operate in silent mode along with the -S flag to still allow curl to output errors if it fails. The -L flag tells the utility to follow redirects, and finally the -o flag indicates to write output to a file instead of standard output.

Once it is downloaded, if you would like to audit the contents of the script before applying it, run:

  1. nano rvm.sh

To close out of nano, hit CTRL-X.

Now, 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.

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

When the installation is complete, source the RVM scripts from the directory they were installed:

  1. source ~/.rvm/scripts/rvm

You should now have a full Ruby on Rails environment configured.

Step 2 – Installing and Using Specific Ruby or Rails Versions

If you need to install a specific version of Ruby for your application, rather than just the most recent one, you can do so with RVM. First, check to see which versions of Ruby are available by listing them:

  1. rvm list known

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:

  1. rvm install 3.0.0

After the installation, you can list the available Ruby versions you have installed by typing:

  1. rvm list

You can switch between the Ruby versions by typing:

  1. rvm use 3.0.0

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. Let’s first list the valid versions of Rails by doing a search:

  1. gem search '^rails$' --all

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

  1. gem install rails -v 7.0.2

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:

  1. rvm gemset create gemset_name

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

  1. rvm 3.0.0@gemset_name --create

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

Step 3 – Installing Node.js, a JavaScript Runtime (Optional)

Out of the box, Rails does not require Node.js since importmaps is now used by default. In practice, you may run into apps that still require it.

First, verify the Node.js script by outputting it to a file, then read it with nano:

  1. \curl -sSL https://deb.nodesource.com/setup_17.x -o nodejs.sh
  2. nano nodejs.sh

Once you are satisfied with the Node.js script, you can install the NodeSource Node.js v17.x repo:

  1. cat nodejs.sh | sudo -E bash -

The -E flag used here will preserve the user’s existing environment variables.

Now you can update apt and use it to install Node.js:

  1. sudo apt update
  2. sudo apt install nodejs

At this point, you can begin testing your Ruby on Rails installation and start to develop web applications.

Step 4 – Uninstalling RVM (Optional)

If you no longer wish to use RVM, you can uninstall it by first removing the script calls in your .bashrc file and then removing the RVM files.

First, remove the script calls with a text editor like nano:

  1. nano ~/.bashrc

Scroll down to where you see the RVM lines of your file:

~/.bashrc
...
# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"

Delete the lines, then save and close the file.

Finally, remove RVM with the following command:

  1. rm -rf ~/.rvm

Conclusion

You have covered the basics of how to install RVM and Ruby on Rails here so that you can use multiple Ruby environments.

For your next steps, you can learn more about working with RVM and how to use RVM to manage your Ruby installations.

If you’re new to Ruby, you can learn about programming in Ruby by following our How To Code in Ruby tutorial series.

For more scalability, centralization, and control in your Ruby on Rails application, you may want to use it with PostgreSQL rather than its default sqlite3 database.


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Default avatar
Tony Tran

author

Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!