How to Intall Ruby On Rails and MySQL on Ubuntu 18.04

Billy Cheng
3 min readAug 23, 2019

--

In this article we are going to walk through the following

  • Setup a Ubuntu 18.04 on a cloud server
  • Install rvm and Ruby 2.6.x
  • Install Rails 5.2.x
  • Setup MySQL
Getting ready for your Ruby on Rails development

Setup a Ubuntu 18.04 on a cloud server

First you need to ssh to your newly created cloud instance. I am a long fan of Linode.com.

Connect to your instance ssh root@<<IP_ADDRESS>>.

Install software updates apt-get update && apt-get upgrade.

Replace example_hostname with one of your choice

echo “example_hostname” > /etc/hostname
hostname -F /etc/hostname

Set the Timezonedpkg-reconfigure tzdata

Once you are done setting up, you can use the date command to view the current date and time according to your server.

root@rails-app: ~# date
Fri Aug 23 22:31:32 HKT 2019

Next, you will have to create a user for your Ruby on Rails application in the following steps.

  1. Create a user called deploy, adduser deploy
  2. Add superuser capability, adduser deploy sudo
  3. Quit your session by typing exit
  4. Verify your username and password ssh deploy@<<IP_ADDRESS>>

Then secure your SSH Access by disallowing root logins over SSH, and look for the option PermitRootLogin.

sudo nano /etc/ssh/sshd_config...
...
# PermitRootLogin no

Finally, restart the SSH service to load the new configuration

sudo systemctl restart sshd

Install RVM and Ruby 2.6.x

The first step is to install some dependencies.

sudo apt install curl
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
curl -sS 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-get update
sudo apt-get install 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 nodejs yarn

Then install the rvm.

sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm install 2.6.3
rvm use 2.6.3 --default
ruby -v

At the end, install Bundler.

gem install bundler

Install Rails 5.2.x

gem install rails -v 5.2.3
rails -v
# Rails 5.2.3

Setup MySQL

Usually I will set up my database instance on a separate server to enable a 2 tier architecture. If this is also what you are looking for, go back to the first step and setup your new cloud instance.

Now you will have 2 server instances, one is client (with Ruby on Rails installed) and one is server (with nothing except a fresh Ubuntu setup).

For client side, install the MySQL client and the necessary library. Installing the libmysqlclient-dev gives you the essential files to compile the mysql2 gem which is used by Rails to connect to MySQL

sudo apt-get install mysql-client libmysqlclient-dev

For server side, install the MySQL server

sudo apt-get install mysql-server

After finished installing the MySQL server, there is a setting you need to change so your server instance accepts remote connection.

Edit mysql configuration file mysqld.cnf and look for bind-address and change to 0.0.0.0,

deploy@mysql-host: cd /etc/mysql/mysql.conf.d
deploy@mysql-host: sudo nano mysqld.cnf
...
...
bind-address = 0.0.0.0

Now that your configuration has changed, you can restart your mysql,

sudo service mysql restart

Go back to your client (with Ruby and Rails installed) and you could verify the connection by:

deploy@rails-app: mysql -u root -p -h <<DB_IP_ADDRESS>>
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 60
Server version: 5.X.XX
Copyright (c) 2000, 2018, Oracle and / or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear current input statement.mysql>

Now that you have your Ruby and Rails installed on one server instance, with a separate instance for MySQL server. You are good to enjoy your Ruby on Rails development.

Enjoy !

--

--

Billy Cheng

Share my tips and codes on my work with Ruby on Rails