How to Install Ruby on Rails on Ubuntu 20.04

Ruby and Rails commonly known as "Rails" is an open-source web application framework. Ruby and Rails framework is written in Ruby programming language which helps you to create highly powerful websites and applications. Rails is a server-side web application framework that follows the MVC concept.

In the previous article, we discussed Ruby installation on Ubuntu 20.04. In this article, we are going to learn how to install the Ruby and Rails framework on Ubuntu 20.04.


  1. Ubuntu server 20.04
  2. A non-root user with Sudo privileges
  3. Internet connection to download packages

Install Ruby on Ubuntu

There are many ways to install Ruby on Ubuntu 20.04. In this article, we are going to use Rbenv. You will need another tool ruby-build to install Ruby.

Use the following command to update your Ubuntu 20.04.

$ sudo apt update

Use the following command to install Ruby dependency packages.

$ 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

After the complete installation of dependency packages, run the following command to clone both Rbenv and Ruby-build repositories at GitHub.

$ curl -sL curl -fsSL | bash

To set path in .bashrc, run the following command:

$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
$ echo 'eval "$(rbenv init -)"' >> ~/.bashrc
$ exec $SHELL

Now you can install any available version of Ruby using Rbenv. To list available Ruby version, run the following command:

$ rbenv install -l

You will get output as:

Listing available Ruby version

You can install any version of Ruby using Rbenv. In this example, I am going to install Ruby 3.0.0. To choose any version compatible with your system, replace version number.

$ rbenv install 3.0.0

After completing installation, run the following command to set global variable:

$ rbenv global 3.0.0

To verify installation, run the following command:

$ ruby -v

You will get output similar as:


ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-linux]

Run the following command to install bundler .

$ gem install bundler

Install NodeJS on Ubuntu 20.04

Rails requires Javascript runtime to build application so you need to install latest version of Node.js.

Run the following command to install NodeJS version 12 Repositories. If you want the latest version, you can install NodeJS version 14.

$ curl -sL | sudo -E bash -

Execute following command to install Nodejs.

$ sudo apt install -y nodejs

To check nodejs version, run the following command:

$ node -v

You will get the output as:



After installing nodejs, you need to install yarn package manager. Run the following command to add gpg key for yarn package manager.

$ curl -sL | sudo apt-key add -

Add repository for yarn package using following command:

$ echo "deb stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Finally run following command to install yarn package manager .

$ sudo apt-get update && sudo apt-get install yarn

Install Rails on Ubuntu

Rails can be installed using the gem install command. Execute the following command to install Rails on ubuntu 20.04.

$ gem install rails

You can use gem install rails –version=<version> command to install specific version of Rails. Use the following command to verify Rails installation on your system.

$ rails -v

As of writing this article the latest version of rails is 6.1.3.


Rails 6.1.3

Install PostgreSQL

In this article, we are going to use PostgreSQL to run the Rails application. In ubuntu 20.04, PostgreSQL is available in the default repository.

Run the following command to install Postgres and dependencies:

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

Once the installation is completed, start and enable postgres service to start automatically after reboot.

$ sudo systemctl start postgresql
$ sudo systemctl enable postgresql

Postgres service has been started and enabled. Now login to the PostgreSQL shell and create a new role with a password and the privileges createdb and login. In this example I am going to use 'linoxide_dev' as a role.

$ sudo -i -u postgres psql

To create role and grant permission run the following command:

$ create role linoxide_dev with createdb login password 'mystrongpassword';

To list all available users on PostgreSQL, run the following command:

$ \du

You will get the role named 'linoxide-dev' on the PostgreSQL list users.

Displaying user list in PostgreSQL

Start New Ruby on Rails Project with PostgreSQL

Ruby on Rails uses SQLite as the default database. In this article, we are going to start new Rails project using PostgreSQL database.

In this article, I am going to create a new application 'linoxideapp' with the default database PostgreSQL using the following 'rails' command.

$ rails new linoxideapp -d postgresql

You will get output similar as:

Creating new project using Ruby on Rails

Now you will get the project directory 'linoxideapp', go to the project directory, and modify the database configuration file 'config/database.yml' using any text editor.

$ cd linoxideapp/
$ vi config/database.yml

In the development section and use your database details. In this example, I have used default credentials created in above steps. You can have your own assumption.

   host: localhost
   port: 5432
   username: linoxide_dev
   password: mystrongpassword

Now go to the testing section and modify PostgreSQL database configuration as below:

   host: localhost
   port: 5432
   username: linoxide_dev
   password: mystrongpassword

Save the file and exit.

Now, run the rails command to generate and migrate database for our new Rails project.

$ rails db:setup
$ rails db: migrate

You will get output similar as:

DB setup and migrate for Project

Now its time to host application. Start the default rails web server using the following command:

$ rails s -b -p 8080

Your rails application will listen to the port '8080' for an incoming connection. The linoxideapp project will run with the port '8080'.

Running default ruby web server

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


You will get the default web page of the Ruby on Rails as:

Ruby on Rails default web page

The output shows the first project is running successfully.


In this article, you have learned how to install Ruby using Rbenv with application compatible version on Ubuntu 20.04. You got knowledge about PostgreSQL database installation and configuration. Finally, you got an idea on creating Rails project and hosting an application using PostgreSQL. Thank you for reading and feedbacks are appreciated.

3 Comments... add one

  1. Truly excellent. Complete and clear.
    Would only add that running the command to add the gpg key for yarn package manager will give a warning that using "add-key" is deprecated, but that does not mean the command was unsuccessful. Just ignore it and continue on.

    Note that if you wish to use sqlite3 as the database instead of PostgreSQL, you will probably need to do the following to get the 'C' language header files required by the Gem install (it compiles some of the files):
    sudo apt install sqlite-devel
    Or for mysql:
    sudo apt install libmysqlclient-dev

    Otherwise you will get errors when you run 'rails new' or any subsequent 'bundle install'.
    If you get errors, look all the way back in the listing of error messages to find any suggestions as to what you should do to fix them.


Leave a Comment