Share on Social Media

Learn how to complete a full Ruby on Rails setup on CentOS 8 with our step-by-step guide. Perfect for developers looking to create robust web applications. #centlinux #linux #ruby

What is Ruby on Rails?

Ruby on Rails, often simply referred to as Rails server, is a popular open-source web application framework written in the Ruby programming language. It is designed to make web development easier and more efficient by providing default structures for a database, a web service, and web pages.

Key features of Ruby on Rails include:

  1. MVC Architecture: Rails follows the Model-View-Controller (MVC) architectural pattern, which helps organize application programming by separating the code into three interconnected components: Models (handling data and business logic), Views (presenting data to users), and Controllers (handling user input and interaction).
  2. Convention over Configuration: Rails promotes the principle of “Convention over Configuration,” which means developers only need to specify unconventional aspects of their application. This reduces the number of decisions developers need to make and allows for quick setup and development.
  3. DRY Principle: Rails follows the “Don’t Repeat Yourself” (DRY) principle, encouraging code reuse and reducing redundancy.
  4. Scaffolding: Rails provides scaffolding, which automatically generates some of the models and views needed for a basic web application, speeding up the development process.
  5. Built-in Testing: Rails includes a comprehensive testing framework, making it easier to write and run tests for your application.
  6. Active Record: Rails features Active Record, an Object-Relational Mapping (ORM) layer that simplifies database interactions by mapping database tables to classes and rows to objects.
  7. Gem Libraries: Rails has access to a vast ecosystem of plugins and libraries called “gems,” which extend the functionality of the framework and simplify the addition of features to applications.

Rails server is used by many well-known websites and applications, such as GitHub, Shopify, and Basecamp, due to its ability to facilitate rapid development and maintainable code. It is a powerful tool for building modern, scalable web applications.

Recommended Online Training: One-stop Ruby on Rails: Build Web Applications from Scratch

369132 258f 5show?id=oLRJ54lcVEg&offerid=1074530.369132&bids=1074530

Ruby vs Ruby on Rails

Ruby and Ruby on Rails are related but distinct entities in the realm of web development:

Ruby

  • Programming Language: Ruby is a dynamic, interpreted programming language known for its simplicity and productivity.
  • Features: It emphasizes readability and simplicity, with elegant syntax that makes it enjoyable to write and easy to understand.
  • Use Cases: Ruby is versatile and used for various applications beyond web development, including scripting, automation, and data analysis.
  • Frameworks: While Ruby on Rails is the most famous framework built on Ruby, there are other frameworks like Sinatra and Hanami that cater to different needs.

Ruby on Rails (Rails Server)

  • Web Application Framework: Ruby on Rails, often called Rails, is a web application framework written in Ruby.
  • Purpose: It provides a structure for building dynamic web applications by enforcing conventions and principles like DRY (Don’t Repeat Yourself) and MVC (Model-View-Controller).
  • Features: Rails includes tools and libraries that automate many common web development tasks, such as database management, routing, and HTML/CSS rendering.
  • Popularity: Rails gained popularity for its ability to accelerate development, allowing developers to create functional prototypes and production-ready applications quickly.
  • Ecosystem: Rails has a robust ecosystem with a vast collection of gems (libraries) that extend its functionality, making it suitable for a wide range of web applications.

In Summary:

  • Ruby is the programming language itself, known for its simplicity and flexibility.
  • Ruby on Rails is a web application framework built with Ruby, providing structure and tools to streamline web development.

Understanding the distinction between Ruby as a language and Ruby on Rails as a framework helps developers choose the right tool for their specific development needs, whether building web applications or leveraging Ruby’s capabilities in other domains.

Also Read: How to install Ruby on Rails on Rocky Linux 9

Linux Server Specification

We are using a CentOS 8 minimal installed virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – CentOS Linux 8.0
  • Hostname – ruby-on-rails.centlinux.com
  • IP Address – 192.168.116.224 /24

Update Linux Software Packages

Connect with ruby-on-rails.centlinux.com as root user by using a SSH client.

Before starting any installation, it is a good practice to update your Linux distribution to the latest software packages.

In CentOS, the software packages are uploaded in online yum repositories as soon as they got updated. Therefore, you can easily update your Linux server by using a single dnf command.

# dnf update -y

Install Ruby on Rails Prerequisites

However, most of the dependencies are automatically resolved by the RVM (Ruby Version Manager) but there are some software packages that you have to install manually.

The first dependency of Ruby on Rails is Git. Git is available in standard yum repository, therefore you can install git on CentOS 8 as follows.

# dnf install -y git

Some components of Ruby on Rails requires Node.js, therefore, you are also required to install it.

Node.js is not available in standard yum repositories. However, Node.js can be installed from NodeSource yum repository.

Add NodeSource yum repository in your Linux server by executing following command.

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

