22nd July, 2016 | Tutorials |

How to Install Ruby on Rails on CentOS 7

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.

Get a Cloud Server

Ruby on Rails is a very popular open source web application framework written in Ruby. It is designed to make programming web applications easier by making assumptions about what every developer needs to get started. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web pages. Rails encourages the use of web standards like JSON and XML for data transfer, HTML, CSS and JavaScript for user interface.

In this tutorial we will learn to install install Ruby on Rails on CentOS 7. For installing Ruby we will use rbenv.


For installing Ruby on Rails, you will need to have a server with CentOS 7 installed. Additionally you will need root access on your server. In this tutorial we will be using the root account to run the commands. If you are not logged in as the root user, you can either use sudo command before all the commands, or you can use su command to login as root.

Installing Ruby

Before installing any package, it is a good practice to update the system and it's repositories using the following command.

    yum -y update

Once the system is updated, we can proceed with the installation of rbenv by installing the required packages using the following command.

    yum -y install 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

The above command will install Git along with some tools to compile the source and install Ruby. Now run the following command as the user in which you want to install Ruby, as rbenv separately install Ruby on each user account. Run the following command to install rbenv.

    git clone ~/.rbenv
    echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
    echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
    source ~/.bash_profile

The above commands will download and install rbenv and then it will set an environment variable path to rbenv, next it will configure your system to start automatically and finally it will apply the changes immediately.

Next fetch Ruby builds so that you can install it with rbenv. Run the following commands.

    git clone git:// ~/.rbenv/plugins/ruby-build
    echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bash_profile
    source ~/.bash_profile

The above commands will fetch Ruby builds using git. Now as we have installed rbenv, we can install latest version of Ruby.

    rbenv install -l

The above command will display a list of available Ruby versions, a snapshot of the above command is shown below.

    Available versions:
      . . . .

As we can see on the list that the latest stable version is 2.3.1, you can install Ruby using the following command.

    rbenv install 2.3.1

You can install as many version of Ruby you want using the above command. rbenv keeps track of the versions separately. To make a version of Ruby as global Ruby versions for all the shells in the system, run the following command.

    rbenv global 2.3.1

If you want to use another version of Ruby for your local shell, you can run the following command.

    rbenv local 2.2.1

This will override the global configuration for your local shell, you can now use other version of Ruby. To unset the local version of Ruby back to the global version, you can unset the local version using the following command.

    rbenv local --unset

You can view the list of all the Ruby versions installed on your system using the following command.

    rbenv versions

You will see output similar to this.

      * 2.3.1 (set by /root/.rbenv/version)

You can also check the current version of Ruby on your shell using the following command.

    ruby -v

You will see following output.

    ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]

By default ruby gems will install documentation for each package we install in our system. If you do not want the documentation to be installed, you can run following command.

    echo "gem: --no-ri --no-rdoc" > ~/.gemrc

You can also install gem bundler which is used to manage application dependencies using the following command.

    gem install bundler

You will get following output.

    Fetching: bundler-1.12.5.gem (100%)
    Successfully installed bundler-1.12.5
    1 gem installed

You can also remove a Ruby version from your system using following command.

    rbenv uninstall 2.2.1

The above command will remove the specified version of Ruby from your system, You will see following output.

    [root@liptan ~]# rbenv uninstall 2.2.1
    rbenv: remove /root/.rbenv/versions/2.2.1? y

Installing Rails

To install Rails on your system you can run the following command.

    gem install rails

The above command will install latest version of Rails into your system. After installing the any package through gems it is important to run the following command so that it can install shims for all Ruby executables known to rbenv, which will allow you to use the executables.

    rbenv rehash

Once done, you can check if Rails is installed correctly using the following command.

    rails -v

The above command will show you the version of Rails installed in your system, you will see output similar to this.

    [root@liptan ~]# rails -v
    Rails 5.0.0

Install JavaScript Runtime

Few features of Rails depends on JavaScript runtime, for example Asset pipeline or Coffeescript. We can provide JavaScript runtime to Rails using Node.js. You can read more about installing Node.js in this step by step tutorial.

To install Node.js on CentOS, run the following command.

    yum -y install epel-release
    yum -y install nodejs

Installing Database

By default Rails uses SQLite as its default database, but it is recommended that on production environment you should use either MySQL or PostgreSQL as SQLite is not capable of handling high traffic and huge data size. In this example we are going to use MySQL as database server for Rails application. To install MySQL in your system run the following commands.

    yum -y install mariadb-server mariadb-devel

Now start and enable MariaDB using following command.

    systemctl start mariadb
    systemctl enable mariadb

Now secure your MariaDB installation using the following command.


It will ask you for various configurations during script run, provide the information accordingly.

Now finally install MySQL adaptor to connect Rails with MySQL server using the following command.

    gem install mysql2
    rbenv rehash

Creating a New Application

To create a new Ruby on Rails application, run the following command.

    cd ~
    rails new myrailsapp -d mysql

In this example we have taken our app name as myrailsapp, but you can use any name according to your choice.

If you want to create the database of your new application using SQLite, do not use -d mysql argument while creating your app. You can simply use rails new myrailsapp.

If you have choosed to use MySQL database then you will need to edit config/database.yml file to provide database server credentials. Use the following commands to edit the file.

    cd myrailsapp
    nano config/database.yml

If you don't have nano installed, you can run yum -y install nano to install nano editor. Once you open the file in your favorate editor, find the following lines.

    default: &default
      adapter: mysql2
      encoding: utf8
      pool: 5
      username: root
      socket: /var/lib/mysql/mysql.sock

You will need to provide the MySQL root password you have created during securing your MySQL installation. Once done save the file and run the following command to create a database.

    rake db:create

You will see following output.

    [root@liptan myrailsapp]# rake db:create
    Created database 'myrailsapp_development'
    Created database 'myrailsapp_test'

Now start your Rails server using the following command.

    rails server --binding=Your-Server-IP

The above command will start your Rails server on port number 3000, you can go to the following link into your favorite browser to access your Rails application.


You will see following page in browser.

HP_NO_IMG/data/uploads/users/2a78e75d-343a-47ce-9d84-14a6ba54abbc/872169384.png" alt="" />

If you wish to change your port from 3000 to 80, on which default http runs, you can do so by editing config/boot.rb file of your application. Open the file in your favorite editor.

    nano config/boot.rb

And append these lines at the end of your file.

    require 'rails/commands/server'
    module Rails
      class Server
        def default_options
          super.merge(Host:  '', Port: 80)

Now you can run your server again using the same command

    rails server --binding=Your-Server-IP

or you can simply run your server using rails server as we have already configured host in config/boot.rb file. You can now go back to your browser and go to the following address.


You will see the same page again, this time without using the port number because by default http runs on port 80.


In this tutorial we have installed Ruby using rbenv also we have installed Rails using gem in CentOS 7. You can now easily set up both development and production environments using Ruby on Rails.

Want your very own server? Get our 1GB memory, Xeon V4, 25GB SSD VPS for £10.00 / month.

Get a Cloud Server