Ruby on Rails (RoR) is an open-source web application framework, published under the MIT License. It is a server-side web application framework that follows the MVC (Model-View-Controller) concept.

Rails are providing default structures for the database, web service, and web pages. More than 3000 developers have contributed code to the Rails framework and there are many well-known applications based on Rails, such as Github, Airbnb, Soundcloud, etc.

In this tutorial, we will show you how to install Ruby on Rails on the Latest Ubuntu 20.04 FocalFossa. This guide will cover some topics, including the installation of RVM Ruby Version Manager, PostgreSQL Database Server, and guide to start a new project with Ruby on Rails.

Prerequisites

For this guide, we will use the Ubuntu 20.04 FocalFossa with 2GB of RAM, 25GB free disk space, and 2 CPUs. Also, you must have the root privileges.

What we will do?

  • Install RVM Ruby Version Manager
  • Install and Configure Ruby
  • Install Nodejs and Yarn
  • Update RubyGem Package Manager
  • Install Ruby on Rails Latest Version
  • Install and Configure PostgreSQL Database
  • Start First Ruby on Rails Project with PostgreSQL Database
  • Create Simple CRUD with Ruby on Rails

Step 1 – Install RVM Ruby Version Manager

RVM or Ruby Version Manager is a command-line tool based on Bash and Ruby to manage the ruby installation. RVM allows you to install and configure multiple ruby versions on one system.

in this first step, we will install the RVM using the official installer script.

Import GPG keys of the RVM using the gpg command below.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 

7D2BAF1CF37B13E2069D6956105BD0E739499BDB

Download the RVM installer script and install the RVM.

curl -sSL https://get.rvm.io | bash -s stable --ruby

The command will automatically install packages required, and install the Ruby 2.7 version.

<img alt="Install RVM Ruby Version Manager" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/1.png62a2e9f3609ab.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="356" loading="lazy" src="data:image/svg xml,” width=”750″>

Once all installation is completed, load the RVM to the system using the following command.

source /usr/local/rvm/scripts/rvm

As a result, you can use the “rvm” command now.

Next, update the RVM to the latest stable version and add the root user to the rvm group.

rvm get stable --autolibs=enable

usermod -a -G rvm root

After that, check the rvm version using the command below.

rvm version

You will get the latest version of the rvm Ruby Version manager.

<img alt="" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/2.png62a2e9f3adf6d.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="726" loading="lazy" src="data:image/svg xml,” width=”750″>

As a result, the installation of the RVM Ruby Version Manager on Ubuntu 20.04 has been completed.

Step 2 – Install and Configure Ruby

For this tutorial, we will install the latest Ruby version for the Ruby on Rails installation.

Install Ruby 2.7.1 using the rvm command below.

rvm install ruby-2.7.1

Once all installation is completed, make the Ruby 2.7.1 as the default Ruby version on your system.

rvm --default use ruby-2.7.1

Now check the Ruby version using the command below.

ruby --version

As a result, the installation of Ruby 2.7.1 using the rvm on Ubuntu 20.04 has been completed.

<img alt="Install Ruby with RVM" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/3.png62a2e9f403bc3.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="446" loading="lazy" src="data:image/svg xml,” width=”750″>

Step 3 – Install Nodejs and Yarn

For the Ruby on Rails, we need the JavaScript Runtime for compiling Ruby on the Rails asset pipeline. And for this guide, we will use the Nodejs and Yarn package manager, which can be installed from the Nodesource repository.

First, install some packages dependencies using the following command.

sudo apt install gcc g   make

Now add the Nodejs Nodesource repository.

curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -

Then add the GPG key and repository of the Yarn package manager.

curl -sL 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

After that, update all available repositories on your system and install the Nodejs and Yarn package manager using the apt command below.

sudo apt update

sudo apt install yarn nodejs

<img alt="Install Nodejs and Yarn Package Manager" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/4.png62a2e9f436bbf.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="332" loading="lazy" src="data:image/svg xml,” width=”750″>

Once all installation is completed, check the Nodejs version using the following command.

node --version

Now you will get the Nodejs ‘v14.3.0’ installed on your system.

Next, check the Yarn package manager version.

yarn --version

And you will get the Yarn ‘1.22.4’ installed on the system.

As a result, the installation of Nodejs and Yarn package manager for Ruby on Rails has been completed.

Step 4 – Update RubyGem Package Manager

RubyGems is a Ruby Package Manager, coming with the gem command-line tool. It’s automatically installed when we install Ruby on the system.

To update the RubyGem to the latest version, run the following command.

gem update --system

You will get the successful message as below.

Updating rubygems-update

Fetching rubygems-update-3.1.3.gem

Successfully installed rubygems-update-3.1.3

...

RubyGems system software updated

