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
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.
cd git clone https://github.com/rbenv/rbenv.git ~/.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://github.com/sstephenson/ruby-build.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: 1.8.5-p113 1.8.5-p114 1.8.5-p115 1.8.5-p231 1.8.5-p52 . . . . 2.3.0-preview1 2.3.0-preview2 2.3.0 2.3.1 2.4.0-dev 2.4.0-preview1
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.
You will see output similar to this.
2.2.1 * 2.3.1 (set by /root/.rbenv/version)
- Shows the current default version of Ruby in your system.
You can also check the current version of Ruby on your shell using the following command.
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.
[[email protected] ~]# rbenv uninstall 2.2.1 rbenv: remove /root/.rbenv/versions/2.2.1? y
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.
Once done, you can check if Rails is installed correctly using the following command.
The above command will show you the version of Rails installed in your system, you will see output similar to this.
[[email protected] ~]# rails -v Rails 5.0.0
To install Node.js on CentOS, run the following command.
yum -y install epel-release yum -y install nodejs
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 password: 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.
You will see following output.
[[email protected] 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.
If you wish to change your port from
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.
And append these lines at the end of your file.
require 'rails/commands/server' module Rails class Server def default_options super.merge(Host: '0.0.0.0', Port: 80) end end end
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
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.