How to install Ruby on Rails on Ubuntu 20.04?

Overview

In this tutorial we will be installing Ruby, Gems, and, Rails which is an open-source framework written in Ruby that is why it is called Ruby on Rails. We will be using rbenv to install them, rbenv is a version manager tool for the Ruby programming language on Unix-like systems.

Prerequisites

There are certain prerequisites that need to be met before you begin.

  • An Ubuntu 20.04 machine

  • Knowledge of CLI

  • A user with root access

Get Started

Step 1 - Installing Dependencies and rbenv

i. Update your packages using the below command:

sudo apt update

ii. Install dependencies necessary for Ruby installation.

sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev

iii. Install rbenv using the following command:

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

iv. To utilise the rbenv command line tool, add /.rbenv/bin to your $PATH environment variable.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

v. To make rbenv load automatically, add the command eval "$(rbenv init -)" to your /.bashrc file:

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

vi. In your current shell session in your ~/.bashrc file, implement changes made, click apply:

source ~/.bashrc

vii. Verify that rbenv is set up by using the following command:

type rbenv

Step 2 - Install Ruby using ruby build

i. List the various Ruby versions first.:

rbenv install -l

ii. Now, let’s install latest version of Ruby:

rbenv install 3.2.1

iii. By using global sub-command, make it the standard version of Ruby.:

rbenv global 3.2.1

iv. Check Ruby's version number to ensure that it was installed correctly:

ruby -v

Step 3 - Installing Gems

i. Gems are distributed Ruby libraries. Gem commands are also used to install Ruby.

echo "gem: --no-document" > ~/.gemrc

ii. Install Bundler to manage gem dependencies for your Ruby projects.

gem install bundler

iii. To confirm the path where gem has been installed, type the following command:

gem env home

Step 4 - Installing Rails

i. Install Rails using the following command:

gem install rails -v 6.1.4.1

ii. Run the following command, whenever installing a new version of Ruby:

rbenv rehash

iii. Verify that Rails has been installed properly by checking its version, with the following command:

rails -v

Conclusion

In this tutorial, we installed and configured the entire Ruby and Rails framework using rbenv and gem. Now you may start working on your Ruby framework projects.

Last updated