How To Install Ruby on Rails on CentOS 7 / RHEL 7

4

ADVERTISEMENT

Install Ruby on Rails on CentOS 7
Install Ruby on Rails on CentOS 7

Ruby on Rails is an open-source web framework mainly used for creating highly powerfull websites and applications. It is written in Ruby.

Here, we will see how to install Ruby on Rails on CentOS 7 / RHEL 7 using,

1. rbenv (Recommended)

2. RVM (Ruby Version Manager)

Prerequisites

Install the development packages.

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

Install Node.js

We will install Node.js to provide a functionality of CoffeeScript and the Asset Pipeline in Rails, depend on a Javascript runtime.

Here, we will use the Long Term Supported version of Node.js for our Ruby on Rails installation.

If you want to make use of the latest feature, install Node.js v13.x.

curl -sL https://rpm.nodesource.com/setup_12.x | bash

yum install -y nodejs

Install Yarn

If you want to install the Yarn package manager, please perform the below steps.

curl -sL https://dl.yarnpkg.com/rpm/yarn.repo -o /etc/yum.repos.d/yarn.repo

yum install -y yarn

Install Ruby

Using rbenv (Recommended)

The rbenv provides an easy way to install and manage the versions of Ruby, and it is simpler than RVM.

To install rbenv, you have to run these commands.

cd
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

The above commands will install rbenv into your home directory and would set appropriate environment variables.

We will install the latest version of Ruby (v2.7.0). The installation process may take 15 to 20 minutes to complete, so please be patient.

rbenv install 2.7.0

If you want to install or use the different versions of Ruby, run the rbenv install <version> command with a Ruby version rbenv install -l.

Set Ruby v2.7.0 as the default version for all shells.

ADVERTISEMENT

rbenv global 2.7.0

Check the Ruby version.

ruby -v

Output:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux]

Install the bundler.

gem install bundler

Using RVM

RVM stands for Ruby Version Manager. It provides an efficient way to install and manage ruby versions independently. We will use it to install the latest version of Ruby on your system.

Before installing RVM on your system, import public key in your system and then use curl to install RVM in your system.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB

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

After installing RVM, we need to load the RVM environment variable using the below command.

source /etc/profile.d/rvm.sh

With RVM, we can install and manage multiple ruby versions on the single system.

Use the following command to install Ruby 2.7.0.

rvm install 2.7.0

You can also install multiple versions of ruby using the rvm install <version> command.

Run the following command to set version 2.7.0 as the default version of Ruby in case your machine has multiple versions of Ruby.

rvm use 2.7.0 --default

Output:

Using /usr/local/rvm/gems/ruby-2.7.0

Check the current ruby version is used.

ruby -v

Output:

ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux

Install the bundler.

gem install bundler

Install Rails

Use the following command to install the latest stable release of Rails. This process may take a while, be patient with it.

ADVERTISEMENT

gem install rails

Check the rails version.

rails -v

Output:

Rails 6.0.2.1

You can also install a specific version of rails by using gem install rails –version=<version> command.

Create Test Application

Once you have installed Ruby on Rails, we will now create a test application with MySQL support to validate it.

Install Database

Rails uses sqlite3 as its default database, which is not recommended to use in a production environment where you have high traffic to web applications. You’ll probably want to go with MariaDB or PostgreSQL.

CentOS 7 / RHEL 7 ships MariaDB v5.5 which is nearing End-Of-Life. So, consider using MariaDB v10.4 from the MariaDB community.

For example, if you want to use MariaDB as your database, install it.

READ: How To Install MariaDB v10.4 on CentOS 7 / RHEL 7

After installing MariaDB, install the development and shared libraries.

yum install -y MariaDB-devel MariaDB-shared

Install the mysql2 extension.

gem install mysql2

Create Rails Application

Create a new application in your home directory.

cd ~

rails new itzgeekapp -d mysql

cd itzgeekapp

Edit your application’s database configuration config/database.yml file.

vi config/database.yml

Update the database username and password.

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root # MySQL User
  password: password # MySQL Password
  socket: /var/lib/mysql/mysql.sock

Create the database.

rake db:create

Output:

Created database 'itzgeekapp_development'
Created database 'itzgeekapp_test'

Firewall

By default, Rails applications listen on to port 3000. We need to configure a firewall to allow it so that users can access from external machines.

ADVERTISEMENT

firewall-cmd --permanent --add-port=3000/tcp

firewall-cmd --reload

Validate Application

Start your Rails application from your application directory.

rails server -b 0.0.0.0

Output:

=> Booting Puma
=> Rails 6.0.2.1 application starting in development
=> Run `rails server --help` for more startup options
* Version 4.3.3 (ruby 2.7.0-p0), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

If everything is working properly, your Rails application should be running on port 3000.

Visit your Rails application by typing the below URL in your web browser.

http://localhost:3000

OR

http://your.ip.add.ress:3000

You should get the following page.

Ruby On Rails On CentOS 7
Ruby On Rails On CentOS 7

Conclusion

That’s All. You have successfully installed install Ruby on Rails on CentOS 7 / RHEL 7.

You might also like