Next, create a new configuration for the RubyGem ‘~/.gemrc’ and disable the installation of package documentation using the command below.

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

After that, check the gem version using the gem command below.

gem -v

As a result, you will get the RubyGem updated to the latest version ‘3.1.3’.

Step 5 – Install Ruby on Rails

In this step, we will install the latest version of Ruby on Rails through the RubyGem package manager.

Install Ruby on Rails using the gem command below.

gem install rails

Once all installation is completed, check the Ruby on Rails version.

rails --version

You will get the Rails 6.0.3 version installed on your system.

<img alt="Install Ruby on Rails using Gem" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/5.png62a2e9f49c333.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="231" loading="lazy" src="data:image/svg xml,” width=”750″>

Step 6 – Install and Configure PostgreSQL

For this tutorial, we will use PostgreSQL as the database for our Rails project. And in this step, we will install the PostgreSQL database server provided by the official ubuntu repository.

Install the PostgreSQL database server to the Ubuntu 20.04 using the apt command below.

sudo apt install postgresql postgresql-contrib libpq-dev -y

Once all installation is completed, start the PostgreSQL service and add it to the system boot.

systemctl start postgresql

systemctl enable postgresql

The PostgreSQL service is up and running.

Next, log in to the PostgreSQL shell and create a new role ‘hakase_dev’ with the password ‘hakasepass’ and the privileges ‘createdb’ and ‘login’.

sudo -i -u postgres psql

create role hakase_dev with createdb login password 'hakasepass';

Now list all available of users on the PostgreSQL using the following query.

du

And you will get the role named ‘hakase_dev’ on the PostgreSQL list users.

<img alt="Display user ejabberd" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/6.png62a2e9f518e61.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="306" loading="lazy" src="data:image/svg xml,” width=”750″>

Step 7 – Start New Ruby on Rails Project with PostgreSQL Database

By default, the Ruby on Rails used the SQLite as the default database. And for this guide, we will start a new Rails project using the PostgreSQL database.

Create a new project ‘hakaseapp’ with the default database PostgreSQL using the following ‘rails’ command.

rails new hakaseapp -d postgresql

Now you will get the project directory ‘hakaseapp’, go to the project directory and edit the database confoiguration ‘config/database.yml’ using vim editor.

cd hakaseapp/

vim config/database.yml

On the development section, add the database configuration as below.

  host: localhost

  port: 5432

  username: hakase_dev

  password: hakasepass

Go to the testing section and add the PostgreSQL database configuration as below.

  host: localhost

  port: 5432

  username: hakase_dev

  password: hakasepass

Save and close.

Next, run the rails command below to generate and migrate the database for our Rails project, and make sure there is no error.

rails db:setup

rails db:migrate

<img alt="Ruby on Rails DB Migrate" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/7.png62a2e9f58ab4b.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="250" loading="lazy" src="data:image/svg xml,” width=”750″>

When all setup is complete, start the default puma rails web server using the command below.

rails s -b 0.0.0.0 -p 8080

The ‘hakaseapp’ will run on your public IP address with the port ‘8080’.

<img alt="Run Ruby on Rails Project" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/9.png62a2e9f615916.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="218" loading="lazy" src="data:image/svg xml,” width=”750″>

Now open your web browser and type your server IP address with port ‘8080’ on the address bar.

http://10.5.5.32:8080/

You will get the default index.html page of the Ruby on Rails.

<img alt="Ruby on Rails default index.html" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/8.png62a2e9f67a52a.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="306" loading="lazy" src="data:image/svg xml,” width=”750″>

As a result, The first project of Ruby on Rails has been created.

Step 8 – Create Simple CRUD with Rails Scaffold

In this step, we will create a new simple CRUD application using the Ruby on Rails and the PostgreSQL database.

Generate the simple CRUD application using the ruby scaffold command as below.

rails g scaffold Post title:string body:text

After that, migrate the database uisng the following command.

rake db:migrate

<img alt="Create Simple CRUD with Rails Scaffold" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/10.png62a2e9f6e03b2.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="750" loading="lazy" src="data:image/svg xml,” width=”743″>

Make sure there is no error, then run the puma server again.

rails s -b 0.0.0.0 -p 8080

Back to your web browser and type the server IP address with port ‘8080’ following by the path ‘/posts’.

http://10.5.5.32:8080/posts

Create a new post and tytpe your title and body, and you will get the result as below.

<img alt="Ruby on Rails Simple CRUD" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/06/echo/11.png62a2e9f74694e.jpg" ezimgfmt="rs rscb5 src ng ngcb5" height="225" loading="lazy" src="data:image/svg xml,” width=”750″>

As a result, the simple CRUD application with the PostgreSQL database has been created using the Rails scaffold.

Finally, the installation and configuration of Ruby on Rails with PostgreSQL database on the Ubuntu 20.04 has been completed successfully.