How To Install Ruby On Rails On Ubuntu 20.04

THIS DOCUMENT IS ALSO AVAILABLE FOR

Ruby on Rails® simply known as Rails, is an open-source web application framework written in Ruby that helps you create highly powerful web sites and applications.

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

  1. rbenv (Recommended)

  2. RVM

Prerequisites

Install Dependencies

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

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 need a Javascript runtime for application development in Linux. So, for that, we will install the LTS version of Node.js (v12.x).

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

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

sudo apt install -y nodejs

Install Yarn

Add the Yarn repository in case 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 below command.

sudo apt update && sudo apt install -y  yarn

Install Ruby

The rbenv lets you install and manage the versions of Ruby easily, and it is lighter than RVM.

To install rbenv on your system, run the below commands. The below commands will install rbenv into your home directory and set 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 versions of ruby with rbenv install <version> 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. It helps install and manage ruby versions efficiently and independently by automatically downloading its dependencies.

Import public key in your system.

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

Use the curl command to install RVM in your system.

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

Load RVM environment variables using the below command.

source ~/.rvm/scripts/rvm

Use the following command to install Ruby 2.7.0.

rvm install 2.7.1

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

Set default Ruby version to 2.7.1 in case your system has multiple versions of ruby.

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 gem install rails command to install the latest stable release of Rails.

gem install rails

You can also use gem install rails –version=<version> to install a specific version of rails.

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 MariaDB Database

Rails uses sqlite3 as the default database, and it is not recommended to use it in a production environment. For production installation, you should probably need to go with MySQL or PostgreSQL.

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

READ: How To Install MariaDB v10.4 on Ubuntu 20.04

sudo apt install -y mariadb-server mariadb-client

Next, install the below development files package.

sudo apt install -y libmariadb-dev

Create Database

Create Database User

sudo mysql -u root -p

Create a database user for your application.

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

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

exit

Install the MySQL2 extension.

gem install mysql2

Create Rails Application

Create an application with database support as a standard user.

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

cd ~

rails new itzgeekapp -d mysql

cd itzgeekapp

Update configuration file with the database information.

nano  config/database.yml

Enter the DB user details shown like below.

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

Create the database.

rake db:create

Output:

Created database 'itzgeekapp_development'
Created database 'itzgeekapp_test'

Validate Rails Application

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

By now, the Rails application should be running on port 3000.

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

http://localhost:3000

OR

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

You should get the following page.

Rails Application Running on Ubuntu 20.04
Rails Application Running on Ubuntu 20.04

Conclusion

That’s All. I hope you have learned how to install Ruby on Rails on Ubuntu 20.04.

Prev Post
Next Post
comments powered by Disqus