Installing Ruby on Rails 6 on Ubuntu [A How-To Guide]

Written by voscarmv@gmail.com | Published 2020/04/18
Tech Story Tags: rails-6 | ubuntu | yarn | nodejs | howto-install | microverse | how-to-install-ruby6-on-ubuntu | ruby-on-rails-top-story

TLDR In 5 easy steps, I’ll show you how to install Ruby on Ruby on Rails 6 on Ubuntu. This article is meant to save you a couple of hours. The Odin Project currently uses Ruby 2.6.5.3, but as Ruby 6 catches on, the older version is bound to fall out of use. Ruby has been known to cause warning messages when used with Ruby 6.0.2.0. Ruby is required to install the latest version of the Ruby version, but you can install 2.7.0 or 2.3.via the TL;DR App

Installing Rails 6 on Ubuntu can be tricky. In 5 easy steps, I’ll show you how to do it. I’m currently a web development student at microverse. As part of the microverse curriculum, we’re expected to solve some Rails exercises from The Odin Project.

I wrote this article as a way to help my fellow microverse students. This may also help anybody interested in learning Rails.
Currently, The Odin Project uses an older version of Rails (5.2.3). Installing this older Rails version is easy. But as Rails 6 catches on, the older version is bound to fall out of use.
It took me quite a while to figure out how to do this on my own. This article is meant to save you a couple of hours. That way you can get to work on the fun stuff sooner!
Anyway, here’s how I installed Rails 6:

Step 1. Prerequisites

I installed Rails 6 on Xubuntu 18.04.4 LTS running on Virtual Box 6.1.2, but this HOWTO will probably also work for later Ubuntu and Xubuntu versions.
You can follow the Virtual Machine installation guide from The Odin Project to set up your preferred Ubuntu distro. You can also install Ubuntu on a real machine if you prefer.

Step 2. Install rbenv and Ruby

The Ruby installation guide from The Odin Project currently uses Ruby 2.6.5. For Rails 6 we can install up to Ruby 2.7. However, Ruby 2.7 has been known to cause warning messages when used with Rails 6.

If you don’t really need the latest Ruby version, you can use 2.6.3 instead. This might save you a few warning messages later on as you work with rails.
Open your terminal, and run the following set of commands
$ sudo apt-get update
$ sudo apt-get install curl git nodejs gcc make libssl-dev libreadline-dev zlib1g-dev libsqlite3-dev g++ libpq-dev
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ echoexport PATH=”$HOME/.rbenv/bin:$PATH”’ >> ~/.bashrc
$ echoeval “$(rbenv init -)”’ >> ~/.bashrc
$ exit
These will install rbenv and close your terminal window. You will need to open a new terminal window for the PATH changes to take effect.
To install Ruby 2.7.0, open a new terminal, and enter these commands:
$ mkdir -p “$(rbenv root)”/plugins
$ git clone https://github.com/rbenv/ruby-build.git “$(rbenv root)”/plugins/ruby-build
$ rbenv install 2.7.0 --verbose
$ rbenv global 2.7.0
This is probably the most time-consuming part of the HOWTO, so be patient. Once Ruby is done installing, you’ll be ready for the next step.

Step 3. Install Rails

This step is pretty straight-forward. Instead of installing the older version of Rails, as detailed in The Odin Project Guide, we’ll simply install Rails 6.0.2.2:
$  gem install rails -v 6.0.2.2

Step 4. Required additional components

When I first tried to install Rails 6 on my virtual machine, I thought the previous step would be all there was to it. But I was wrong! At this point, if you try to run something like rails new my_first_rails_app you’ll get a series of error messages.
To fix this, you’ll have to install these recent versions of the following components:

Step 4.a. Nodejs

To install the nodejs version required by Ruby 6, run:
$ curl -sL https://deb.nodesource.com/setup_12.x |sudo -E bash -
$ sudo apt-get install -y nodejs
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

Step 4.b. Bundler and pg

Bundler will allow you to load the necessary dependencies from Gemfiles, which are used by Rails apps. Pg is a Ruby gem you’ll need to communicate with PostgreSQL.
To install these, do:
$ gem install bundler:2.1.4
$ gem install pg

Step 4.c. Yarn

You will need yarn to be able to run rails server and test your Rails 6 apps. To install the necessary version, run:
$ echo “deb https://dl.yarnpkg.com/debian/ stable main” | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt update && sudo apt install yarn
$ yarn install --check-files

Step 5. Test Rails

Finally, you’ll want to make sure everything works as intended. To do this, I suggest you clone my Toy App Github repository and run some tests on it. This repository contains one of the first Rails exercises we did from The Odin Project at microverse:
$ git clone https://github.com/voscarmv/toy_app
$ cd toy_app
$ bundle install
$ rails db:migrate
$ rails server
This should serve the Toy App homepage to http://localhost:3000/ Play around with it and see how it works.
NOTE: If you run into errors when running bundle install, try running yarn install --check-files again. That will likely fix the problem.

If you decided to use Ruby 2.7

You might be getting warning messages when running 
rails c
 or any other rails command. I suggest you point to the 6-0-stable branch of the rails 6.0.2.2 gem in your Gemfile.
Your Gemfile should look something like this:

Conclusion and acknowledgments

I hope this HOWTO helped you save some time. If you run into trouble while trying it out, leave a comment or contact me on Twitter. I’ll do my best to help you out!
Finally, a special thank you to my coding partner Alexis for helping me find most of the commands from Step 4. I wouldn’t have been able to finish this guide so soon without his help!

Written by voscarmv@gmail.com | Microverse student, aspiring web developer.
Published by HackerNoon on 2020/04/18