Ruby on Rails, often referred to as Rails, is a popular open-source web application framework written in Ruby. It’s known for its simplicity and productivity, allowing developers to build applications quickly without sacrificing quality or scalability. In this tutorial, we will show you how to install Ruby on Rails with PostgreSQL on AlmaLinux 9.
To add to this, PostgreSQL is a powerful, open-source object-relational database system. It’s highly extensible and standards-compliant, making it an excellent choice for storing your application’s data.
AlmaLinux is an open-source, community-driven project that intends to fill the gap left by the demise of the CentOS stable release. AlmaLinux 9 is a 1:1 binary compatible fork of RHEL® 9, and the creators of the established CloudLinux OS help build it. Let’s start with the installation.
Table of Contents
To complete the guide, we need:
- A VPS with AlmaLinux 9 installed.
- A non-root user with sudo privileges.
Step 1. Update your System
Before proceeding with the actual installation, we need to run a full system update. So, to do that, run the following on your SSH as root:
$ dnf update -y
Step 2. Install Ruby on Rails
As the ruby package is already present on Almalinux, to install it, we’ll run:
$ sudo dnf install ruby ruby-devel
As Rails depends on the Development tools package to be installed, be sure to run:
$ sudo dnf group install "Development Tools" -y
This will ensure that our Rails installation will work fine.
Step 3. Installing PostgreSQL
For our PostgreSQL installation, we’ll use the APPStream repository. To add the repository to your system, you need to run:
$ sudo dnf install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-9-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Then, to install the service, you run:
$ sudo dnf install -y postgresql15-server postgresql15-contrib libpq5-devel
As soon as your installation is completed, you can initiate the PostgreSQL configuration with:
$ sudo postgresql-15-setup initdb
Be sure also to start the service and enable it to boot with your system:
$ sudo systemctl start postgresql-15
$ sudo systemctl enable postgresql-15
Step 4. Installing NodeJS and Yarn
Since Ruby on Rails is more focused on back-end services, we need software that will compile our front-end assets. This will be handled by NodeJS/Yarn. To install NodeJS, we need to run:
$ sudo dnf install nodejs npm -y
And yarn is installed using this command:
$ sudo npm install -g yarn
Yarn package by default uses
/usr/local/bin path. If your system $PATH variable does not include this path, be sure to run:
$ echo "export PATH=$PATH:/usr/local/bin" >> ~/.bashrc
$ source ~/.bashrc
Step 5. Installing Ruby on Rails via Gem Manager
We need to install Ruby on Rails using Gem as any other modules/dependencies, etc. from Ruby on Rails are installed with the gem command as well. So, to install it after installing all dependencies, you need to run:
$ sudo gem install rails
Step 6. Creating a PostgreSQL role
We need to create a PostgreSQL role (user) to manage the database. This user will need to have the “management” role, so the user can create/delete databases, etc.
So, first of all, to manage the PostgreSQL service, you need to login to the
$ su - postgres psql
When you’re logged in as the
postgres user, you can create our role with:
CREATE ROLE rails WITH CREATEDB LOGIN PASSWORD 'Y0ur_S3cur3_P4ssw0rd';
Make sure to change Y0ur_S3cur3_P4ssw0rd to a unique password. To confirm your user is created properly, you can list all users with:
Step 7. Creating a Rails Project
In this last section, we’ll deploy our Rails project. To do that, we’ll start by creating the folder
/var/www/ and entering it:
$ mkdir -p /var/www; cd /var/www
Now that we are inside our folder, we can create the project:
$ rails new rails_project --database=postgresql
After your project is created, you need to change your directory to it:
$ cd rails_project/
Then, edit the database file so we can setup our password we set before:
$ nano config/database.yml
After changing/adding the values, you can save the file and exit the editor.
Next, to migrate the database we need to run:
$ rails db:setup
$ rails db:migrate
After the migrate command is finished, you can finally start your rails server with the command:
$ rails server --binding=0.0.0.0
And, if you receive a positive output, you are good to go, and you can access your website at http://your_ip:3000.
That’s it, we have successfully installed Ruby on Rails on a VPS server with AlmaLinux 9.
If you find all of this a little too overwhelming or if you’re getting errors, why not sign up for one of our Ruby on Rails hosting plans? Our VPSes are one of the fastest you can find anywhere, and our managed support covers all aspects of your server and software, not just Ruby on Rails or your OS. Try us – you won’t regret it.
If you liked this post on how to install Ruby on Rails together with PostgreSQL on AlmaLinux 9, please share it with your friends & followers on social media, or leave a comment below if you have anything to add or any questions. Thanks.