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

How to Install Ruby on Rails on Ubuntu 18.04 LTS

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, I will show you the steps to install Ruby on Rails on Ubuntu 18.04 LTS. We will show you how to install and configure Rails with a PostgreSQL database, and how to create a new first project with Rails.

Prerequisites

  • Ubuntu 18.04 LTS
  • Root privileges

What we will do?

  1. Install RVM (Ruby Version Manager)
  2. Setup Ruby
  3. Install Nodejs
  4. Configure Ruby Gem
  5. Install Ruby on Rails
  6. Setup PostgreSQL Database for Rails Development
  7. Create Your First App with Rails and PostgreSQL

Step 1 - Install RVM (Ruby Version Manager)

RVM (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.

The first step we will do is to install the rvm packages using the installer script.

Add the rvm key to the server.

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

Install the rvm stable version by running the command below.

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

The command will automatically install packages required, and install the latest stable rvm version.

After the installation is complete, run the following command.

source /usr/local/rvm/scripts/rvm

Install RVM

Now you can use the rvm command to manage the ruby version.

rvm version

Step 2 - Setup Ruby Latest Version

The latest version of ruby at this day is Ruby 2.5.1, and it will be automatically installed during the rvm installation, when there is no ruby package on the system.

In this step, we will set up the default ruby version on the Ubuntu system.

Update the rvm to the latest stable version.

rvm get stable --autolibs=enable
usermod -a -G rvm root

Now check all available ruby versions.

rvm list known

And you will get a lot of available versions of ruby - install the latest stable version Ruby 2.5.1 using the rvm command as shown below.

rvm install ruby-2.5.1

After all installation is complete, make the ruby 2.5.1 as a default version on the Ubuntu system.

rvm --default use ruby-2.5.1

Check the Ruby version.

ruby -v

Now you will see ruby 2.5.1 is default ruby version on the Ubuntu 18.04 system.

Setup Ruby Latest Version

Step 3 - Install Nodejs

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

Add the nodejs nodesource repository to the system.

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

Install the latest version nodejs 10 and some additional packages using the apt command below.

sudo apt install -y nodejs
sudo apt install gcc g++ make

The nodejs 10 has been installed on Ubuntu 18.04 system.

Step 4 - Configure Ruby Gem

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

Update gem to the latest version and check it.

gem update --system
gem -v

Note:

This is optional, we can disable gem to install documentation on every ruby package installation. Simply by adding the configuration to the '.gemrc' configuration file.

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

Step 5 - Install Ruby on Rails

In this tutorial, we will be using the latest stable Ruby on Rails 5.2.0. We will install Rails using the gem ruby package manager.

Install Ruby on Rails 5.2.0 using the command below.

gem install rails -v 5.2.0

After the installation is complete, check the rails version.

rails -v

And following should the result.

Install Ruby on Rails

Ruby on Rails 5.2.0 has been installed on Ubuntu 18.04 LTS.

Step 6 - Setup PostgreSQL Database for Rails Development

By default, Ruby on Rails is using the SQLite database. It supports many databases system, including MySQL, SQLite, and PostgreSQL. And for this guide, we will be using PostgreSQL.

Install the PostgreSQL database using the apt command below.

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

After all installation is complete, start the Postgres service and enable it to launch everytime at system boot.

systemctl start postgresql
systemctl enable postgresql

Next, we will configure a password for the Postgres user, and create a new user for the Rails installation.

Login to the 'postgres' user and run the Postgres shell.

su - postgres
psql

Change the Postgres password using the query below.

\password postgres

Type your password and the password for postgres user has been added.

Now we will create a new role for our rails installation. We will create a new role named 'rails_dev' with the privilege of creating the database and with the password 'aqwe123'.

Run the Postgres query below.

create role rails_dev with createdb login password 'aqwe123';

Now check all available roles on the system.

\du

And you will get the 'rails_dev' role on the list.

Setup PostGres

PostgreSQL installation and configuration for Rails Development has been completed.

Step 7 - Create Your First App with Rails and PostgreSQL

Ruby on Rails provides a command-line 'rails' for bootstrapping our first rails application.

Create a new project 'myapp' with default database 'PostgreSQL' by running rails command below.

rails new myapp -d postgresql

Now you will see the 'myapp' directory, go to that directory and edit the database configuration file 'database.yml' using vim editor.

cd myapp/
vim config/database.yml

There are different configuration sections for each setup - Development, Testing, and Production.

In the development section, uncomment those line and change the value as below.

username: rails_dev
password: aqwe123
host: localhost
port 5423

For the testing section, paste those configurations under the testing section.

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

Save and exit.

Now generate the database and make sure there is no error.

rails db:setup
rails db:migrate

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

rails s -b 192.168.1.10 -p 8080

The first rails project will be running on the IP address '192.168.1.10' with port 8080.

Create Ruby App

Open your web browser and type the server IP address on the address bar.

http://192.168.1.10:8080/

You will get the default rails project homepage as below.

Ruby on Rails App is working

Next, we will test to create simple CRUD with PostgreSQL database on rails.

Run the rails command below.

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

Run the puma web server again.

rails s -b 192.168.1.10 -p 8080

And open the web browser with the URL below.

http://192.168.1.10:8080/posts/

Now you will get the simple CRUD form.

Test App written in RoR

And following is my result after creating a simple post.

Ruby on Rails installation with PostgreSQL database on Ubuntu 18.04 LTS has been completed successfully.

Share this page:

Suggested articles

14 Comment(s)

Add comment

Comments

By: Nick

Now why the heck would we use vim to edit the database.yml file. Thats so wierd

By: Arno Some

The development database config has port 5423 which should be port 5432

By: Gaurav

Should have read this comment before, you mentioned the correct port number.....

By: khaing war

This is very useful article.

Thank u very much :)

