How to install Ruby on Rails (RoR) on Debian 10
Ruby on Rails (RoR) is an open source web application framework, released under the MIT license. It is a server-side web application framework that follows the concept of MVC (Model-View-Controller).
Rails provides default structures 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 RVM (Ruby Version Manager) installation, PostgreSQL database installation, and creating simple CRUD applications using Ruby on Rails and PostgreSQL. As a back-end database.
For this guide, we will use the latest Debian Buster 10 with 2GB RAM and 25GB free disk space and 2 CPUs.
What are we going to do?
- Install RVM (Ruby Version Manager)
- Install Ruby using RVM
- Install Nodejs and Yarn
- Upgrade RubyGem (Ruby Package Manager)
- Install Ruby on Rails Stable
- Install and configure PostgreSQL database
- Start a project using Ruby on Rails
Step 1-Install RVM (Ruby Version Manager)
First, we install RVM (Ruby Version Manager) into our Debian Server. RVM will make it easy for you to install and manage Ruby on your system, especially when you use multiple Ruby versions.
Before proceeding, please install the gnupg2 package using the apt command below.
sudo apt install gnupg2
Next, use the following gpg command to import the GPGP key of the RVM software package.
gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2.69D6956105BD0E739499BDB
Download the RVM installer script and install it using the following command.
curl -sSL https://get.rvm.io | bash -s stable --ruby
After the installation is complete, load the RVM to the Debian system.
And Ruby version manager has been installed on Debian 10 system, please use the following “rvm” command to check.
As a result, RVM (Ruby Version Manager) xxx has been installed on Debian Buster 10.
Step 2-Install Ruby using RVM
In this step, we upgrade the RVM package to the latest version and install the latest stable version of Ruby as the default Ruby version on the system.
Use the following command to upgrade RVM to the latest stable version.
rvm get stable --autolibs=enable
After that, add the “root” user to the “rvm” group. In fact, the root user will be able to run the rvm command.
usermod -a -G rvm root
Next, use the following rvm command to install the latest stable version ‘Ruby 2.6’.
rvm install ruby-2.6
After the installation is complete, set Ruby 2.6 as the default Ruby version on Debian systems.
rvm --default use ruby-2.6
Now, use the following command to check the installed version of Ruby.
Therefore, the latest stable version of “Ruby 2.6” has been installed via RVM and used as the default Ruby on Debian 10 systems.
Step 3-Install Nodejs and Yarn
First, use the following apt command to install the development tools on the Debian system.
sudo apt install gcc g++ make
After that, add Nodejs 12’s Nodesource repository.
curl -sL https://deb.nodesource.com/setup_12.x | bash -
Then add Yarn GPG key and 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 updatesudo apt install -y nodejs yarn
After the installation is complete, use the following command to check.
node --versionyarn --version
Step 4-Upgrade RubyGem (Ruby Package Manager)
RubyGems is a Ruby package manager, provided with the gem command line tool, and is automatically installed when we install Ruby on the system. For this step, we upgraded RubyGem to the latest version.
Use the following command to upgrade RubyGem.
gem update --system
After completion, use the following command to check the gem version.
As a result, RubyGem has been upgraded to the latest version.
RubyGem is a package manager for installing Ruby packages. It will install the Ruby package and its documentation.
To reduce the installation of Ruby packages, you can disable the installation of Ruby package documentation through RubyGem configuration.
Add / create gem options on ~ / .gemrc configuration as shown below.
echo "gem: --no-document" >> ~/.gemrc
As a result, RubyGem will only install the core package of the Ruby package, without documentation.
Step 5-Install Ruby on Rails Stable
In this step, we install Ruby on Rails to the Debian system. We will install the latest version of Ruby on Rails v220.127.116.11.
Use the RubyGem command to install Ruby on Rails as follows.
gem install rails -v 6.0.2
After the installation is complete, check the following rails command.
As a result, Ruby on Rails 18.104.22.168 was installed on Debian Buster 10. You are ready to start a new application using Rails.
Step 6-Install and configure PostgreSQL database
In this tutorial, we will use PostgreSQL as the back-end database for Ruby on Rails.
Use the following apt command to install PostgreSQL to the Debian system.
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 postgresqlsystemctl enable postgresql
The PostgreSQL service is up and running.
Next, log in to the PostgreSQL Shell using the following command.
sudo -i -u postgres psql
Change the password of the default PostgreSQL user ‘postgres’.
Enter the new password for the “postgres” user.
Next, use the following PostgreSQL query to create a new PostgreSQL role named ‘rails_dev’ with ‘CreateDB’ privilege and password ‘aqwe123’.
create role rails_dev with createdb login password 'aqwe123';
The “created” privilege will allow the “rails_dev” user to create a new database for the Ruby on Rails project.
Now, use the following command for all available users on PostgreSQL.
You will get the ‘rails_dev’ user with the privilege to create a database and have the ‘CreateDB’ permission.
Enter “ExportTo exit the PostgreSQL Shell.
As a result, you have installed the PostgreSQL database on Debian buster 10. In addition, you have created a new PostgreSQL role ‘rails_dev’ with ‘CreateDB’ privilege for the ‘Ruby on Rails’ project.
Step 7-Use Ruby on Rails to start the project
In this step, we will create a new simple CRUD project using Ruby on Rails (with PostgreSQL as the back-end database).
Start Rails project with PostgreSQL
First, use the following rails command to start a new project named “myapp”.
rails new myapp -d postgresql
It will use PostgreSQL as the backend database to generate a project directory named “myapp”.
Now go to the “myapp” directory and edit the database configuration “config / database.yml”.
cd myapp/vim config/database.yml
In the “Development” section, add the PostgreSQL detailed information configuration below.
username: rails_dev password: aqwe123 host: localhost port: 5432
In the “Test” section, add the PostgreSQL details configuration below.
host: localhost port: 5432 username: rails_dev password: aqwe123
Save and close.
Next, generate the PostgreSQL database scheme and ensure that there are no errors.
rails db:setuprails db:migrate
After completing all the steps, use the following command to start the puma rails web server.
rails s -b 22.214.171.124 -p 8080
The Ruby on Rails project has been started and is running on port “8080”.
Open your web browser and type the server IP address using port “8080”.
You will get the default index.html page of the Rails project. In addition, it will detect your Rails deployment environment.
As a result, you have created a new project using Ruby on Rails (with PostgreSQL as the database backend).
Create a simple CRUD application
Next, we will generate a simple CRUD on the project “myapp”.
Use the following command to generate CRUD on Ruby on Rails.
rails g scaffold Post title:string body:textrake db:migrate
Use the following command to start the rails project.
rails s -b 192.168.1.10 -p 8080
Return to your web browser and check the rails project using the URL below.
Now, you will get the following simple CRUD application.
The following is the result after creating a sample post.
Finally, the installation of Ruby on Rails using PostgreSQL database on Debian Buster 10 has been successfully completed.