MySQL with Ruby on Rails featured image

Using MySQL with Ruby on Rails App on Ubuntu 21.04

Ruby is a powerful programming language. It’s an interpreted, high-level, and general-purpose language that incorporates multiple programming paradigms. Yukihiro Matsumoto, the creator of Ruby, blended various parts of his favorite programming languages like Perl, Ada, Lisp, Eiffel, Smalltalk, etc., and created Ruby. In his own words, he intended to make Ruby “more natural, not simple”.

Much of the popularity of Ruby comes from Ruby on Rails. It’s a powerful web framework built on top of Ruby. It serves as a backend to numerous web applications, for example, Airbnb, Spotify, GitHub, SoundCloud, and more. The philosophy of Ruby on Rails is to offer an intuitive framework to speed up building robust and high-performance web pages.

Being a web framework, Ruby on Rails can work with popular database engines, for example, SQLite, MySQL/MariaDB, and PostgreSQL. This guide will showcase building the foundation of a Ruby on Rails app that uses MySQL as its database engine.

Prerequisites

  • All the steps demonstrated in this guide are performed on Ubuntu 21.04. Learn more about setting up an Ubuntu server here.
  • We’ll also showcase a quick demonstration on installing and configuring a MySQL server. For more info, refer to this guide on setting up MySQL.
  • As for installing and managing Ruby, we’ll use rbenv. It’s a tool designed to help manage the Ruby environment. Compared to RVM, rbenv comes with its differences.

Step 1: Installing and Configuring MySQL

MySQL will serve as the database backend for our Ruby on Rails app. Assuming you don’t have MySQL installed and configured already, follow along. Launch a terminal and update the APT cache:

MySQL with Ruby on Rails code screenshot 1

Then, install the MySQL components:

MySQL with Ruby on Rails code screenshot 2

Once the installation is complete, run the following MySQL script. It will perform a quick post-installation configuration:

The script will ask multiple questions. Complete the steps to finish the initial configuration:

MySQL with Ruby on Rails code screenshot 4

3

4

MySQL with Ruby on Rails code screenshot 5

  • Additional Step

The following step is optional and should only be done when further down the tutorial, Ruby on Rails fails to connect to MySQL server. It sets the root user in MySQL to use mysql_native_password to allow traditional password logins. Access the root account on Ubuntu:

Now, launch the MySQL root shell without password check:

Next, run the following query to change the password and set mysql_native_password as the authentication method:

MySQL with Ruby on Rails code screenshot 6

Step 2: Installing and Configuring Ruby and Ruby on Rails

Our database server is ready for production. We now need to install Ruby. As mentioned earlier, we’ll be using rbenv as the Ruby version manager for our project. Ruby on Rails is available as a Ruby gem.

  • Installing rbenv

The Ruby manager rbenv is directly available from the official Ubuntu package server. For other distros, check out the official rbenv installation guide.

Fire up a terminal and install it right away:

MySQL with Ruby on Rails code screenshot 7

Next, we have to integrate rbenv with the default shell. Run the following rbenv command:

MySQL with Ruby on Rails code screenshot 8

As the output says, for bash, we need to include the line in the local bashrc file. Without going into too much detail, the bashrc file is loaded every time the user opens a new shell in interactive mode. Open the file in a text editor and add the following text:

Save the file and close the editor. It’s recommended to restart the terminal to take the changes into effect. Alternatively, you can reload the bashrc file into the current shell:

Next, we’ll install a rbenv plugin to provide the support for the rbenv install command. This plugin is directly available from the rbenv GitHub repo. Run the following commands:

To test our rbenv configuration, run the rbenv-doctor script:

It will check for various components and report if everything is ok.

  • Installing Ruby

Once rbenv is configured, we’re ready to install Ruby. The following command will list all the available versions of Ruby:

The output will be a bit long, so it’s advised to pass it through more or less for better browsing. The list also contains various builds of Ruby. For the purpose of this guide, we’ll focus on the latest official Ruby build. At the time of writing this article, it’s Ruby v3.0.3.

Now, install Ruby:

The next command will declare the installed Ruby as the global version available to all applications:

Verify the Ruby installation:

  • Installing Ruby on Rails

The web framework is available as a gem for Ruby. Although Ruby on Rails is directly available from the Ubuntu package servers, installing it as a gem offers more flexibility.

First, install the gem Bundler. It’s a tool that resolves gem dependencies. Rails is dependent on it:

Running the next command will install the latest stable version of Ruby on Rails as a gem:

If you want a specific version installed, then the command structure would look like this:

Once the gems are installed, run the following command:

The Ruby manager rbenv works by creating dedicated directories for shims that point to the files of the current Ruby version enabled. The rehash sub-command tells rbenv to maintain shims in that directory to match every Ruby command across every installed version of Ruby on the server. It should be run every time you install a new version of Ruby or a gem that provides commands similar to Rails.

Verify the Rails installation:

Step 3: Installing MySQL gem

To connect to the MySQL server, Rails needs an adapter. We’ll use mysql2 gem for this functionality. Install mysql2 gem:

Step 4: Creating and Configuring a New Rails App

  • Creating a Sample App

So far, we’ve configured all the necessary components to establish our Rails app. The following command will create a dedicated directory for the app and place all the necessary files with MySQL support:

  • Configuring MySQL Connection

Now, we need to dial in the necessary MySQL info so that Rails can connect to the database and exchange data. By default, Rails connects to MySQL as root. Open the Rails database configuration file:

Scroll down to the following section and enter the root password for MySQL. Then, save the file and close the editor.

  • Implementing Database Changes

If everything went fine, then Rails should now be able to connect to MySQL and perform necessary database operations. The following command will create the necessary databases:

Step 5: Testing the Configuration

Start the Rails development server:

You should be able to access the welcome page at the following URL:

Final Thoughts

In this guide, we’ve installed Ruby using rbenv, installed Rails gem, and configured a sample Rails app to connect to MySQL and use it as the database engine. It serves as a great skeleton for a scaling and centralized web app.

To learn how to install Ruby on Rails with RVM on Ubuntu 20.04 check the following tutorial Installing Ruby on Rails with RVM on Ubuntu 20.04. To learn how to set up Ruby on Rails with PostgreSQL, check out the following tutorial: Setting up Ruby on Rails with PostgreSQL.

Happy Computing!