## Installing the NodeSource Node.js 12.x repo...


## Inspecting system...

+ rpm -q --whatprovides redhat-release || rpm -q --whatprovides centos-release || rpm -q --whatprovides cloudlinux-release || rpm -q --whatprovides sl-release
+ uname -m

## Confirming "el8-x86_64" is supported...

+ curl -sLf -o /dev/null 'https://rpm.nodesource.com/pub_12.x/el/8/x86_64/nodesource-release-el8-1.noarch.rpm'

## As yum will try to install Node.js from the AppStream repository
instead of the NodeSource repository, the AppStream's version of Node.js has to be disabled.
## Run `sudo yum module enable -y nodejs` to reactivate the AppStream's Node.js repository.

+ yum module disable -y nodejs
Last metadata expiration check: 23:08:51 ago on Sat 25 Apr 2020 12:52:37 PM PKT.
Dependencies resolved.
================================================================================
 Package           Architecture     Version             Repository         Size
================================================================================
Disabling modules:
 nodejs

Transaction Summary
================================================================================

Complete!

## Downloading release setup RPM...

+ mktemp
+ curl -sL -o '/tmp/tmp.mwRVNDdser' 'https://rpm.nodesource.com/pub_12.x/el/8/x86_64/nodesource-release-el8-1.noarch.rpm'

## Installing release setup RPM...

+ rpm -i --nosignature --force '/tmp/tmp.mwRVNDdser'

## Cleaning up...

+ rm -f '/tmp/tmp.mwRVNDdser'

## Checking for existing installations...

+ rpm -qa 'node|npm' | grep -v nodesource

## Run `sudo yum install -y nodejs` to install Node.js 12.x and npm.
## You may also need development tools to build native addons:
     sudo yum install gcc-c++ make
## To install the Yarn package manager, run:
     curl -sL https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
     sudo yum install yarn

Now, you can install Node.js on CentOS 8 by using dnf command.

# dnf install -y nodejs

Besides Node.js, Ruby on Rails also required YARN (Package Manager). YARN is also provided by its official yum repository. Therefore, you should also add the YARN yum repository in your Linux server.

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

[yarn]
name=Yarn Repository 
baseurl=https://dl.yarnpkg.com/rpm/ 
enabled=1 
gpgcheck=1 
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg

Now, you can easily install yarn by executing dnf command.

# dnf install -y yarn

Installing MariaDB on CentOS 8

MariaDB Server 10.3 is available in standard yum repository, therefore, you can install it by using dnf command.

# dnf install -y mariadb-server

Enable and start MariaDB database service.

# systemctl enable --now mariadb.service
Created symlink /etc/systemd/system/mysql.service â /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/mysqld.service â /usr/lib/systemd/system/mariadb.service.
Created symlink /etc/systemd/system/multi-user.target.wants/mariadb.service â /usr/lib/systemd/system/mariadb.service.

Configure MariaDB database server and set root user password as follows.

# mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

Install RVM on CentOS 8

RVM (Ruby Version Manager) is a command line tool that allows you to easily install, manage work with multiple Ruby environments from interpreter to set of gems.

You should install RVM on your Ruby on Rails server and later you will install Ruby by using RVM commandline.

RVM is available in its official yum repository. Therefore, you have to add the RVM yum repository in your Linux operating system.

Before adding RVM yum repository, you are required to add the GPG key for the RVM repo as follows.

# gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: key 105BD0E739499BDB: 8 signatures not checked due to missing keys
gpg: /root/.gnupg/trustdb.gpg: trustdb created
gpg: key 105BD0E739499BDB: public key "Piotr Kuczynski <piotr.kuczynski@gmail.com>" imported
gpg: key 3804BB82D39DC0E3: 108 signatures not checked due to missing keys
gpg: key 3804BB82D39DC0E3: public key "Michal Papis (RVM signing) <mpapis@gmail.com>" imported
gpg: no ultimately trusted keys found
gpg: Total number processed: 2
gpg:               imported: 2

If you face problem during installation of GPG key, then you should refer to RVM official website.

Now, you can install RVM as follows.

# curl -sSL https://get.rvm.io | bash -s stable
Downloading https://github.com/rvm/rvm/archive/1.29.10.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.10/1.29.10.tar.gz.asc
gpg: Signature made Thu 26 Mar 2020 02:58:42 AM PKT
gpg:                using RSA key 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: Good signature from "Piotr Kuczynski <piotr.kuczynski@gmail.com>" [unknown]
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 7D2B AF1C F37B 13E2 069D  6956 105B D0E7 3949 9BDB
GPG verified '/usr/local/rvm/archives/rvm-1.29.10.tgz'
Creating group 'rvm'
Installing RVM to /usr/local/rvm/
Installation of RVM in /usr/local/rvm/ is almost complete:

  * First you need to add all users that will be using rvm to 'rvm' group,
    and logout - login again, anyone using rvm will be operating with `umask u=rwx,g=rwx,o=rx`.

  * To start using RVM you need to run `source /etc/profile.d/rvm.sh`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
  * Please do NOT forget to add your users to the rvm group.
     The installer no longer auto-adds root or users to the rvm group. Admins must do this.
     Also, please note that group memberships are ONLY evaluated at login time.
     This means that users must log out then back in before group membership takes effect!
Thanks for installing RVM ð
Please consider donating to our open collective to help us maintain RVM.

ð  Donate: https://opencollective.com/rvm/donate

However, you can also install latest stable Ruby and Rails along with the RVM. But we are installing each component separately for the sake of demonstration.

RVM environment script has been copied to /etc/profile.d/ directory. To set the RVM environment for the current session, execute it once.

# source /etc/profile.d/rvm.sh

RVM has been installed on CentOS 8.

Install Ruby on CentOS 8

Check and resolve any missing prerequisites for Ruby by using the rvm command.

# rvm requirements
Checking requirements for centos.
Installing requirements for centos.
Installing required packages: patch, autoconf, automake, bison, bzip2, gcc-c++, libffi-devel, libtool, make, patch, readline-devel, ruby, sqlite-devel, glibc-headers, glibc-devel........................
Requirements installation successful.

Check list of known versions of Ruby language.

# rvm list known
# MRI Rubies
[ruby-]1.8.6[-p420]
[ruby-]1.8.7[-head] # security released on head
[ruby-]1.9.1[-p431]
[ruby-]1.9.2[-p330]
[ruby-]1.9.3[-p551]
[ruby-]2.0.0[-p648]
[ruby-]2.1[.10]
[ruby-]2.2[.10]
[ruby-]2.3[.8]
[ruby-]2.4[.9]
[ruby-]2.5[.7]
[ruby-]2.6[.5]
[ruby-]2.7[.0]
ruby-head

# for forks use: rvm install ruby-head-<name> --url https://github.com/github/ruby.git --branch 2.2

# JRuby
jruby-1.6[.8]
jruby-1.7[.27]
jruby-9.1[.17.0]
jruby[-9.2.11.1]
jruby-head

# Rubinius
rbx-1[.4.3]
rbx-2.3[.0]
rbx-2.4[.1]
rbx-2[.5.8]
rbx-3[.107]
rbx-4[.12]
rbx-head

# TruffleRuby
truffleruby[-20.0.0]

# Opal
opal

# Minimalistic ruby implementation - ISO 30170:2012
mruby-1.0.0
mruby-1.1.0
mruby-1.2.0
mruby-1.3.0
mruby-1[.4.1]
mruby-2.0.1
mruby-2[.1.0]
mruby[-head]

# Ruby Enterprise Edition
ree-1.8.6
ree[-1.8.7][-2012.02]

# Topaz
topaz

# MagLev
maglev-1.0.0
maglev-1.1[RC1]
maglev[-1.2Alpha4]
maglev-head

# Mac OS X Snow Leopard Or Newer
macruby-0.10
macruby-0.11
macruby[-0.12]
macruby-nightly
macruby-head

# IronRuby
ironruby[-1.1.3]
ironruby-head

Install your required version of Ruby. We are installing MRI Ruby 2.7 as follows.

# rvm install 2.7
Searching for binary rubies, this might take some time.
No binary rubies available for: centos/8/x86_64/ruby-2.7.0.
Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
Checking requirements for centos.
Requirements installation successful.
Installing Ruby from source to: /usr/local/rvm/rubies/ruby-2.7.0, this may take a while depending on your cpu(s)...
ruby-2.7.0 - #downloading ruby-2.7.0, this may take a while depending on your connection...
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 14.0M  100 14.0M    0     0  1134k      0  0:00:12  0:00:12 --:--:-- 1211k
ruby-2.7.0 - #extracting ruby-2.7.0 to /usr/local/rvm/src/ruby-2.7.0.....
ruby-2.7.0 - #configuring........................................................................
ruby-2.7.0 - #post-configuration..
ruby-2.7.0 - #compiling............................................................................................
ruby-2.7.0 - #installing..................
ruby-2.7.0 - #making binaries executable...
Installed rubygems 3.1.2 is newer than 3.0.8 provided with installed ruby, skipping installation, use --force to force installation.
ruby-2.7.0 - #gemset created /usr/local/rvm/gems/ruby-2.7.0@global
ruby-2.7.0 - #importing gemset /usr/local/rvm/gemsets/global.gems................................................................
ruby-2.7.0 - #generating global wrappers.......
ruby-2.7.0 - #gemset created /usr/local/rvm/gems/ruby-2.7.0
ruby-2.7.0 - #importing gemsetfile /usr/local/rvm/gemsets/default.gems evaluated to empty gem list
ruby-2.7.0 - #generating default wrappers.......
ruby-2.7.0 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
Install of ruby-2.7.0 - #complete
Ruby was built without documentation, to build it run: rvm docs generate-ri

