Ruby on Rails or RoR is a free and open-source web application framework written in Ruby and released under the MIT license. Rails is a full-stack web framework for easily building enterprise-grade applications. Rails shipped with different tools that allow developers easily to create both frontend and backend applications. Ruby on Rails also has built-in security features such as protection for common attacks like SQL injection, XSS, and CSRF.

Ruby on Rails provides a default structure for the database, rendering HTML templates, a web service, and a web page. It follows the model-view-controller (MVC) architecture and also uses well-known design philosophies such as Don’t Repeat Yourself (DRY), Convention over Configuration (CoC), and active records pattern. Ruby on Rails was designed to be fast and easy to use and learn, Some notable sites developed with Rails such as Twitch, Airbnb, Github, Soundcloud, etc.

In this guide, we’ll walk you through the installation of Ruby on Rails on the Debian 12 server. You will install Ruby on Rails with a PostgreSQL database server and Rbenv Ruby version manager. You will also create a scaffold, the basic structure of the Rails project.

Prerequisites

Before commencing, confirm you’ve got:

  • A Debian 12 server.
  • A non-root user with sudo administrator privileges.

Installing Dependencies

In the first step, you will install some basic dependencies on your Debian server. This includes the PostgreSQL database server that will be used as the database for your Rails project, the node.js, and the Yarn package manager that will be used by Rails to compile static assets.

To start, update and refresh your package index by executing the apt update command below.

sudo apt update

Next, install dependencies using the following apt install command. This includes the PostgreSQL database server, libpq driver, Node.js, Yarn, Git, and some system libraries and tools.

sudo apt install 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 software-properties-common libffi-dev

Type y to confirm and proceed with the installation.

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

Once dependencies are installed, check the PostgreSQL server status using the following command. This will ensure that PostgreSQL is running and enabled on your Debian machine.

sudo systemctl is-enabled postgresql

sudo systemctl status postgresql

If PostgreSQL is enabled, you should get the output enabled. When PostgreSQL running, you should get the output active(running) or active(exited).

<img alt="checking postgresql" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/2-check-postgresql.png65032c1233b23.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="179" loading="lazy" src="data:image/svg xml,” width=”750″>

Lastly, check the Node.js and Yarn package manager by executing the command below.

node --version

yarnpkg --version

In this example, Node.js 18 and Yarn 1.22 is installed.

<img alt="checking nodejs and yarn" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/3-check-nodejs-yarn.png65032c12507a8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="143" loading="lazy" src="data:image/svg xml,” width=”362″>

Installing Rbenv

After installing package dependencies, the next step is to install Rbenv, the Ruby version manager for Unix-like operating systems. With Rbenv, you can easily manage your Ruby apps environment, also you can install multiple Ruby versions on your system.

Log in to your User using the following command.

su - user

Download the rbenv source code and the ruby-build plugin via the git command below.

git clone https://github.com/rbenv/rbenv.git ~/.rbenv

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

Now execute the following command to add a custom PATH to your shell.

echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc

echo 'eval "$(rbenv init -)"' >> ~/.bashrc

echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc

Reload your ~/.bashrc configuration to apply the changes. After executing the command, your rbenv installation should be activated.

source ~/.bashrc

Verify rbenv by executing the rbenv command below. If rbenv installation is successful, you should see available rbenv commands/options.

rbenv commands

<img alt="install rbenv" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/5-install-rbenv.png65032c1268d84.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="610" loading="lazy" src="data:image/svg xml,” width=”750″>

Installing Ruby via Rbenv

With Rbenv installed, you can now install Ruby on your system. With Rbenv, you will install Ruby on your current environment only, which does not affect the whole system. You will install Ruby 3.2.2 to your current user environment.

Execute the rbenv install command below to install Ruby 3.2.2 to your system.

rbenv install 3.2.2

During the installation, you should see this:

<img alt="install ruby via rbenv" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/6-install-ruby-rbenv.png65032c12966cf.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="234" loading="lazy" src="data:image/svg xml,” width=”750″>

Once Ruby is installed, execute the following command to set up the default Ruby version to 3.2.2.

rbenv global 3.2.2

Lastly, verify the Ruby version on your system using the command below.

ruby -v

If everything goes well, you should see Ruby 3.2.2 is installed.

<img alt="setup and verify ruby" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/7-checking-ruby-version.png65032c12b1f8d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="153" loading="lazy" src="data:image/svg xml,” width=”563″>

Installing Ruby on Rails

At this point, your system is configured and ready to install Ruby on Rails to your Debian machine. In this example, you will install Ruby on Rails 7.0, and check the list of available versions of Rails on the official site.

Execute the gem commands below to install the bundler, then install Ruby on Rails 7.0.7.2.

gem install bundler

gem install rails -v 7.0.7.2

During the installation, you should see an output like the following:

<img alt="install bundler" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/8-install-bundler.png65032c12e02cf.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="237" loading="lazy" src="data:image/svg xml,” width=”576″>

<img alt="install rails 7.0" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/9-install-rails.png65032c1309be0.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="601" loading="lazy" src="data:image/svg xml,” width=”449″>

Now run the rebenv command below to rehash and reload your current environment.

rbenv rehash

Lastly, execute the rails command below to ensure that Ruby on Rails is installed.

rails version

rails -h

If the installation is successful, you should see your current Rails version and the help page of the rails command.

<img alt="checking rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/10-checking-rails.png65032c131d921.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="243" loading="lazy" src="data:image/svg xml,” width=”750″>

Creating First Rails Project

In this section, you will learn how to create your first project with Ruby on Rails. You will be using PostgreSQL as the default database for your Rails project. To achieve that you must complete the following:

  • Preparing the PostgreSQL user for application.
  • Creating the first Rails project.

