13.8 C
Texas

How To Install Ruby On Rails On Ubuntu 20.04

In this tutorial you’ll learn that how to install Ruby On Rails On Ubuntu 20.04. Ruby On Rails also called Rails(simple form) is an open source platform that can be used to create high performance & powerful websites as well as applications. So, here we’ll see that how we can install it on our Ubuntu 20.04 system.

Step 1: Update Your System

As usual we do, update your system and install the required dependencies.

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

Step 2: Install Node.Js On Ubuntu 20.04

As Rails required a Javascript runtime to build applications in Linux. So, install LTS version of Node.js (v12.x). You can also use the latest version of Node.js (14.x).

- Advertisement -
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install -y nodejs

Step 3: Install Yarn On Ubuntu 20.04

Now, add the yarn repository on Ubuntu 20.04 and install it with the help of below commands.

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
sudo apt update && sudo apt install -y yarn

Step 4: Install Ruby On Ubuntu 20.04

Here we’ll see two methods to install Ruby On Ubuntu 20.04.

  1. By Using rbenv (Recommended Method)
  2. With the help of RVM

So, choose your favourite method to proceed.

By Using rbenv (Recommended Method)

This method is very useful for managing the versions of Ruby quickly & it is lighter than RVM. Simply, run the following commands in your terminal to install the rbenv.

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

Install the latest version of Ruby by typing

rbenv install 2.7.2

You can also install other version with the help of rbenv install <version> command.

Now, set the above installed version as default by typing

rbenv global 2.7.2

Verify the installed version by typing

ruby -v

Output:

sabi@Ubuntu20:~$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

And type the below command to install bundler.

gem install bundler

Step 5: Install Ruby By Using RVM

RVM (Ruby Version Manager) helps you to install the ruby versions efficiently & auto-download its dependencies.

Add Public key & install it with the help of curl command.

gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable

Now, run the below command to load RVM environment variables.

source ~/.rvm/scripts/rvm

And hit the following command to install Ruby 2.7.2

rvm install 2.7.2

You can also install different versions by typing rvm install <version>.

Set the default version by entering

rvm use 2.7.2 --default

And verify the Ruby version.

ruby -v

Output:

sabi@Ubuntu20:~$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]

And type the below command to install the bundler.

gem install bundler

Step 6: Install Rails On Ubuntu 20.04

Type the given command to install the Rails on Ubuntu 20.04.

gem install rails

To install a specific version, type

gem install rails –version=<version>

Verify the installed version by

rails -v

Output:

sabi@Ubuntu20:~$ rails -v
Rails 6.0.3.4

Step 7: Create Rails Application

In order to create rails application, you’ve to install a database. By default Rails uses its own database but it is not recommended for production use. So, install MariaDB & use it with the Rails.

To install MariaDB on Ubuntu 20.04, type

sudo apt install -y mariadb-server mariadb-client

And install the required packages.

sudo apt install -y libmariadb-dev

Now, login to MariaDB & create a database.

sudo mysql -u root -p
CREATE USER 'sabi'@'localhost' IDENTIFIED BY 'Your_Password';
GRANT ALL PRIVILEGES ON . TO 'sabi'@'localhost';
exit;

And install the MySQL extension.

gem install mysql 2

Then create the new test application.

cd ~
rails new sabitestapp -d mysql
cd sabitestapp

And update the config file with Database info.

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

Note: Replace Username & Password with your own.

Then create the database.

rake db:create

Output:

You’ll see the similar output.

sabi@Ubuntu20:~/sabitestapp$ rake db:create
Created database 'sabitestapp_development'
Created database 'sabitestapp_test'

And then start the application by typing.

rails server -b 0.0.0.0

Output: You’ll see the similar output.

sabi@Ubuntu20:~/sabitestapp$ rails server -b 0.0.0.0
=> Booting Puma
=> Rails 6.0.3.4 application starting in development
=> Run rails server --help for more startup options
Puma starting in single mode…
Version 4.3.7 (ruby 2.7.2-p137), codename: Mysterious Traveller
Min threads: 5, max threads: 5
Environment: development
Listening on tcp://0.0.0.0:3000
Use Ctrl-C to stop

Step 8: Access Rails On Web

Rails application should be running on port 3000 you can access it in your browser.

Type your server IP with port 3000 like IP:3000 to access the web dashboard.

How To Install Ruby On Rails On Ubuntu 20.04

As installation done, start building with Rails. So, this is how you can install Ruby on Rails on Ubuntu 20.04

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article