There is a new version of this tutorial available for Ubuntu 22.04 (Jammy Jellyfish).

How to Install Ruby on Rails (RoR) with PostgreSQL on Ubuntu 16.04

Ruby on Rails (RoR) is an open source web application framework, published under MIT License. Ruby on Rails, or rails is written in Ruby, it is a server-side web application framework that follows the MVC (Model-View-Controller) concept. Rails is providing default structures for the database, web service, and web pages. More than 3000 developers have been 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, I will show you how to install Ruby on Rails on ubuntu 16.04 LTS. I will show you how to install and configure Rails with a PostgreSQL database, and how to create a new first project with Rails.

Prerequisite

  • Ubuntu 16.04 Server.
  • Root privileges.

Step 1 - Install RVM

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 step, we will install the stable RVM version and add the repository key with the gpg command.

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

Install RVM stable. Make sure curl command is installed on your system.

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

That command will install the requires packages for the RVM installation, then download the latest stable RVM version and install it.

Now we can start using RVM by executing the command below at the first:

source /usr/local/rvm/scripts/rvm

Install Ruby Version Manager (RVM)

Step 2 - Install Ruby

The current stable version of Ruby is 2.3.1. Install it with the rvm command and then make it the default ruby version for your system.

Update rvm to the latest stable version, then install Ruby 2.3.1 version.

rvm get stable --autolibs=enable
rvm install ruby-2.3.1

Next, make 2.3.1 the default ruby version on the system.

rvm --default use ruby-2.3.1

Now check the ruby version with command below:

ruby -v

Install Ruby with rvm and check the version

Step 3 - Install Nodejs

Rails requires a JavaScript runtime to compile the Rails asset pipeline. For Rails development on Ubuntu Linux, it's best to install Nodejs as the Javascript runtime.

Install nodejs from the nodesource repository:

curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
apt-get install -y nodejs

Step 4 - Configure Ruby Gem

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

Update the gem version and check it:

gem update --system
gem -v

This is optional, you can disable to install the documentation on every gem installation by adding a new line to the .gemrc file below.

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

Step 5 - Install Ruby on Rails 5 Stable

Install Ruby on Rails 5 with the gem command below:

gem install rails -v 5.0.0

When the installation finished successfully, check the rails version:

rails -v

And you will see the results of rails version:

Rails 5.0.0

Check the Rails version

Rails 5.0 has been installed on Ubuntu 16.04 with Ruby 2.3.1.

Step 6 - Setting up the PostgreSQL Database for Rails Development

In this step, we will prepare PostgreSQL for rails development. Ruby on Rails supports many databases such as MySQL, SQLite (Default) and PostgreSQL. We will use PostgreSQL as the database for this guide.

Install PostgreSQL and some other required packages with the apt command:

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

When the installation is done, login to the postgres user and access the postgresql shell.

su - postgres
psql

Give the postgres user a new password with command below:

\password postgres
Enter new password:

Next, create a new role named 'rails-dev' for the rails development with the command below:

create role rails_dev with createdb login password 'aqwe123';

Set a new password for the user and check that the user has been created.

Now check the new role and you will see new role has been created:

\du

The PostgreSQL database has been prepared for the Rails Development.

Check postgres permissions

Step 7 - Create the First Application with Rails and PostgreSQL

Ruby on Rails or Rails ships with the command "rails" and we can bootstrap our first application with that command.

Create a new application "myapp" with PostgreSQL as the default database.

rails new myapp -d postgresql

That command will create a new directory 'myapp' and install new gem including pg gem that is needed by rails to connect to the PostgreSQL database into that directory.

Next, go to the 'myapp' directory and edit the database.yml file in the config directory.

cd myapp/
vim config/database.yml

In the development section, uncomment line 32 and type the role we've created in step 6.

username: rails_dev

Set the rails_dev user password on line 35.

password: aqwe123

Uncomment line 40 and 44 for the database host configuration.

host: localhost
port: 5432

Now go to the test section and add the new configuration below:

  database: myapp_test
  host: localhost
  port: 5432
  username: rails_dev
  password: aqwe123

Save and exit.

Next, generate the database with the rails command:

rails db:setup
rails db:migrate

And then start the rails server with the command below:

rails s -b 192.168.1.110 -p 8080

Create the first Ruby on Rails application

Open your web browser and visit the server IP address on port 8080 - 192.168.1.110:8080.

Ruby on Rails application in the web browser

Back to the terminal and create a new scaffold to interact with the PostgreSQL database.

Type the command below:

rails g scaffold Post title:string body:text
rake db:migrate

Run the rails server again.

rails s -b 192.168.1.110 -p 8080

Visit the server : 192.168.1.110:8080/posts

You will see simple CRUD on Ruby on Rails with PostgreSQL.

Scaffolded ROR app

Reference

Share this page:

7 Comment(s)