By: Rich

I don't think this command is working because I get this usage message

Usage  rvm-installer [options] [action]Options  [[--]version] <version>    The version or tag to install. Valid values are:      latest         - The latest tagged version.      latest-minor   - The latest minor version of the current major version.      latest-<x>     - The latest minor version of version x.      latest-<x>.<y> - The latest patch version of version x.y.      <x>.<y>.<z>    - Major version x, minor version y and patch z.  [--]branch <branch>    The name of the branch from which RVM is installed. This option can be used    with the following formats for <branch>:      <account>/        If account is rvm or mpapis, installs from one of the following:          https://github.com/rvm/rvm/archive/master.tar.gz          https://bitbucket.org/mpapis/rvm/get/master.tar.gz       Otherwise, installs from:         https://github.com/<account>/rvm/archive/master.tar.gz      <account>/<branch>        If account is rvm or mpapis, installs from one of the following:          https://github.com/rvm/rvm/archive/<branch>.tar.gz          https://bitbucket.org/mpapis/rvm/get/<branch>.tar.gz        Otherwise, installs from:          https://github.com/<account>/rvm/archive/<branch>.tar.gz      [/]<branch>        Installs the branch from one of the following:          https://github.com/rvm/rvm/archive/<branch>.tar.gz          https://bitbucket.org/mpapis/rvm/get/<branch>.tar.gz      [--]source <source>        Defines the repository from which RVM is retrieved and installed in the format:          <domain>/<account>/<repo>        Where:          <domain>  - Is bitbucket.org, github.com or a github enterprise site serving                      an RVM repository.          <account> - Is the user account in which the RVM repository resides.          <repo>    - Is the name of the RVM repository.        Note that when using the [--]source option, one should only use the [/]branch format        with the [--]branch option. Failure to do so will result in undefined behavior.      --trace        Provides debug logging for the installation script.Actions  master - Installs RVM from the master branch at rvm/rvm on github or mpapis/rvm           on bitbucket.org.  stable - Installs RVM from the stable branch a rvm/rvm on github or mpapis/rvm           on bitbucket.org.  help   - Displays this output.

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

By: Ed

rails db:setuprails db:migrateshould be...rake db:setuprake db:migrateOn top of that it fails when it runs. it fails with "ActiveRecord::AdapterNotSpecified: 'rails_dev' database is not configured."

 

By: Steve

This doesn't work on WSL 

By: Cesar Raul Villalbac

Thank you very much for the article, I could install in ubuntu 18.04 virtualbox on windows 10 and it worked

By: Jetson Davis

Instead of chanhging linix user using $su - postgres, I had to type: $sudo -u postgres psql postgres

 

By: Mosiah

Thanks dude!

By: Uli

Thanks for the tutorial, awesome stuff...

By: david

getting the error.

Cannot assign requested address - bind(2) for "192.168.1.10" port 8080 (Errno::EADDRNOTAVAIL)

 

nothing is running on that port. rails s -p 8080 works. just curious, do you have "192.168.1.10"  defined in host somewhere?

 

By: barry

When I run the commandrails g scaffold Post title:string body:text

I get a usage error message complaining about the syntax for the rails command

By: S Kumar

Every time use this "source /usr/local/rvm/scripts/rvm" when change folder or connect server (SSH).

How to set a path for this issue.