Installing Ruby on Rails on Ubuntu 20.04

Updated on December 1, 2021
Installing Ruby on Rails on Ubuntu 20.04 header image

Overview

Ruby on Rails or Rails is an open-source web framework written in Ruby. It enables developers to efficiently build server-side rendered web applications through a model-view-controller code structure.

Prerequisites

Install Dependencies

  1. Install build-essential.

     $ sudo apt-get install build-essential
  2. Install the developer dependencies.

     $ sudo apt-get install libssl-dev zlib1g-dev sqlite3 libsqlite3-dev
  3. Install Git and curl if not already installed.

     $ sudo apt-get install git curl

Install Nodejs & Yarn

Rails uses the Javascript runtime through Nodejs to build applications through its Asset Pipeline.

  1. Install the latest LTS version of Nodejs.

     $ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
     $ sudo apt-get install nodejs
  2. Verify successful Nodejs installation.

     $ node -v
     v16.xx.x
  3. Install Yarn.

     $ curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
     $ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
     $ sudo apt-get update && sudo apt-get install yarn
  4. Verify successful Yarn installation.

     $ yarn -v
     1.2x.xx

Install rbenv

rbenv is a Ruby version manager used to install and manage Ruby environments.

  1. Install rbenv.

     $ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash

    > This script automatically installs and updates rbenv.

  2. Add rbenv to the path.

     $ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
     $ echo 'eval "$(rbenv init - bash)"' >> ~/.bashrc
     $ exec $SHELL
  3. Test that rbenv is installed correctly.

     $ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-doctor | bash
  4. Confirm audit results.

     $ Checking for `rbenv' in PATH: /home/user/.rbenv/bin/rbenv
     $ Checking for rbenv shims in PATH: OK

    > Verify PATH is correct by replacing user with your user name.

Install Ruby

  1. List the latest stable versions of Ruby.

     $ rbenv install -l
  2. Install Ruby with the desired version.

    Keep in mind, Rails requires Ruby 2.5.0 or greater.

     $ rbenv install 3.0.2 -v
  3. Set global Ruby version.

     $ rbenv global 3.0.2
  4. Confirm installation with similar output.

     $ ruby -v
     ruby 3.0.2pxxx
  5. Install Bundler using gem.

     $ gem install bundler

Installing Rails

  1. Install Rails using gem.

     $ gem install rails
  2. Confirm successful installation.

     $ rails -v
     Rails 6.x.x.x

Creating a Test Application

  1. Create a new Rails project.

     $ cd ~
     $ rails new hello-world
     $ cd hello-world
  2. Start Rails server.

     $ rails server --binding=0.0.0.0
  3. Navigate to your server's IP address at port 3000 in a web browser. For example:

     $ http://192.0.2.123:3000

You should see a working Rails server response.