How to Install Ruby on Rails on Debian 12

1 install deps

How to Install Ruby on Rails on Debian 12

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.


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.

install dependencies

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).

checking postgresql

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.

checking nodejs and yarn

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

install rbenv

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:

install ruby via rbenv

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.

setup and verify ruby

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
gem install bundler
gem install rails -v

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

install bundler

install rails 7.0

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.

checking rails

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';

create postgresql role

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

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

list users

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 -d postgres

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

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

check connections

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:

create rails project

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.
  <<: *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.

migrate the database

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

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

running rails

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

rails index

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

generate scaffold

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

migrate the database

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

run raikls project

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

scaffold rails

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

scaffoled input data


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.

Đăng ký liền tay Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !

Add Comment

Click here to post a comment

Đăng ký liền tay
Nhận Ngay Bài Mới

Subscribe ngay

Cám ơn bạn đã đăng ký !

Lỗi đăng ký !