Install Ruby On Rails on Ubuntu 16.04 | 18.04 | 18.10

Ruby on Rails, a web application framework that helps you build great applications and websites can easily be installed and setup on Ubuntu systems using the steps below. Many beautiful applications and websites are built on Ruby programming language. and setting up your environment is the first step.

This brief tutorial shows students and new users how to setup a Ruby on Rails environment on Ubuntu 16.04 | 18.04 and 18.10.

There are many tutorials online that will help you setup your own environment. however, this post will make it easy for students and new users who are just learning how to create their own Ruby on Rails environments.

When you’re ready to get your environment setup, follow the steps below:These are the packages we’re going to be setting up.

  • Ruby version 2.5.3
  • Rails version 5.2.1
  • MariaDB

Step 1: Installing Ruby

To install Ruby and Rails on Ubuntu, you’ll need  to install some dependencies. To make that happen, install Node.js and Yarn repositories. This will make installing the dependencies easier.

First install these curl and git packages.

sudo apt update
sudo apt install curl git

Than run the commands below to add Node.js and Yarn repositories and keys to your system. Then install some core packages to get your environment going.

curl -sL  | sudo -E bash -
curl -sS  | sudo apt-key add -
echo "deb  stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update
sudo apt-get install nodejs yarn 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

When you’re done. continue below:

After adding the repositories and installing necessary packages above, install Ruby with your local profile settings using rbenv. you’ll then use rbenv to install ruby-build.

cd ~/
git clone  ~/.rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
exec $SHELL

git clone  ~/.rbenv/plugins/ruby-build
echo 'export PATH="$HOME/.rbenv/plugins/ruby-build/bin:$PATH"' >> ~/.bashrc
exec $SHELL

After setting up your local profile. run the commands below to install Ruby version 2.5.3. If a newer version is available, replace the version number to that. vist this site to find out Ruby latest versions.

rbenv install 2.5.3
rbenv global 2.5.3

To verify that Ruby is installed, run the commands below:

ruby -v

You should see similar lines as below:

ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux]

Another package management you’ll want to install is bundler. to do that, run the commands below

gem install bundler

Now run the command below after installing bundler.

rbenv rehash

Step 2: Install Rails

Now that Ruby environment is set up, run the commands below to install Rails. Rails can be installed from a Node.Js. Run the commands below to install Node.js repository, then install Node.js package.

curl -sL  | sudo -E bash -
sudo apt-get install -y nodejs

Now that Node.js is installed, run the commands below to install Rails.

gem install rails -v 5.2.1

Don’t forget to rehash your rbenv environment installing install new packages.

rbenv rehash

To verify if Rails is installed, run the commands below.

rails -v

You should see something similar to the lines below:

Rails 5.2.1

Step 3: Install MariaDB

Usually a database server is used for most applications. and MariaDB is a great open source database server. to install it on Ubuntu, run the commands below:

sudo apt-get install mariadb-server mariadb-client libmariadbclient-dev

After installing MariaDB, the commands below can be used to stop, start and enable MariaDB service to always start up when the server boots.

Run these on Ubuntu 16.04 LTS

sudo systemctl stop mysql.service
sudo systemctl start mysql.service
sudo systemctl enable mysql.service

Run these on Ubuntu 18.10 and 18.04 LTS

sudo systemctl stop mariadb.service
sudo systemctl start mariadb.service
sudo systemctl enable mariadb.service

Next, run the commands below to secure the database server with a root password if you were not prompted to do so during the installation.

sudo mysql_secure_installation

When prompted, answer the questions below by following the guide.

  • Enter current password for root (enter for none): Just press the Enter
  • Set root password? [Y/n]: Y
  • New password: Enter password
  • Re-enter new password: Repeat 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

Now that MariaDB is installed, to test whether the database server was successfully installed, run the commands below.

sudo mysql -u root -p

type the root password when prompted.

mariadb welcome

If you see a similar screen as shown above, then the server was successfully installed.

After that, Ruby and Rails should be installed and your environment ready for you to start building apps based on Ruby and Rails.

You may also like the post below: