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:
Now we can use the rvm command to control the ruby 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:
Now version 2.5.1 is used by default.
Step 3 – Install Nodejs
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
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.
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
To change the postgres password do:
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:
Now check all the available roles in the system.
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.
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.
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:
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:
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
And start the puma server again:
rails s -b 192.168.1.10 -p 8080
And then open such an address in a browser:
We got a simple CRUD form:
This completes the installation of Ruby on Rails on Ubuntu 18.04 with the Postgresql database.