My Ruby on Rails Development Environment

Forward

Sometimes we have to configure a new development machine, whether it is for ourselves or a friend. We often find that there are some little pieces of our environment that we have forgotten about or have neglected to document. This is my documentation for a ready-to-work development environment for Ruby on Rails.

About Me

I have been working with Ruby on Rails for several years (since 2009) and have tried various other platforms; yet always circling back to Ruby on Rails. In my early days, I developed on Ubuntu as I did not have nor could I afford an Apple. I moved back to a Windows machine and used my host operating system for my main development environment. I kept an Ubuntu server within a Virtual Machine (Oracle VirtualBox) where I would SSH into the VM and run any tasks. I used a shared network folder via Samba to access my files. While my environment was very productive, it didn’t have the true native feel of a development environment. I got an Apple, where I now spend my days developing Ruby applications.

I also run and maintain several pet projects; one of which is Drifting Ruby. Some of the older apps that I use for different things around the house will need updating from time to time and might use an older version of Ruby with a different gem versions. So, having multiple Ruby versions and being able to switch them is very handy.

While it is not required, I do have several servers in my basement that I use as a testing/staging environment as well as a code repository for my projects. I also back up all of my work to an offsite location in the event of a destroyed home.

I come from a Systems Administrator background where I worked on Windows environments and Linux environments daily. As a Ruby on Rails developer, you should become comfortable with your Terminal as it can be a great tool.

About This Guide

By no means are the topics or recommendations covered in this guide best practices. Please let me know if you have found anything that I should be doing differently. However, I have found that these steps work great for my productivity and comfort. Not everything in this guide is Ruby on Rails specific, but the majority does touch on the installation and configuration of Ruby on Rails or the development environment that I enjoy working in. This guide is based on MacOS Sierra 10.12.4. It should work for previous versions. If you do find some issues, please let me know and I”ll see about updating this guide.

Software

I have found that there are a few programs that makes life much easier whenever I setup a new development environment. Here are the basic applications that I feel is necessary for any new environment.

Homebrew – The missing package manager for OSX
iTerm2 – Alternative Terminal
Spectacle – Hotkeys for Window Movement and Placement. This is an absolute must have if you’re using multiple monitors.
SublimeText – Preferred text editor
Atom or Visual Studio Code are great free alternatives
Google Chrome – Browser of choice

Homebrew

Most developers that use an Apple probably rely on Homebrew as their quick goto for installing available packages. I’m no different. Let’s get Homebrew installed. For the rest of the guide, most of our work is done in the Terminal. Where necessary, I’ll indicate if you need to run something as sudo or not. If you do not see sudo prepended by any of the commands, it’s safe to say that it is not required and/or recommended.

We can then use Homebrew to install the rest of our software applications.

iTerm2

Spectacle

Editor

Sublime Text

Atom

Visual Studio Code

Browser

Additional Packages

Now that we have our core development tools installed, we can start installing our services.

MySQL

While most Ruby on Rails developers like using PostgreSQL for their database backend, I like and use MySQL. You can also use MariaDB if you wish which is very similar to MySQL.

You can then start the MySQL service (and this will allow it to start whenever the computer is booted)

We then need to set our password for the mysql  server. Be sure to remember this password. We will set our password through the secure installation script that gets shipped with mysql .

You will be asked a series of questions. Other than the first question and setting the root password, I’ll answer yes to the rest.

Apache (Optional)

phpMyAdmin is a handy and fairly light weight web interface to the MySQL server. However, it does rely on enabling Apache as well as PHP

By default, Apache  comes with OSX 10.12.4 (as well as other previous versions). To start the service, type the below in your terminal.

PHP (Optional)

By default, the PHP module is included with OSX. However, it is not enabled. Edit the httpd.conf file.

Look for the following line (Use CTRL + W to do a search within nano) and uncomment the line (remove the pound infront of it). You kids may call it a hashtag, but I’m still oldschool.

Save the httpd.conf file. You can do this by typing CTRL + X within nano. It will prompt you to save, enter y and press enter (which will overwrite the existing file).

sudo apachectl restart

PHPMyAdmin

While MySQL Workbench is an absolutely great tool, I do not care for it. I prefer using PHPMyAdmin in my development environmen. I usually keep PHPMyAdmin opened in a different tab, but have recently found myself using it less and less.

Let’s create a symlink to the mysql.sock.

