Linux

How to Install Ruby on Rails (RoR) on Debian 10

How to Install Ruby on Rails (RoR) on Debian 10

Ruby on Rails (RoR) is an open source web application framework, published under the MIT License. This is a server-side web application framework that follows the MVC (Model-View-Controller) concept.

Rails provides a default structure for databases, web services, and web pages. More than 3000 developers have contributed code to the Rails framework and there are many well-known applications based on Rails, such as Github, Airbnb, Soundcloud, etc.

In this tutorial, we will show you how to install Ruby on Rails on Debian Buster 10. This guide will cover installing RVM (Ruby Version Manager), installing a PostgreSQL database, and creating a simple CRUD application with Ruby on Rails and using PostgreSQL as a backend database .

Precondition

For this guide, we will use the latest Debian Buster 10 with 2GB of RAM and 25GB of free disk space and 2 CPUs.

What we will do?

  • Install RVM (Ruby Version Manager)
  • Install Ruby with RVM
  • Install Nodejs and Threads
  • Upgrade RubyGem (Ruby Package Manager)
  • Install Ruby on Rails Stable
  • Install and Configure PostgreSQL Database
  • Starting a Project with Ruby on Rails

Step 1 – Install RVM (Ruby Version Manager)

First, we will install RVM (Ruby Version Manager) on our Debian Server. RVM will make it easy for you to install and manage Ruby on your system, especially when you use multiple versions of Ruby.

Before proceeding further, install the gnupg2 package using the apt command below.

sudo apt install gnupg2

Next, import the GPGP key for the RVM package using the gpg command below.

gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2.69D6956105BD0E739499BDB

Download the RVM installer script and install it using the command below.

curl -sSL https://get.rvm.io | bash -s stable --ruby

1

After installation is complete, load RVM into the Debian system.

source /usr/local/rvm/scripts/rvm

And the Ruby Version Manager has been installed to the Debian 10 system, check using the ‘rvm’ command below.

rvm version

As a result, RVM (Ruby Version Manager) xxx has been installed on Debian Buster 10.

2

Step 2 – Install Ruby with RVM

In this step, we will update the RVM package to the latest version and install the latest stable version of Ruby, and make it the default Ruby version on our system.

Upgrade RVM to the latest stable version using the commands below.

rvm get stable --autolibs=enable

After that, add the ‘root’ user to the ‘rvm’ group. As a result, the root user will be able to run the rvm command.

usermod -a -G rvm root

Next, install the latest stable version of ‘Ruby 2.6’ using the rvm command below.

rvm install ruby-2.6

After installation is complete, make Ruby 2.6 the default version of Ruby on the Debian system.

rvm --default use ruby-2.6

Now check the version of Ruby installed using the command below.

ruby --version

As a result, the latest stable version of ‘Ruby 2.6’ has been installed via RVM and is used as the default Ruby on Debian 10 systems.

3

Step 3 – Install Nodejs and Yarn

In this step, we will install Nodejs and Yarn to the Debian system. This will be used by Rails to process JavaScript files.

First, install the Development Tool to the Debian system using the apt command below.

sudo apt install gcc g++ make

After that, add the Nodesource repository for Nodejs 12.

curl -sL https://deb.nodesource.com/setup_12.x | bash -

Then add the key and the GPG Thread repository.

curl -sL 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

Update and refresh all repositories and install NodeJS and Yarn using the apt command below.

sudo apt update
sudo apt install -y nodejs yarn

After the installation is complete, check using the command below.

node --version
yarn --version

As a result, the Nodejs JavaScript Runtime and Yarn package manager has been installed on Debian Buster 10.

4

Step 4 – Upgrade RubyGem (Ruby Package Manager)

RubyGems is the Ruby Package Manager, comes with a jewel command line tool and is automatically installed when we install Ruby on the system. For this step, we will upgrade the Ruby Gem to the latest version.

Upgrade RubyGem using the command below.

gem update --system

When finished, check the gem version using the command below.

gem -v

As a result, RubyGem has been upgraded to the latest version.

Optional:

RubyGem is a package manager for installing Ruby packages. This will install the Ruby package and the documentation.

To make a small Ruby package installation, you can deactivate the installation of the Ruby package documentation through the RubyGem configuration.

Add / make gem options to the ~ / .gemrc configuration as below.

echo "gem: --no-document" >> ~/.gemrc

As a result, RubyGem will only install core packages from Ruby packages, without documentation.

Step 5 – Install Ruby on Rails Stable

