Saturday, December 14, 2024
Google search engine
HomeGuest BlogsInstall Ruby on Rails on Rocky Linux 9 / AlmaLinux 9

Install Ruby on Rails on Rocky Linux 9 / AlmaLinux 9

.tdi_3.td-a-rec{text-align:center}.tdi_3 .td-element-style{z-index:-1}.tdi_3.td-a-rec-img{text-align:left}.tdi_3.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_3.td-a-rec-img{text-align:center}}

Ruby on Rails also known as Rails is an open-source server-side web development application framework written in the Ruby programming language. With the complexity of modern web applications, Rails makes web development easier with the pre-built structure for development, providing all the required tools to build an app.

Ruby on rails is preferred due to:

  • Free, open-source, and cross-platform. This makes it easier to get started at any time on any platform
  • Its simple syntax is designed to be close to the English language. This reduces the time spent on memorizing the meaning of symbols in code.
  • The large community of developers helps beginners by answering the questions. Due to its popularity, several developers provide solutions to problems on the internet.
  • Rails is a full-stack framework that makes it easier to build an application in a short time.
  • Active Records make modeling easy. The modeling associations between tables, providing callbacks when saved, encrypting sensitive data seamlessly, and expressing SQL queries beautifully.
  • Action Dispatch route URLs that are used to configure how URLs connect to the controllers using the routing domain language
  • Action Controllers handle all requests that include exposing the domain model to the web, processing incoming parameters, setting caching headers, and rendering templates, responding with either HTML or JSON.

Ruby on Rails is used in all industries that develop web applications and services. These include applications such as marketing websites, eCommerce sites, CMSs, and custom web applications. The companies that make use of Ruby on Rails include Soundcloud, Yammer, Github, Zendesk, Square, Crunchbase, Airbnb, Shopify, Hulu, Basecamp e.t.c

.tdi_2.td-a-rec{text-align:center}.tdi_2 .td-element-style{z-index:-1}.tdi_2.td-a-rec-img{text-align:left}.tdi_2.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_2.td-a-rec-img{text-align:center}}

In this guide, we will learn how to install Ruby on Rails on Rocky Linux 9 / AlmaLinux 9.

Getting started

Begin by updating your system and the packages to the latest available versions:

sudo dnf update -y

Once updated, install the required packages

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

#1. Install Ruby on Rails on Rocky Linux 9 / AlmaLinux 9

In this guide, I will cover 3 ways how to install Ruby on Rocky Linux 9 / AlmaLinux 9. These methods are:

  • Using Appstream Repository
  • Using RVM
  • Using rbenv

Option 1 – Install Ruby using AppStream Repository

Ruby exists in the Rocky Linux 9 / AlmaLinux 9 Appstream repository and can be installed using the command:

sudo dnf install ruby ruby-devel

Dependency Tree:

Transaction Summary
================================================================================
Install  10 Packages

Total download size: 4.4 M
Installed size: 16 M
Is this ok [y/N]: y

Once complete, verify the installation:

$ ruby --version
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

Option 2 – Install Ruby using RVM

Ruby Version Manager commonly abbreviated as RVM is a versatile CLI tool that makes it easier to install and manage Ruby environments.

To use this method, we need to download and install the RVM startup script

curl -sSL https://rvm.io/pkuczynski.asc | gpg2 --import -
curl -sSL https://rvm.io/mpapis.asc | gpg2 --import -
sudo dnf -y install tar
curl -sSL https://get.rvm.io | bash -s stable

Sample Output:

Downloading https://github.com/rvm/rvm/archive/1.29.12.tar.gz
Downloading https://github.com/rvm/rvm/releases/download/1.29.12/1.29.12.tar.gz.asc
gpg: Signature made Fri 15 Jan 2021 07:46:22 PM CET
gpg:                using RSA key 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
gpg: Good signature from "Piotr Kuczynski <[email protected]>" [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 '/home/rocky9/.rvm/archives/rvm-1.29.12.tgz'
Installing RVM to /home/rocky9/.rvm/
    Adding rvm PATH line to /home/rocky9/.profile /home/rocky9/.mkshrc /home/rocky9/.bashrc /home/rocky9/.zshrc.
    Adding rvm loading line to /home/rocky9/.profile /home/rocky9/.bash_profile /home/rocky9/.zlogin.
Installation of RVM in /home/rocky9/.rvm/ is almost complete:

  * To start using RVM you need to run `source /home/rocky9/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
Thanks for installing RVM 🙏
Please consider donating to our open collective to help us maintain RVM.

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

Update the system environment

source /etc/profile.d/rvm.sh

Now reload RVM.

rvm reload

Install all the required packages:

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

List the installable versions:

$ 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[.10]
[ruby-]2.5[.8]
[ruby-]2.6[.6]
[ruby-]2.7[.2]
[ruby-]3[.0.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.14.0]
jruby-head
.........

Install the preferred version of Ruby, example install

rvm install ruby <VERSION>

Check the Ruby version.

$ ruby --version
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]

Option 3 – Install Ruby using rbenv

You can also use rbenv to install Ruby on Rocky Linux 9 / AlmaLinux 9. But first, navigate to the user home directory:

sudo dnf -y install git
cd ~

Now git clone rbenv and set PATH variables

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

Also git clone Ruby-build and set the PATH variables

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

Now list the available versions of Ruby.

$ rbenv install -l
2.6.10
2.7.6
3.0.4
3.1.2
jruby-9.3.7.0
mruby-3.1.0
picoruby-3.0.0
rbx-5.0
truffleruby-22.2.0
truffleruby+graalvm-22.2.0

Only latest stable releases for each Ruby implementation are shown.
Use 'rbenv install --list-all / -L' to show all local versions.

Install the preferred Ruby version.

rbenv install -v 3.1.2
rbenv rehash

View the installed versions of Ruby;

$ rbenv versions
3.1.2

#2. Setting Ruby Versions on Rocky Linux 9 / AlmaLinux 9

It is possible to have multiple Ruby versions installed on your Rocky Linux 9 / AlmaLinux 9 system. To set a preferred version to use, you can either user RVM or rbenv

For RVM you can list the installed versions with the command:

$ rvm list
 * ruby-3.0.3 [ x86_64 ]
=> ruby-3.1.2 [ x86_64 ]

# => - current
# =* - current && default
#  * - default

Once listed, set the default version to use as shown;

$ rvm use 3.1.2 --default
Using /home/rocky9/.rvm/gems/ruby-3.1.2

For rbenv the available versions of Ruby are listed with the command:

$ rbenv versions
3.1.2

The global version set has a * on it. You can set a desired default version with the command:

rbenv global 3.1.2

Verify the made changes by checking the Ruby version:

$ ruby -v
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-linux]

#3. Install Rails on Rocky Linux 9 / AlmaLinux 9

Install Ruby Development package:

sudo dnf install ruby-devel -y

Once Ruby has been installed, we can easily install Rails as a Gem. The command for this ill be:

gem install rails

It is possible to install a preferred version of Rails with the command:

gem install rails –v [insert.number.version]

Verify the Rails version installed.

$ rails -v
Rails 7.0.4

You can also install any other Gem such as bundlerwith the command:

gem install bundler

#4. Test Ruby on Rails Functionality

By default, Rails comes with sqlite3 as the default database. However, this is not recommended in a production environment. It is recommended to use MySQL or PostgreSQL for high traffic web applications.

First enable the EPEL and PowerTools repositories:

sudo dnf install epel-release -y
sudo dnf config-manager --set-enabled crb

To install MariaDB, use the command:

sudo dnf install -y mariadb-server mariadb mariadb-devel

We have installed the MariaDB-devel package for Rails. Once the installation is complete, start and enable the service:

sudo systemctl enable --now mariadb

Harden the instances:

$ sudo mysql_secure_installation
....
Enter current password for root (enter for none): Just press Enter
......
Switch to unix_socket authentication [Y/n] Y
.....
Change the root password? [Y/n] Y
New password:  New-root-password
Re-enter new password: Re-enter New-root-password
....
Remove anonymous users? [Y/n] Y
....
Disallow root login remotely? [Y/n] Y
.....
Remove test database and access to it? [Y/n] Y
......
Reload privilege tables now? [Y/n] Y
...
Thanks for using MariaDB!

Install the mysql2extension.

gem install mysql2

Create a Sample Rails application

We will now create a simple Rails application using the MariaDB database.

cd ~
rails new sampleapp -d mysql
cd sampleapp

Create a database config file:

vim config/database.yml

Make the below changes to the file

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

Now create a database with the command;

rake db:create

sample Output:

Created database 'sampleapp_development'
Created database 'sampleapp_test'

Allow port 3000 through the firewall

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

Now start the Rails application:

rails server -b 0.0.0.0

Sample Output;

=> Booting Puma
=> Rails 7.0.3.1 application starting in development 
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.5 (ruby 3.1.2-p20) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 93848
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop

Verify if the applications is accessible from your browser using the URL http://IP_Address:3000

Ruby on Rails on Rocky

Conclusion

That marks the end of this guide on how to install Ruby on Rails on Rocky Linux 9 / AlmaLinux 9. I hope this was significant to you.

See more:

.tdi_4.td-a-rec{text-align:center}.tdi_4 .td-element-style{z-index:-1}.tdi_4.td-a-rec-img{text-align:left}.tdi_4.td-a-rec-img img{margin:0 auto 0 0}@media(max-width:767px){.tdi_4.td-a-rec-img{text-align:center}}

RELATED ARTICLES

Most Popular

Recent Comments