Prepare Database User

First, you must create a new PostgreSQL user that will be used for your Rails application. This user must have privileges for creating databases and users.

Back to your user account and log in to the PostgreSQL server using the command below.

sudo su

sudo -u postgres psql

Now create a new user bob with the password p4sswordbob. Then, assign new privileges for creating a database and roles to the user bob.

CREATE USER bob WITH PASSWORD 'p4sswordbob';

ALTER USER bob CREATEDB CREATEROLE;

<img alt="create postgresql role" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/11-create-postgresql-user.png65032c133e34f.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="265" loading="lazy" src="data:image/svg xml,” width=”531″>

Verify the list of users and privileges on your PostgreSQL server using the command below.

du

You should see the user bob with privileges CREATEDB and CREATEROLE.

<img alt="list users" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/12-checking-user.png65032c136c0fa.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="156" loading="lazy" src="data:image/svg xml,” width=”750″>

Type q to log out from the PostgreSQL server.

Lastly, log in to your user and execute the following psql command to log in to the PostgreSQL server as the new user bob.

su - user

psql -U bob -h 127.0.0.1 -d postgres

Once connected to the PostgreSQL server, execute the following query to verify your connection information.

conninfo

You should see that you’ve connected to the PostgreSQL server as a user bob.

<img alt="check connections" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/13-checking-user-connection-postgresql.png65032c1396f82.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="259" loading="lazy" src="data:image/svg xml,” width=”750″>

Type q to exit from the PostgreSQL server.

Creating Rails Project

After creating a PostgreSQL user, you can now start creating a new Rails project via the rails command-line utility.

To create a new rails project, run the rails command below. In this example, you will create a new project testapp with the default database PostgreSQL.

rails new testapp -d postgresql

The output of the command should look like this:

<img alt="create rails project" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/14-create-new-app-rails.png65032c13e5978.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="579" loading="lazy" src="data:image/svg xml,” width=”750″>

After the project is created, the new directory ~/testapp will also be created. Move into the ~/testapp directory and open the database configuration config/database.yml using your preferred text editor.

cd testapp/

nano config/database.yml

Change the default database settings for development, test, and production. Be sure to input your PostgreSQL username and password.

development:

  <<: *default

  database: testapp_development

  # The specified database role is being used to connect to postgres.

  # To create additional roles in postgres see `$ createuser --help`.

  # When left blank, postgres will use the default role. This is

  # the same name as the operating system user running Rails.

  username: bob

  # The password associated with the postgres role (username).

  password: p4sswordbob

  # Connect on a TCP socket. Omitted by default since the client uses a

  # domain socket that doesn't need configuration. Windows does not have

  # domain sockets, so uncomment these lines.

  host: localhost

  # The TCP port the server listens on. Defaults to 5432.

  # If your server runs on a different port number, change accordingly.

  port: 5432

Save and exit the file when you’re done.

Now run the rails command below to migrate the database. This will automatically create a new database for your testapp project.

rails db:setup

rails db:migrate

Below you should see the output during the database migration of the testapp project.

<img alt="migrate the database" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/15-migrate-the-database.png65032c1405cbb.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="192" loading="lazy" src="data:image/svg xml,” width=”561″>

After the database is migrated, execute the rails command below to run the testapp project. This will run testapp within your IP address on port 3000.

rails server -b 192.168.10.15

In the following output, you should see that testapp is running.

<img alt="running rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/16-running-rails.png65032c141f0ea.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="284" loading="lazy" src="data:image/svg xml,” width=”575″>

Now launch your favorite web browser and visit your server IP address followed by port 3000, such as http://192.168.10.15:3000/. If your installation is successful, you should see the default index.html page of Ruby on Rails.

<img alt="rails index" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/17-rails-index.png65032c14394b4.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="507" loading="lazy" src="data:image/svg xml,” width=”750″>

Press Ctrl c to terminate your Rails application.

Rails Scaffolding for Starter Kit

A scaffold is an automatic way to generate the basic structure of a Rails project, which includes a controller, a model, and a view.

Execute the rails command below to create scaffold books with three fields title, author, and publication_year.

rails g scaffold books title:string author:string publication_year:integer

<img alt="generate scaffold" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/18-generate-scaffolding.png65032c14556a8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="498" loading="lazy" src="data:image/svg xml,” width=”750″>

Now migrate the database to apply the changes using the rails command below.

rails db:migrate

<img alt="migrate the database" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/19-migrate-database.png65032c1471f6d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="164" loading="lazy" src="data:image/svg xml,” width=”745″>

Next, run your Rails project by executing the rails server command below.

rails server -b 192.168.10.15

<img alt="run raikls project" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/16-running-rails.png65032c141f0ea.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="284" loading="lazy" src="data:image/svg xml,” width=”575″>

Once testapp is running, check the books scaffold via URL path /books, such as http://192.168.10.15:3000/books. If everything goes well, you should see the generated scaffold like the following:

<img alt="scaffold rails" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/20-scaffold-rails.png65032c14c939d.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="197" loading="lazy" src="data:image/svg xml,” width=”577″>

You can now insert new data to the books scaffold like the following:

<img alt="scaffoled input data" data-ezsrc="https://kirelos.com/wp-content/uploads/2023/09/echo/21-insert-data.png65032c14e69c8.jpg" ezimgfmt="rs rscb10 src ng ngcb9" height="360" loading="lazy" src="data:image/svg xml,” width=”536″>

Conclusion

In conclusion, you’ve completed the installation of Ruby on Rails with the PostgreSQL database server and Rbenv on the Debian 12 server. You’ve also learned how to generate scaffolds for basic structures for the Rails project.