By using RVM, you can install multiple Ruby environments on the same platform. You just need to set a default version of Ruby, that is being used.

Set default version of Ruby language.

# rvm use 2.7 --default
Using /usr/local/rvm/gems/ruby-2.7.0

Verify the current Ruby version.

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

Ruby language has been installed on CentOS Linux 8.

Install Rails Server on CentOS 8

Now its time to install Rails on your Linux server. You can do this conveniently by using the gem (Ruby Package Manager).

# gem install rails

Verify installed version of Rails.

# rails -v
Rails 6.0.2.2

Ruby on Rails default service port is 3000/tcp. Therefore, you are required to allow this port in Linux firewall.

# firewall-cmd --permanent --add-port=3000/tcp
success
# firewall-cmd --reload
success

Installing Ruby Gems

Install mysql2 gem for Ruby connectivity with MariaDB/MySQL database servers.

# gem install mysql2
Fetching mysql2-0.5.3.gem
Building native extensions. This could take a while...
Successfully installed mysql2-0.5.3
Parsing documentation for mysql2-0.5.3
Installing ri documentation for mysql2-0.5.3
Done installing documentation for mysql2 after 0 seconds
1 gem installed

Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.

You can install Bundler by using the gem command.

# gem install bundler
Fetching bundler-2.1.4.gem
Successfully installed bundler-2.1.4
Parsing documentation for bundler-2.1.4
Installing ri documentation for bundler-2.1.4
Done installing documentation for bundler after 4 seconds
1 gem installed

Your Ruby on Rails server is configured and ready to use now.

Deploy your First Ruby Application

Create a HelloWorld Project to test your Ruby on Rails environment.

Following command will create the HelloWorld project directory with default skeleton of files and directories.

# rails new HelloWorld -d mysql
...
ââ serve-index@1.9.1
ââ serve-static@1.14.1
ââ sockjs-client@1.4.0
ââ sockjs@0.3.19
ââ spdy-transport@3.0.0
ââ spdy@4.0.2
ââ thunky@1.1.0
ââ type-is@1.6.18
ââ unpipe@1.0.0
ââ utils-merge@1.0.1
ââ wbuf@1.7.3
ââ webpack-dev-middleware@3.7.2
ââ webpack-dev-server@3.10.3
ââ websocket-extensions@0.1.3
ââ ws@6.2.1
ââ yargs-parser@11.1.1
ââ yargs@12.0.5
Done in 11.72s.

Set MariaDB database credentials in the database.yml file.

# cd HelloWorld
# vi config/database.yml

Under the default configurations set the root user password.

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

Create the database for your Ruby project by using rake command.

# rake db:create
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Created database 'HelloWorld_development'
Created database 'HelloWorld_test'

Start your Ruby on Rails application.

# rails server -b 0.0.0.0
Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.
The dependency tzinfo-data (>= 0) will be unused by any of the platforms Bundler is installing for. Bundler is installing for ruby but the dependency is only for x86-mingw32, x86-mswin32, x64-mingw32, java. To add those platforms to the bundle, run `bundle lock --add-platform x86-mingw32 x86-mswin32 x64-mingw32 java`.
Warning: the running version of Bundler (2.1.2) is older than the version that created the lockfile (2.1.4). We suggest you to upgrade to the version that created the lockfile by running `gem install bundler:2.1.4`.
=> Booting Puma
=> Rails 6.0.2.2 application starting in development
=> Run `rails server --help` for more startup options
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/stack.rb:37: warning: Using the last argument as keyword parameters is deprecated; maybe ** should be added to the call
/usr/local/rvm/gems/ruby-2.7.0/gems/actionpack-6.0.2.2/lib/action_dispatch/middleware/static.rb:110: warning: The called method `initialize' is defined here
Puma starting in single mode...
* 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

Open URL http://ruby-on-rails.centlinux.com:3000 in a web browser.

Ruby on Rails Default Webpage
Ruby on Rails Default Webpage

If you wish to learn more about the Ruby on Rails, then you should read Agile Web Development with Rails 6 (PAID LINK) by Pragmatic Bookshelf.

Conclusion – Ruby on Rails Setup

Thank you for following this guide on completing a Ruby on Rails setup on CentOS 8. I hope this tutorial has equipped you with the necessary steps to start developing robust web applications using Rails.

If you have any questions or need further assistance in setting up or configuring Ruby on Rails on Linux Server, feel free to reach out. I offer professional services on Fiverr that include Rails setup, customization, troubleshooting, and more. Check out my profile and services here: Linux System Administrator

Let’s build something great with Ruby on Rails!

Leave a Reply