Tutorial

How To Install Ruby on Rails with RVM on Ubuntu 18.04

How To Install Ruby on Rails with RVM on Ubuntu 18.04
Not using Ubuntu 18.04?Choose a different version or distribution.
Ubuntu 18.04

Introduction

A popular web application framework, Ruby on Rails was designed to help you develop successful projects while writing less code. With an aim to making web development fun and supported by a robust community, Ruby on Rails is open-source software that is free to use and welcomes contributions to make it better.

The command-line tool RVM (Ruby Version Manager) provides you with a solid development environment. RVM will let you manage and work with multiple Ruby environments and allow you to switch between them. The project repository is located in a git repository.

This tutorial will take you through the Ruby and Rails installation process and set up via RVM

Prerequisites

This tutorial will take you through the Ruby on Rails installation process via RVM. To follow this tutorial, you need a non-root user with sudo privileges on an Ubuntu 18.04 server.

To learn how to achieve this setup, follow our manual initial server setup guide or run our automated script.

Installation

The quickest way of installing Ruby on Rails with RVM is to run the following commands.

We first need to update GPG, which stands for 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 install gnupg2

We are using a user with sudo privileges to update here, but the rest of the commands can be done by a regular user.

Now, we’ll be requesting the RVM project’s key to sign each RVM release. Having the RVM project’s public key allows us to verify the legitimacy of the RVM release we will be downloading, which is signed with the matching private key.

  1. gpg2 --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Let’s now move into a writable location such as the /tmp directory and then download the RVM script into a file:

  1. cd /tmp

We’ll use the curl command to download the RVM installation script from the project’s website. The backslash that leads the command ensures that we are using the regular curl command and not any altered, aliased version.

We will append the -s flag to indicate that the utility should operate in silent mode along with the -S flag to override some of this to 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.

Putting all of these elements together, our full command will look like this:

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

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

  1. less /tmp/rvm.sh

Then we can [pipe] (https://www.digitalocean.com/community/tutorials/an-introduction-to-linux-i-o-redirection#pipes) it to bash to install the latest stable Rails version which will also pull in the associated latest stable release of Ruby.

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

During the installation process, you may be prompted for your regular user’s password. When the installation is complete, source the RVM scripts from the directory they were installed, which will typically be in your home/username directory.

  1. source /home/sammy/.rvm/scripts/rvm

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

Installing Specific Ruby and 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, where ruby_version can be typed as ruby-2.4.0, for instance, or just 2.4.0:

  1. rvm install ruby_version

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

  1. rvm list

We can switch between the Ruby versions by typing:

  1. rvm use ruby_version

Since Rails is a gem, we 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, we can install our required version of Rails. Note that rails_version will only refer to the version number, as in 5.1.6.

  1. gem install rails -v rails_version

We 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 we will use:

  1. rvm gemset create gemset_name

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

  1. rvm ruby_version@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 we install.

Install JavaScript Runtime

A few Rails features, such as the Asset Pipeline, depend on a JavaScript Runtime. We will install Node.js with the package manager apt to provide this functionality.

Like we did with the RVM script, we can move to a writable directory, verify the Node.js script by outputting it to a file, then read it with less:

  1. cd /tmp
  2. \curl -sSL https://deb.nodesource.com/setup_10.x -o nodejs.sh
  3. less nodejs.sh

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

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

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

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

  1. sudo apt update
  2. sudo apt install -y nodejs

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

How To Uninstall RVM

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.

Next, remove RVM with the following command:

  1. rm -rf ~/.rvm

At this point, you no longer have an

Conclusion

We 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] (https://www.digitalocean.com/community/tutorials/how-to-use-rvm-to-manage-ruby-installations-and-environments-on-a-vps).

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] (https://www.digitalocean.com/community/tutorials/how-to-use-postgresql-with-your-ruby-on-rails-application-on-ubuntu-14-04) or [MySQL] (https://www.digitalocean.com/community/tutorials/how-to-use-mysql-with-your-ruby-on-rails-application-on-ubuntu-14-04) rather than its default sqlite3 database. As your needs grow, you can also learn how to [scale Ruby on Rails applications across multiple servers] (https://www.digitalocean.com/community/tutorials/how-to-scale-ruby-on-rails-applications-across-multiple-droplets-part-1).

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
3 Comments


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!

Hi,

When doing this: sudo apt install gnupg2

i got this error: Package gnupg2 is not available, but is referred to by another package. This may mean that the package is missing, has been obsoleted, or is only available from another source

and was solved by updating: apt-get update

New computer, time to go back to this tutorial lol. Awesome tutorial! thanks!

Ubuntu has a dedicated RVM package actually. Follow the package instructions on github to install it.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.