Linux

How to Install Ruby On Rails on Ubuntu 20.04

How to Install Ruby On Rails on Ubuntu 20.04

Ruby on Rails is only known as Rails, an open source web application framework written in Ruby that helps you create powerful websites and applications.

This post will help you install Ruby on Rails on Ubuntu 20.04 using two methods:

  1. rbenv (Recommended)
  2. RVM

Precondition

Install Dependency

Install curl and other packages needed for the installation of Ruby on Rails.

sudo apt update

sudo apt install -y curl gnupg2 dirmngr git-core zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev software-properties-common libffi-dev

Install Node.js

Rails requires a Javascript runtime for application development on Linux. So, for that, we will install the Node.js LTS version (v12.x).

If you want to use the latest Node.js feature, install Node.js v14.x.

curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -

sudo apt install -y nodejs

Install Thread

Add a Yarn repository if you want to install the Yarn package manager to manage packages.

curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -

echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

Install Yarn with the command below.

sudo apt update && sudo apt install -y  yarn

Install Ruby

Using Rbenv (Recommended)

Rbenv allows you to install and manage Ruby versions easily, and is lighter than RVM.

To install rbenv on your system, run the command below. The command below will install rbenv to your home directory and set the appropriate environment variables.

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

For this post, we will install the latest version of Ruby (v2.7.1).

rbenv install 2.7.1

You can also install other ruby versions by rbenv installing the command.

Set Ruby v2.7.1 as the default version for all login shells.

rbenv global 2.7.1

Check the Ruby version.

ruby -v

Output:

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Install the bundler.

gem install bundler

Using RVM

RVM stands for Ruby Version Manager. This helps install and manage the ruby version efficiently and independently by automatically downloading the dependencies.

Import public keys in your system.

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

Use the curl command to install RVM on your system.

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

Load the RVM environment variable using the command below.

source ~/.rvm/scripts/rvm

Use the following command to install Ruby 2.7.0.

rvm install 2.7.1

You can also install other ruby versions using the rvm install command.

Set the default Ruby version to 2.7.1 if your system has multiple ruby versions.

rvm use 2.7.1 --default

Output:

Using /home/raj/.rvm/gems/ruby-2.7.1

Check the Ruby version.

ruby -v

Output:

ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]

Install the bundler.

gem install bundler

Install Rails

Use the gem install rails command to install the latest stable Rails release.

gem install rails

You can also use gems to install rel -version = to install certain rail versions.

Check the Rails version.

rails -v

Output:

Rails 6.0.3.1

Create Rails Application

We will now create an application with MariaDB support to check the Ruby on Rails installation.
Install the MariaDB Database

Rails uses sqlite3 as the default database, and it is not recommended to use it in a production environment. For production installations, you may need to use MySQL or PostgreSQL.

For this demo, we will install MariaDB (v10.3) from the Ubuntu repository and use it as a database server.

sudo apt install -y mariadb-server mariadb-client

Next, install the development file package below.

sudo apt install -y libmariadb-dev

Create Database

Create Database Users

sudo mysql -u root -p

Create database users for your application.

CREATE USER 'installworld'@'localhost' IDENTIFIED BY 'password';

GRANT ALL PRIVILEGES ON *.* TO 'installworld'@'localhost';

exit

Install the MySQL2 extension.

gem install mysql2

Create Rails Application

Create applications with database support as standard users.

NOTE: Running the Rails server because the root user is not recommended.

cd ~

rails new installworld -d mysql

cd installworld

Update the configuration file with database information.

nano  config/database.yml

Enter the DB user details as below.

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: installworld << DB User
  password: password  << DB Password
  socket: /var/run/mysqld/mysqld.sock

Create a database.

rake db:create

Output:

Created database 'installworldapp_development'
Created database 'installworldapp_test'

Validating Rails Applications

Start the Rails application.

rails server -b 0.0.0.0

Output:

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

Now, the Rails application should be running on port 3000.

Access the Rails application by opening the URL below in a web browser.

http://localhost:3000

OR

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

You must get the following page.

1

Rails Application Running on Ubuntu 20.04

Conclusion

That is all. I hope you have learned how to install Ruby on Rails on Ubuntu 20.04.

Related posts

How to Set Up WireGuard VPN on CentOS 8

Linux

How to Install MariaDB on Ubuntu 20.04

Linux

How to Install KVM on Ubuntu 20.04 LTS

Linux

Using the ‘ldd’ command on Linux

Linux

How to Change Hostname on Ubuntu 20.04

Linux

How to Install Apache ActiveMQ on Debian 10

Linux

How to Grep Way for Various Strings and Patterns

Linux

How to use the chmod command on Linux

Howto

How to Add SWAP Space on Ubuntu 20.04

Linux