If you’re a developer looking for a reliable, open source, cross-platform web development environment for Linux, Ruby on Rails is a great choice for you. This helps them build applications and websites, abstracting and simplifying the repetitive tasks that arise during the development process. It is called Ruby on Rails because Rails is written in the Ruby programming language, just like Symfony and Zend are written in PHP, and Django is written in Python. Rails provides standard frameworks for databases, web servers, and websites. Well-known applications like Soundcloud, Github, and Airbnb are based on Rails. Ruby on Rails is licensed under the MIT and was first released in December 2005. All of his repositories are available on Github, including the latest version to date.
In this article, we will provide step-by-step instructions for installing and configuring Ruby on Rails with all its requirements. Then we will explain how to install and configure the PostgreSQL database to create your first Rails project. Finally, we will also create a simple CRUD interface to make your application more interactive and useful.
We have completed the commands and procedures mentioned in this article on the Debian 10 Buster system. We use the Debian command line terminal to install and configure Ruby on Rails. You can access the terminal application by searching in the Application Launcher as follows:
The application launcher can be accessed using the Super / Windows key on the keyboard.
Installing Ruby on Rails (RoR) on Debian 10
To install Ruby on Rails, you must first install and configure the latest versions of some of the necessary components on your system, such as:
- RVM-Ruby Version Manager
- Ruby Gems-Ruby Package Manager
In this section, we will first prepare our system by first installing all these step-by-step instructions, installing their latest versions, and then finally installing Ruby on Rails.
1. Install Ruby Version Manager (RVM)
Ruby Version Manager helps us manage the installation of Ruby and configure multiple versions of Ruby on the same system. Complete these steps in order to install the RVM package through the installer script:
Step 1: Add RVM Key to Your System
Run the following command to add the RVM key; This key will be needed when installing the stable version of RVM:
$ gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
Step 2: Install Curl
We will install RVM through Curl. Since it does not come by default with the latest versions of Debian, we will need to install it using the following commands as sudo:
$ sudo apt-get install curl
Please note that only authorized users can add / remove and configure software in Debian.
The system will offer you the Y / n option to confirm the installation. Please enter Y to continue, after which Curl will be installed on your system.
Step 3: Install the stable version of RVM
Now run the following command to install the latest stable version of RVM.
$ curl -sSL https://get.rvm.io | bash -s stable --ruby
This command will also automatically install all the necessary packages needed to install RVM.
The process will take some time depending on the speed of your Internet, after which RVM will be installed on your system.
Step 4. Configure the RVM source folder
Note that the last few lines of the RVM installation output prompts you to run the following command:
$ source /home/[username]/.rvm/scripts/rvm
This is used to set the source folder to the one specified in the output. You need to run this command to start using RVM.
When you configure the source, you will receive the following output:
Now the source for the RVM is installed. You can check the version number of the RVM installed on your system with the following command:
$ rvm --version
It also ensures that RVM is indeed installed on your system.
2. Configure the latest version of Ruby As by default
When installing RVM, the latest version of Ruby is also installed on your system. However, we need to configure our system to use the latest version of Ruby by default. To do this, follow these steps:
Step 1: Get a List of All Available Ruby Versions
The following command gives you a list of all versions of Ruby released before the date:
$ rvm list known
In this list, select the latest available version of Ruby. As you can see from the results, Ruby 2.7 is the latest available version.
Step 2: Install the Latest Ruby
Now install the latest version of Ruby that you selected in the previous step by running the following rvm command:
$ rvm install ruby-2.7
The process may take some time depending on the speed of your Internet, after which a selected amount of Ruby will be installed on your system.
Step 3: Install the latest version of Ruby by default
The following rvm command will help you install the latest installed version of Ruby as the system default setting:
$ rvm --default use ruby-2.7
You can see that now my system will use Ruby 2.7.0 as the default version of Ruby
In case of error:
You can also get the following output after executing the above command:
In this case, run the following command to enable the login shell:
Then enter the following command again to install the Ruby version:
$ rvm --default use ruby-2.7
You can also verify this default version by running the following command:
$ ruby -v
3. Install Nodejs and the gcc compiler
Step 1: Install the Latest Nodejs
Use the following command to install the Nodesource repository on your system:
$ curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
Now install the latest version of Nodejs using the following apt command as sudo:
$ sudo apt-get install -y nodejs
The latest available version of Nodejs 10 will be installed on your system.
Step 2. Install the gcc compiler.
The gcc compiler is another prerequisite that must be set before doing any development on Rails. Use the following command as sudo to install:
$ sudo apt-get install gcc g++ make
5. Set up the latest version of RubyGems as the default system.
When you install RVM, RubyGems is also installed on your system. However, we need to configure our system to use the latest version of RubyGems, which is used by default. Ruby Gems is basically a Ruby on Rails package manager that comes with the gem command-line tool.
Run the following gem command to update the system to use the latest version:
$ gem update --system
Now, when you check the version number with the following command, you will see that your system uses the latest version of RubyGems on the command line:
$ gem -v
5. Install Ruby on Rails
Finally, after installing all the necessary components, we can install Ruby on Rails on our system by doing the following:
Step 1: Search for the latest available version
The RubyGems website supports all versions of Ruby on Rails up to date at the following link:
Select the latest version of Ruby on Rails that you want to install. At the time of this writing, the latest available version is 220.127.116.11.
Step 2: Install the Latest Version
You can install the latest version of Ruby on Rails using the gem command line tool as follows:
$ gem install rails -v 18.104.22.168
The installation process may take some time depending on your internet connection.
After the installation is complete, run the following command to view the version of Rails installed on your system.
$ rails -v
The team also checks to see if Ruby on Rails is actually installed on your system.
Ruby on Rails supports many databases, such as SQLite, MySQL, and PostgreSQL. In this section, we will explain how to start developing Rails with PostgreSQL. This will include:
- PostgreSQL database installation
- PostgreSQL configuration and role creation
- Your first Rails app
- Creating a simple CRUD with a PostgreSQL database on Rails
1. Install and configure the PostgreSQL database
Step 1: Install PostgreSQL
Use the following apt command as sudo to install the PostgreSQL database and some other required packages:
$ sudo apt-get install postgresql postgresql-contrib libpq-dev -y
Step 2: Start and enable the Postgresql service
After installing PostgreSQL, you need to start the postgresql service with the following command:
$ systemctl start postgresql
The system will offer you an authentication dialog, since only an authorized user can enable services in Debian. Enter the administrator password and click the “Authenticate” button, after which the service will start.
The next step is to enable the service using the following command:
$ systemctl enable postgresql
The system will offer you a similar authentication dialog several times; enter the administrator password each time and click the Authentication button, after which the service will be turned on.
Step 3: Verify Installation
Please run the following command to view a detailed status report on your PostgreSQL installation:
$ dpkg --status postgresql
2. Configure PostgreSQL and create roles
PostgreSQL applications can be created by the user or by roles. By default, there is a user called “postgres”, which is the superuser and can create and migrate databases, as well as manage other user roles.
First, you can log in as sudo on PostgreSQL with the following command:
$ sudo -u postgres psql
Here you can change the postgres password as follows:
postgress=# password postgres
The superuser can create a new user role with the following command:
create a role “role_name” with a password to log in the created user “password”;
postgress=# create role dev_rails with createdb login password 'rockon123' ;
We create a role called “dev_rails”. This is the user who will create the database for our first Rails application.
The superuser can view the list of roles existing in PostgreSQL as follows:
Use Ctrl + z to exit PostgreSQL.
3. Your first Rails application
Now we will create our first Rails application with PostgreSQL as the default database. This includes the following steps:
Step 1. Create a new application
Create a new project with the name “firstapp” or any other name with the following command and specify PostgreSQL as the database as follows:
$ rails new firstapp -d postgresql
This will create a project folder in your home folder as follows:
Step 2: Configure Your Rails Project to Enable the PostgreSQL User Role
Now we want the custom role that we created in PostgreSQL to create the database in the Rails application. To do this, we need to edit the database.yml file located in the folder of the newly created application in the / config / folder.
Go to your first application and then to the configuration folder as follows
$ cd firstapp/config/
Here you will see the database.yml file. You can edit this file through your favorite text editor. We will do this through the Nano editor with the following command:
$ nano database.yml
In this file you can see mainly three sections:
We will need to configure the “Development” and “Testing” sections of the file.
Make the following settings in the Development section
And the following in the “Test” section:
Note: Please make sure the syntax is correct. Each line must be preceded by 2 spaces and NOT Tabs
Save the file by pressing Ctrl + X, then Y and then pressing Enter.
Step 3. Create and then transfer the database.
Create the database using the following rails command:
$ rails db:setup
You may get the following error in Debian:
Run the command shown in the screenshot above:
And then run the rails db: setup command again:
Please make sure there are no errors. Most errors are due to the incorrect syntax for the database.yml file or the username and password mismatching with the one you created in PostgreSQL.
After successful generation, migrate the database using the following rails command:
$ rails db:migrate
Step 4: Launch the Puma Rails Web Server
Before starting the Puma Rails web server successfully, you need to install Yarn on your system. You can install Yarn on Debian with the following set of commands:
$ curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
$ echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
$ sudo apt-get upate
$ sudo apt install --no-install-recommends yarn
Another condition is to install WebPacker via Rails as follows:
After completing the application setup, enter the following command to start the default Puma web server:
$ rails s -b localhost -p 8080
Or in our case
$ rails s -b 127.0.0.1 -p 8080
After this command, your first Rails application runs on a local host with port 8080.
Step 5: Open the Default Rails Project Home Page
You can view your database successfully hosted on the default Rails project home page by entering the URL in one of your web browsers:
HTTP: // local: 8080 /
Now you can perform any CRUD operation in this simple application. Follow this article to make your application more interactive.
4. Create a simple CRUD with a PostgreSQL database on Rails
Let’s make our application more interactive by implementing the CRUD interface (create, read, update, delete).
Step 1: Create a Scaffold in Rails
Run the following command to create a scaffold in the Rails application folder
$ rails g scaffold Post title:string body:text
Then migrate the database by running the following command:
$ rake db:migrate
Step 2: Run the application on the Puma Rails web server
Then run the application on the local host by starting the Puma web server again with the following command:
$ rails s -b localhost -p 8080
You can also use your local IP address, like us, for the above command:
$ rails s -b 127.0.0.1 -p 8080
Step 3: Open the Messages Page in the Rails Project
You can view your database successfully hosted on the Rails project page by entering the URL in one of your web browsers:
HTTP: // local: 8080 / messages /
Or use a local IP address like us:
You will be able to see the simple CRUD interface with which you can create, edit, display and delete messages.
When I created the message using the “New message” link, this is what my message page looks like:
Installing and Configuring Ruby on Rails on Debian 10