0

Ruby on Rails (RoR) is an open source web application platform published under the MIT license. It is a server-side web application and follows the concept of MVC (Model-View-Controller). Rails provides presets for creating databases, web services, and web pages. More than 3,000 developers have contributed to the development of the Rails environment. There are many well-known Rails-based web applications such as Github, Airbnb, Soundcloud, etc.

In this article, I will show how to install Ruby on Rails on Ubuntu 18.04 LTS. You will also learn how to set up Rails with a PostgreSQL database, and how to create your first Rails project.

The content of the article:

 

 

Installing ruby ​​on rails on Ubuntu 18.04

Step 1. Installing Ruby Version Manager

RVM (Ruby Version Manager) is a Bash and Ruby-based command line tool for managing ruby ​​installation. RVM allows you to install and configure multiple versions of ruby ​​on the same system.

First install the rvm packages using the installer script. Add the rvm key to the system:

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E37D2BAF1CF37B13E2069D6956105BD0E739499BDB

Install the stable version of rvm, run the command below:

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

The command will automatically install the necessary packages and install the latest stable version of rvm. After installation is complete, run the following command:

source /usr/local/rvm/scripts/rvm

Now we can use the rvm command to control the ruby ​​version:

rvm version

Step 2. Install the latest version of Ruby

The latest version of ruby ​​is currently considered Ruby 2.5.1, and it will be automatically installed during the installation of rvm, if the system does not have a ruby ​​package. Next, we will configure the ruby ​​version in the Ubuntu operating system. Update rvm to the latest stable version.

rvm get stable --autolibs=enable

usermod -a -G rvm root

Now check out all available ruby ​​versions:

rvm list known

We will get many available versions of ruby ​​and install the latest stable version of Ruby using the rvm command, as shown below.

rvm install ruby-2.5.1

After installation is complete, make ruby ​​2.5.1 the default version:

rvm --default use ruby-2.5.1

You can check the ruby ​​version:

ruby -v

Now version 2.5.1 is used by default.

Step 3 – Install Nodejs

Ruby on Rails takes time to compile the Rails resource pipeline into JavaScript. For Rails developers on Ubuntu Linux, it is best to install and use Nodejs as the Javascript runtime. Add the nodejs Node repository to the system.

curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -

After that, install the latest version of nodejs 10 and additional packages using the apt command.

sudo apt install -y nodejs
sudo apt install gcc g++ make

Step 4 – Configure Ruby Gem

RubyGems is a Ruby package manager delivered as a command line tool gem. It is automatically installed when Ruby is installed on the system. Update gem to the latest version and check it:

gem update --system
gem -v

Note: You can disable the installation of documentation for each ruby ​​package. Just do:

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

Step 5. Installing Ruby on Rails

In this guide, we will use the latest stable version of Ruby on Rails 5.2.0. Install Rails Ubuntu using the gem ruby ​​package manager with the command:

gem install rails -v 5.2.0

After installation is complete, you can check the rails version.

rails -v

Step 6. Configuring PostgreSQL for Rails

By default, Ruby on Rails uses a SQLite database. In addition, it supports many database systems, including MySQL, SQLite, and PostgreSQL. We will use PostgreSQL. Install the PostgreSQL database using the apt command:

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

After the installation is complete, start the Postgres service and allow it to start each time the system boots:

systemctl start postgresql
systemctl enable postgresql

Next, configure the password for the user postgres and create a user to install Rails. To do this, log in as the user ‘postgres’ and run the shell postgres:

su - postgres
psql

To change the postgres password do:

password postgres

Now create a new user to install rails, for example, with the name rails_dev, with the privilege of creating a database and the password “aqwe123”. To do this, run the following query:

create role rails_dev with createdb login password 'aqwe123';

Now check all the available roles in the system.

du

You will see the user rails_dev in the list.

This completes the installation and configuration of PostgreSQL for Rails Development.

Step 7. First Rails Application

Ruby on Rails has a team railsdesigned to customize your application. Create a new project myapp with the default PostgreSQL database by running the rails command as shown below.

rails new myapp -d postgresql

After the command is executed, the “myapp” directory will appear, go to this directory and edit the database configuration file database.yml using the vim editor.

cd myapp/
vim config/database.yml

This file contains the following sections: development, testing and production. In the development section, uncomment the lines below and change their value to the data that you used to create the postgresql user.

username: rails_dev
password: aqwe123
host: localhost
port 5423

For the testing section, do a similar operation. Save the changes and close the editor. Now create a database and make sure there are no errors:

rails db:setup
rails db:migrate

When all settings are complete, start the puma rails web server using the command below:

rails s -b 192.168.1.10 -p 8080

The first rails project will run on the IP address “192.168.1.10” with port 8080.

Open a web browser and enter the IP address of the server in the address bar:

http://192.168.1.10:8080/

You will get the default rails project home page as shown below.

Next, we will create a simple CRUD application with a PostgreSQL database on Rails. Run the following commands:

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

And start the puma server again:

rails s -b 192.168.1.10 -p 8080

And then open such an address in a browser:

http://192.168.1.10:8080/posts/

We got a simple CRUD form:

This completes the installation of Ruby on Rails on Ubuntu 18.04 with the Postgresql database.

How to Configure Matrix Synapse Chat Server and Riot client

Previous article

What to do when Laptop Battery does not Charges?

Next article

You may also like

Comments

Leave a reply

Your email address will not be published. Required fields are marked *

More in How To