In this step, we will install Ruby on Rails to the Debian system. We will install the latest version of Ruby on Rails v6.0.2.1.

Install Ruby on Rails using the RubyGem command as below.

gem install rails -v 6.0.2

After the installation is complete, check the rails command as below.

rails -v

As a result, Ruby on Rails 6.0.2.1 has been installed on Debian Buster 10. And you are ready to start a new Application using Rails.

5

Step 6 – Install and Configure the PostgreSQL Database

For this tutorial, we will use PostgreSQL as the backend database for Ruby on Rails.

Install PostgreSQL to the Debian system using the apt command below.

sudo apt install postgresql postgresql-contrib libpq-dev -y

After the installation is complete, start the PostgreSQL service and add it to the system boot.

systemctl start postgresql
systemctl enable postgresqlThe PostgreSQL service is active and running.

6

Next, enter the PostgreSQL shell using the command below.

sudo -i -u postgres psql

Change the password for the default PostgreSQL user ‘postgres’.

\password postgres

Type the new password for the ‘postgres’ user.

Next, create a new PostgreSQL role named ‘rails_dev’ with ‘CreateDB’ privileges and password ‘aqwe123’ using the PostgreSQL query below.

create role rails_dev with createdb login password 'aqwe123';

The ‘Createdb’ privilege will allow ‘rails_dev’ users to create a new database for the Ruby on Rails project.

Now all users available in PostgreSQL use the command below.

\du

You will get a ‘rails_dev’ user with the ‘CreateDB’ privilege to create a database.

7

Type ‘exit’ to exit the PostgreSQL shell.

As a result, you have installed the PostgreSQL database on Debian buster 10. Additionally, you have created a new PostgreSQL role ‘rails_dev’ with ‘CreateDB’ privileges for the ‘Ruby on Rails’ project.

Step 7 – Start the Project with Ruby on Rails

In this step, we will create a new simple CRUD project using Ruby on Rails with PostgreSQL as a backend database.

Start the Rails Project with PostgreSQL

First, start a new project called ‘myapp’ using the rails command below.

rails new myapp -d postgresql

This will produce a project directory named ‘myapp’ with PostgreSQL as the backend database.

Now open the ‘myapp’ directory and edit the configuration database ‘config / database.yml’.

cd myapp/
vim config/database.yml

In the ‘Development’ section, add the PostgreSQL detailed configuration below.

username: rails_dev
  password: aqwe123
  host: localhost
  port: 5432

In the ‘Testing’ section, add the PostgreSQL detailed configuration below.

 host: localhost
  port: 5432
  username: rails_dev
  password: aqwe123

Save and close.
Next, create a PostgreSQL database scheme and make sure there are no errors.

rails db:setup
rails db:migrate

8

After all is done, start the puma rails web server using the command below.

rails s -b 149.28.140.6 -p 8080

The Ruby on Rails project is active and running on port ‘8080’.

Open your web browser and type the server’s IP address with port ‘8080’.

http://10.5.5.31:8080/

You will get the default index.html page from the Rails project. Additionally, it will detect your Rails deployment environment.
9

As a result, you have created a new project with Ruby on Rails with PostgreSQL as a database backend.
Create a Simple CRUD application

Next, we will produce a simple CRUD on our ‘myapp’ project.

Generate a CRUD in Ruby on Rails using the command below.

rails g scaffold Post title:string body:text
rake db:migrate

10

Start the rail project using the command below.

rails s -b 192.168.1.10 -p 8080

Return to your web browser and check your rail project with the URL below.

http://10.5.5.31:8080/posts/

Now you will get a simple CRUD application like below.

11

Below are the results after posting a sample.

12

Finally, the installation of Ruby on Rails with the PostgreSQL database in Debian Buster 10 has been completed successfully.

Related posts

How to Install the Open Source Computer Vision Library (OpenCV) on CentOS 8

Linux

How to Set Up an FTP Server on Ubuntu

Linux

How to Get Windows Terminal Fonts (Cascadia Code) on an Ubuntu Terminal

Linux

How to Set Up a Firewall with UFW on Ubuntu 20.04

Linux

Linux Pinky Command Tutorial for Beginners (8 Examples)

Linux

How to delete a Terminal on Ubuntu and other Linux distributions

Linux

How to Install Anaconda on Ubuntu 20.04

Linux

How to Install VirtualBox Guest Additions on Ubuntu 20.04

Linux

How to Install and Use a Linux Firewall

Linux