How to Install Ruby on Rails on Ubuntu 20.04 LTS
This tutorial exists for these OS versions
On this page
- Step 1 - Install RVM Ruby Version Manager
- Step 2 - Install and Configure Ruby
- Step 3 - Install Nodejs and Yarn
- Step 4 - Update RubyGem Package Manager
- Step 5 - Install Ruby on Rails
- Step 6 - Install and Configure PostgreSQL
- Step 7 - Start New Ruby on Rails Project with PostgreSQL Database
- Step 8 - Create Simple CRUD with Rails Scaffold
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.
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 \
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.
Once all installation is completed, load the RVM to the system using the following command.
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.
You will get the latest version of the rvm Ruby Version manager.
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.
As a result, the installation of Ruby 2.7.1 using the rvm on Ubuntu 20.04 has been completed.
Step 3 - Install Nodejs and Yarn
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
Once all installation is completed, check the Nodejs version using the following command.
Now you will get the Nodejs 'v14.3.0' installed on your system.
Next, check the Yarn package manager 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.
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.
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.
You will get the Rails 6.0.3 version installed on your system.
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.
And you will get the role named 'hakase_dev' on the PostgreSQL list users.
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.
On the development section, add the database configuration as below.
Go to the testing section and add the PostgreSQL database configuration as below.
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.
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'.
Now open your web browser and type your server IP address with port '8080' on the address bar.
You will get the default index.html page of the Ruby on Rails.
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.
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'.
Create a new post and tytpe your title and body, and you will get the result as below.
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.