Installing Ruby on Rails on Ubuntu 18.04

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

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

Installing ruby on rails on Ubuntu 18.04

Step 1. Install Ruby Version Manager

RVM (Ruby Version Manager) is a command line tool based on Bash and Ruby and designed for ruby. RVM allows you to install and configure multiple ruby versions in one system.

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

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

Install stable version of rvm, run the command below:

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

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

source /usr/local/rvm/scripts/rvm

Now we can use rvm to control the ruby version:

rvm version

Step 2. Installing latest Ruby version

The latest version of ruby is currently considered Ruby 2.5.1, and it will be automatically installed during installation of rvm, if there is no package ruby. Next, we will configure a version of ruby in Ubuntu. Update your rvm to the latest stable version.

rvm get stable --autolibs=enable

usermod -a-G rvm root

Now check all the available versions of ruby:

rvm list known

We get a lot of available ruby versions 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 2.5.1 ruby version by default:

rvm --default use ruby-2.5.1

Can check the version of ruby:

ruby -v

Now the version 2.5.1 is the default.

Step 3 – Install Nodejs

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

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

Then install the latest version of nodejs and 10 additional packages using apt.

sudo apt install-y nodejs

sudo apt install gcc g++ make

Step 4 – Adjust The Ruby Gem

RubyGems – the package Manager in Ruby, packaged as a command line tool gem. It is automatically installed when you install Ruby on the system. Update gem to the latest version and check it out:

gem update --system

gem -v

Note: you Can disable the installation of documentation for each ruby gem. Simply run:

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 in Ubuntu using the package Manager and ruby gem with the command:

gem install rails -v 5.2.0

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

rails -v

Step 6. Setup PostgreSQL for Rails

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

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

After installation is complete, start the Postgres service and let it run every time you boot the system:

systemctl start postgresql

systemctl enable postgresql

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

su - postgres

psql

To change the password for postgres, run:

password postgres

Now create a new user for the installation of rails, for example, with the name rails_dev, with the privilege of creating a database with 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 user rails_dev in the list.

The installation and setup PostgreSQL for Rails Development is completed.

Step 7. The first Rails application

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

rails new myapp -d postgresql

After you run the command will appear in the directory “myapp”, navigate to this directory and edit the database configuration file database.yml file using vim editor.

cd myapp/

vim config/database.yml

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

username: rails_dev
password: aqwe123
host: localhost
port 5423

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

rails db:setup

rails db:migrate

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

rails s-b 192.168.1.10 -p 8080

The first rails project will be launched on the IP address of “192.168.1.10” 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 receive the project’s home page rails by default, as shown below.

Next, let’s create a simple CRUD application with a PostgreSQL database on Rails. Give the following commands:

rails g scaffold Post title:string body:text

rake db:migrate

And restart the puma server:

rails s-b 192.168.1.10 -p 8080

And then open this address in your browser:

http://192.168.1.10:8080/posts/

We got a simple CRUD form:

The installation of Ruby on Rails on Ubuntu 18.04 database Postgresql has been successfully completed.

Source: losst.ru

(Visited 22 times, 1 visits today)