Install PHPMyAdmin

Let’s edit our httpd.conf again.

Another tool that I use very often is called puma-dev. This allows me to quickly access and spin up a Ruby on Rails application without having to manually start the web service. I have found that there is sometimes a conflict with the puma-dev service and apache. To avoid this entirely, I change the port that apache will listen on.

Look for the line Listen 80 and change this to:

Listen 8888

At the very end of the httpd.conf file, copy and paste the following:

Restart apache

Oh My Zsh

If you prefer to have a fancier shell than bash, you can use oh-my-zsh. Purely a preference and an optional step. I will say though that the little x if changes that needs to be commited to version control and letting me know at a glance what branch I’m currently working on is a great tool to have if you spend a lot of time in the terminal.

You’ll then need to provide your user password to change your terminal to ZSH.

RVM

While OSX 10.12.4 does come with a Ruby interpreter, I do not like using my system ruby version. Instead, I use RVM as my Ruby Version Manager. Other ones like rbenv and chruby are fine. All three will act similar as far as keeping your ruby environments separate. At the time of this guide, Ruby 2.4.1 is the latest MRI version. This is the version that we will be using.

I often prefer to skip the installation of my gem documentation. I do this because it takes longer to download and install the gem. If I need to find the documentation of a gem, I’ll usually look at the Github README of the gem or similar.

Let’s install Ruby on Rails!

Git

Personally, since I use a privately hosted Gitlab environment for my version control, I install and configure git on my development machines. However, this is purely optional if you have your own way/method of version control.

Be sure to change your USERNAME and EMAILADDRESS to your own information.

SSH Key

In order to pull and push my code to my git repository, I authenticate my machine with the version control server via an authorized ssh key.

Copy and paste the SSH Key into your Version Control SSH Key section.

Puma-dev

Puma-dev is an amazing tool. It will spin up web servers so that you can quickly access your Ruby on Rails applications. For example, if I have a project called testapp under ~/Rails/testapp, I can use puma-dev to create a link to this application. Even on a fresh reboot, I can go to http://testapp.dev or https://testapp.dev and it will launch my Ruby on Rails application. There is no need to go into your terminal to start the web service. You can also have multiple Ruby on Rails applications running at the same time with this. No more fuss with trying to find out which port is currently in use by another Ruby on Rails application.

For a video tutorial, check out https://www.driftingruby.com/episodes/puma-dev-replacement-for-pow-and-prax

cd into your Rails application and ENTER. We will then create our link with puma-dev. You can pass the optional -n NAME to customize the domain.dev that will be linked.

You now have a symlink created under ~/.puma-dev where it references to your Ruby on Rails application. You can now visit the YOURAPP.dev in the browser and it should load up.

ElasticSearch (Optional)

If you use ElasticSearch for full text searching, then here is the quick way to install it. ElasticSearch does require Java to be installed.

Redis (Optional)

If you will be using caching, sidekiq, or ActionCable, you will need Redis.

brew install redis
brew services start redis

ImageMagick (Optional)

If you upload images and resize them, you’ll most likely use minimagick. A dependency of these tools is ImageMagick

Pulling an Existing Project

I typically create a Rails folder in my home directory.

Cloning a repository from within my Rails folder, will create a new folder with that repository’s code.

From here, you’re ready to go with your new development environment!

Quick Notes of Advice

Within my Google Chrome Bookmarks Bar, I keep a few different things. My First thing is a folder in the bookmarks that keeps a shortcut to my puma-dev URLs. I also keep a bookmark to http://localhost:8888/phpmyadmin to quickly access PHPMyAdmin.

  • devsimple

    Hello, Dave. Thanks for this guid and all your work.
    I found for myself very handy and free data bases manager http://dbeaver.jkiss.org/
    Maybe someone will be useful

    • Evgeniy Fateev

      I use it after searching and trying a few alternatives and it’s awesome!

      Also, thanks for the post, Dave! )

  • Gabriel

    Hi Dave,
    I installed puma-dev, but can’t find the log file to look for errors in rails… can you tell us please where to find it?

    • kobaltz

      For Puma-Dev logs, you can tail or cat the file at

      tail -f ~/Library/Logs/puma-dev.log

      Otherwise, for your rails application, it logs to the normal development.log. So, with the app running, you can simply tail from your project directory.

      tail -f log/development.log

      • Gabriel

        thanks Kobaltz =)