Ruby on Rails or RoR or Rails is a Ruby web application framework with MVC (Model-View-Controller) architecture, as well as design philosophies such as Don’t Repeat Yourself (DRY), Convention over Configuration (CoC), and active records pattern. It’s a full-stack web framework for building enterprise-grade web applications.

In this tutorial, we’ll walk you through the installation process of Ruby on Rails onto Ubuntu 24.04, creating the first Rails project, configuring PostgreSQL as the database for your Rails project, and generating a scaffold with the ‘rails’ command.

Prerequisites

Before you begin, make sure you have the following:

  • An Ubuntu 24.04 server.
  • A non-root user with sudo administrator privileges.

Installing Dependencies

To install Ruby on Rails on your Ubuntu system, you must install dependencies such as Ruby, Node.js, PostgreSQL (optional for database), Git, and additional development packages. Those packages are available on the default Ubuntu repository, and you can install those using APT.

To begin, update your Ubuntu repository with the following command:

sudo apt update

<img alt="update repo" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/1-update-repo.png664dc0238772c.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="193" loading="lazy" src="data:image/svg xml,” width=”704″>

Now install dependencies for Ruby on Rails with the command below. With this, you will install Ruby, PostgreSQL, Node.js, git, and some developer packages.

sudo apt install ruby ruby-dev postgresql libpq-dev nodejs yarnpkg git zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev libffi-dev

Input Y to proceed to the installation.

<img alt="install dependencies" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/2-install-deps.png664dc023cf821.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="83" loading="lazy" src="data:image/svg xml,” width=”750″>

After the installation is complete, check the Ruby and Node.js version with the following:

ruby -v

node -v

You can see below that Ruby 3.2 and Node.js 18.19 are installed.

Next, run the command below to set up Yarn as Node.js package manager with the command below. This will create a new command ‘yarn‘ and you will see the version of Yarn.

sudo ln -s /usr/bin/yarnpkg /usr/bin/yarn

yarnpkg -v

<img alt="check versions" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/3-check-versions.png664dc023ef2a9.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="231" loading="lazy" src="data:image/svg xml,” width=”653″>

Lastly, check the PostgreSQL service status with the following:

sudo systemctl is-enabled postgresql

sudo systemctl status postgresql

The PostgreSQL server will start at startup, and the PostgreSQL status with ‘active (exited)‘.

Creating PostgreSQL User

After dependencies are installed, you will be creating a new PostgreSQL user for your Rails project. This user must be able to log in and create a new database on PostgreSQL.

Log in to the PostgreSQL server with the command below.

sudo -u postgres psql

Run the following queries to create a new PostgreSQL user ‘appuser‘ with the password ‘AppuserPassword‘. This user will be able to create a database and role in PostgreSQL.

CREATE USER appuser WITH PASSWORD 'AppuserPassword';

ALTER USER appuser CREATEDB CREATEROLE;

Now type quit or press Ctrl d to exit from the PostgreSQL.

<img alt="create postgresql user" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/5-create-postgresql-user.png664dc02426ca7.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="418" loading="lazy" src="data:image/svg xml,” width=”746″>

Next, run the following command to log in to PostgreSQL with user ‘appuser‘. Input your password when prompted.

su - postgres

psql -U appuser -h 127.0.0.1 -d postgres

After logging in, check the PostgreSQL connection status with the following:

conninfo

You can see below the new user ‘appuser‘ can connect to the PostgreSQL server with the password authentication through localhost or 127.0.0.1.

<img alt="login to postgresql" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/6-login-to-postgresql.png664dc02454cac.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="292" loading="lazy" src="data:image/svg xml,” width=”750″>

Installing Ruby on Rails

In this section, you will be installing the latest Ruby on Rails on the user-specific environment. So you need to set up the default GEM_HOME directory under your user.

First, log in to your user with the following command:

su - user

Create a new ~/.gem directory and add the GEM_HOME environment variable through the ~/.bashrc file. With this, all Ruby gems will be installed in the ~/.gem directory.

mkdir -p ~/.gem

echo 'export GEM_HOME=$HOME/.gem' >> $HOME/.bashrc

Now run the command below to reload your current shell and check the GEM_HOME environment variable. You will see the GEM_HOME is pointed to the ~/.gem directory.

source ~/.bashrc

echo $GEM_HOME

