Ruby on Rails Installation | Guide for Installing Ruby on Rails - W3School

Before you start developing a web application with the help of Ruby on Rails framework, you need to have a list of software installed in your system. The list is given below –

* Ruby,
* Ruby on Rails framework,
* a web server, and
* a database system.

We are assuming that you are already done with the installation of the web server and the required database system. Well, you can also use the server offered by the Ruby, which is, WEBrick Web Server. But generally, most of the web applications use other web servers such as Apache and Lighttpd, whenever the application is in production.

Ruby on Rails supports a number of database systems. All of these are listed below –

* MySQL
* PostgreSQL
* SQLite
* Oracle
* DB2, and
* SQL Server

To set up your database to be used by Rails, you can refer to the corresponding Database System Setup manual.

In the below provided sections, we will be helping you to install Rails framework on two operating systems, which are –
* Windows, and
* Linux

How to Install Ruby on Rails Framework on Windows?
For the installation of Rails framework on Windows, follow the given steps.

Step 1: Check the version of Ruby
Begin by looking for a pre-installed Ruby on your system. For that, open the command prompt (or cmd) on your system and type in the following command –
ruby -v

If the respond of the Ruby is its installed version (such as version 2.2.2 or above) then type in another command, which is –
gem –version 

In case, you are displayed with an error only then, you are required to go through the Install Ruby step. Otherwise, you can directly jump to the third step of this whole process.

Step 2: Installation of Ruby
This step is needed to be followed only when Ruby is not installed on your system. You need to start by installing a package from the website: https://rubyinstaller.org/. Under this web page, follow the provided download link. After that, run the resulting installer. This installer will be an .exe (executable) file, i.e., rubyinstaller-2.2.2.x.exe. The installation of this executable file will start in a single click. It is a light weight package which will carry RubyGems along with it. For more details, you can check for the Release Notes.

Step 3: Installation of Ruby on Rails Framework
Once you the RubyGems are loaded, now, you can install all of Ruby on Rails and its dependencies by typing in the following command on the command prompt –
C:\> gem install rails

 

Note – The above command will take time for the installation of all the Rails and its dependencies. It is important that your system keep a continuous connection with the internet during the installation of the gems dependencies.

Step 4: Check the version of Ruby on Rails
To check the version of Rails, type in the below provided command on the command prompt of your system –
C:\> rails -v 

Output –
Rails 4.2.4 

How to Install Ruby on Rails Framework on Linux?
For the installation of Ruby on Rails framework on Linux, follow the given steps. To install Rails on Linux, you will be making use of rbenv tool. Rbenv is actually a light weight Ruby Version Management Tool. This tool is capable of providing the easiest installation procedure which manages all the available versions of Ruby. It also provides a user friendly and concrete environment for the development of web applications on Rails.

The following steps will lead you to the installation of Ruby on Rails using rbenv.

Step 1: Installation of Pre-requisite Dependencies
You need to start by installing git – core. You also need to install a few of the dependencies which will eventually help in the installation process of Ruby on Rails. Type in the following command to install Ruby on Rails dependencies by making use of yum repository –

tp> sudo yum install -y git-core zlib zlib-devel gcc-c++ patch readline readline-devel libyaml-devel libffi-devel openssl-devel make bzip2 autoconf automake libtool bison curl sqlite-devel

Step 2: Installation of rbenv tool
Now, it’s time to install rbenv tool. With that, you are also required to set the appropriate environment variables. The following set of commands will help you to get the rbenv tool for the git repository.

tp> git clone git://github.com/sstephenson/rbenv.git .rbenv
tp> echo ‘export PATH = “$HOME/.rbenv/bin:$PATH”‘ >> ~/.bash_profile
tp> echo ‘eval “$(rbenv init -)”‘ >> ~/.bash_profile
tp> exec $SHELL 
tp> git clone git://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
tp> echo ‘export PATH = “$HOME/.rbenv/plugins/ruby-build/bin:$PATH”‘ << ~/.bash_profile
tp> exec $SHELL