<img alt="gem setup" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/7-gem-install-directory.png664dc0247c257.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="171" loading="lazy" src="data:image/svg xml,” width=”689″>

Next, run the gem command below to install the bundler to your system.

gem install bundler

<img alt="install bundler" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/8-bundler.png664dc024a1ffd.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="190" loading="lazy" src="data:image/svg xml,” width=”594″>

After that, install Ruby on Rails with the following gem command. You can install the latest version of Ruby on Rails or pick the specific version using the -v option.

gem install rails

gem install rails -v 7.1.0

<img alt="install rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/9-rails.png664dc024cd892.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="191" loading="lazy" src="data:image/svg xml,” width=”591″>

When the installation is finished, check the Ruby on Rails version with the command below:

rails --version

rails --help

As shown below, the latest Ruby on Rails 7.1.3 is installed, and you can also see the basic usage of the ‘rails‘ command.

<img alt="check version rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/10-check-rails.png664dc02501496.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="419" loading="lazy" src="data:image/svg xml,” width=”750″>

Creating Project with Ruby on Rails and PostgreSQL

Now that you have installed Ruby on Rails. In this section, you will create your first Rails project with PostgreSQL as the default database.

Run the ‘rails’ command below to create a new Rails project. In this example, you will create a new project testapp with the default database PostgreSQL.

rails new testapp -d postgresql

You can see below the Rails application is generated, including the git initialized.

<img alt="create app rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/11-create-project-rails-postgresql.png664dc02533522.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="679" loading="lazy" src="data:image/svg xml,” width=”716″>

Now move to the ~/testapp directory and edit the database configuration ‘config/database.yml‘ file.

cd ~/testapp/

nano config/database.yml

Change the database details for the development, test, and production environment like the following. The database for each environment will be created automatically during migration.

development:

<<: *default

database: testapp_development

username: appuser

password: AppuserPassword

host: localhost

port: 5432

test:

<<: *default

database: testapp_test

username: appuser

password: AppuserPassword

host: localhost

port: 5432

production:

<<: *default

database: testapp_production

username: appuser

password:

When done, save the file and exit.

Next, run the following ‘rails‘ command to set up and migrate the database.

rails db:setup

rails db:migrate

<img alt="migrating database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/12-db-migrate.png664dc02560997.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="153" loading="lazy" src="data:image/svg xml,” width=”750″>

After the database migration is complete, run your Rails project with the following:

rails server -b 0.0.0.0

You can see below that your Rails project is running on default port 3000.

<img alt="run rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/13-run-rails.png664dc02583138.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="277" loading="lazy" src="data:image/svg xml,” width=”693″>

Visit your server IP address http://192.168.5.30:3000/ using your web browser. if your Rails installation is successful, you should get the following default Rails index:

<img alt="ror index" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/14-ror-installed.png664dc025a95cf.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="500" loading="lazy" src="data:image/svg xml,” width=”750″>

Generating with Scaffolding

Ruby on Rails provides an easy way to build your application for beginners. You can utilize a scaffold to generate the basic structure of a Rails project, which includes a controller, a model, and a view.

Run the following command to generate a scaffold for a simple application ‘site‘ with two user inputs ‘name‘ and ‘url‘.

rails g scaffold site name:string url:string

Below is your application’s Rails generating model, controller, and view.

<img alt="scaffolding ror" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/15-scaffold.png664dc025d3191.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="351" loading="lazy" src="data:image/svg xml,” width=”705″>

After the scaffold is generated, run the following command to migrate your Rails database.

rails db:migrate

You can see below the database for Rails has been migrated.

<img alt="migrating database" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/16-migrate-database.png664dc02603b78.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="226" loading="lazy" src="data:image/svg xml,” width=”750″>

Now run your Rails project with the following command.

rails server -b 0.0.0.0

Lastly, visit your server IP address http://192.168.5.30:3000/sites to access the ‘site‘ application. You can submit a new data name and url. Then, you can see below an example after new data is submitted.

<img alt="scaffolding site" data-ezsrc="https://kirelos.com/wp-content/uploads/2024/05/echo/17-scaffold-app-site.png664dc02632985.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="435" loading="lazy" src="data:image/svg xml,” width=”485″>

Conclusion

Congratulations! You have finished the installation of Ruby on Rails on Ubuntu 24.04. You have created a Rails project with PostgreSQL as the database. Lastly, you have also learned how to generate basic applications on Rails using the scaffold feature.