Step 3: Installation of Ruby
It is important that you determine the version of Ruby you want to install. In here, we will be installing the Ruby version 2.2.3. Type in the following command on the command prompt which will help you to install Ruby –
tp> rbenv install -v 2.2.3 

This another command will help you to set up the current installed Ruby version as the default one –
tp> rbenv global 2.2.3 

The given command will verify the version of the installed Ruby –
tp> ruby -v 

Output –
ruby 2.2.3p173 (2015-08-18 revivion 51636) [X86_64-linux] 

For installing the supported dependencies, Ruby offers a keyword gem. In case, you want to skip the installation of documents of Ruby – gems then type in the following command on the command prompt of your system –
tp> echo “gem: –no-document” > ~/.gemrc 

After this, install the Bundler gem. This gem will help you in managing the dependencies of your web application. The following command will install the bundler gem –
tp> gem install bundler 

Step 4: Installation of Ruby on Rails
In this step, the below provided command will install the 4.2.4 version of Rails to get installed in your system –
tp> install rails -v 4.2.4 

To make this newly installed Ruby on Rails executable available, follow the given command –
tp> rbenv rehash 

To check the Rails version, type in the following command –
tp> rails -v 

Output –
tp> Rails 4.2.4 

For the management of features offered by Rails, the Ruby on Rails framework needs to have the JavaScript Runtime Environment (Node.js). In the next step, we will help you understand how to use Node.js for managing the Asset Pipeline. Asset Pipeline is another feature offered by Ruby on Rails. 

Step 5: Installation of JavaScript Runtime
To install Node.js, you are required to use the yum repository. In here, we will be making use of EPEL yum repository for Node.js. The following command will help you to add the EPEL package to the yum repository –
tp> sudo yum -y install epel-release 

The command will install the Node.js package by making use of the yum repository-
tp> sudo yum install nodejs
Step 6: Installation of database

There are a number of databases supported by Ruby on Rails. All of them are listed below –

* MySQL,
* PostgreSQL,
* SQLite, and
* other RDBMS (Relational Database Management System).

Well, by default, Ruby on Rails uses sqlite3. This database installation step is completely optional. So, in case, if you already have any database installed then you can skip this step. Also, it is not mandatory that you need to install a database before you start the Ruby on Rails server. You can perform this step whenever required. In this tutorial, we will be using PostgreSQL database throughout. Now, install the PostgreSQL, type in the following command on the command prompt of your system –

tp> sudo yum install postgresql-server postgresql-contrib 

Type in y to accept the prompted message on the terminal. After this, you are required to create a PostgreSQL database cluster by using the following command –
tp> sudo postgresql-setup initdb

The following commands will help you to start and enable the PostgreSQL database –

To start the PostgreSQL database: tp> sudo systemctl start postgresql
To enable the PostgreSQL database: tp> sudo systemctl enable postgresql

How to Keep Ruby on Rails Updated?
We are assuming that you have followed the above mentioned steps only (or in particular, you have used RubyGems for the installation of Rails) then it would be a really easy task for you to keep Rails updated for all time. The following command will help you to do that –

tp> gem update rails

Output –
Though the following screenshot is of Window’s command prompt. But this output will be the same for Linux’s terminal.

The command will help you to automatically update your Ruby on Rails installation. And this upgradation to work properly, you need to restart your system after the successful completion of the Rails upgradation.

Note – Keep in mind that you need to be connected to the internet during the processing of this command.

Installation Verification
You can always check if the installed Ruby on Rails is set up as per your requirements or not. The given command will help you to create a demo project –

tp> rails new demo

Output –
The final output of the above command will appear as –

The above code will generate a demo Rails project that we will be discussing later in the module. Well, the above command will also be used to check whether the Rails environment is set up properly or not. After this, you are required to type in the following 2 commands to run WEBrick web server on your system.

tp> cd demo
tp> rails server

The above command will generate an auto-code which will lead to the starting of the web server.

The next thing you have to do is open your web browser and type in the given URL: http://localhost:3000.

The display screen will prompt a message “Welcome abroad”. You can also get another message such as “Congratulations” indicating the successful installation and set up of the Rails on your system.