Forward
Previously, I posted My Ruby on Rails Development Environment on how I set up my development environment. This article is still mostly correct as far as my checklist goes, but some of the steps have changed with time. I’ve copied most of the content from that article to this one with the updated steps as well as updated some of the text.
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 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.
I’ve been an active panelist on Ruby Rogues for over a year now. I love getting to know other members of the community through the talks that we have.
I’ve been an active member on the Ruby on Rails Link Slack group and chatting with other members in the community. This is by far one of the largest active communities that I’ve found for Rails related stuff.
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 Movaje 10.14.2. 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.
Visual Studio Code – Preferred text editor
Google Chrome – Browser of choice
EasyRes – Quick Resolution changer
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.
1 2 3 |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" |
We can then use Homebrew to install the rest of our software applications.
iTerm2
1 2 3 |
brew cask install iterm2 |
Spectacle
1 2 3 |
brew cask install spectacle |
Visual Studio Code
1 2 3 |
brew cask install visual-studio-code |
Browser
1 2 3 |
brew cask install google-chrome |
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. I choose to install MySQL 5.7, otherwise version 8.x or later would get installed. I do this because I have older applications which are not directly compatible with v8.x+.
1 2 3 |
brew install mysql@5.7 |
You can then start the MySQL service (and this will allow it to start whenever the computer is booted)
1 2 3 |
brew services start mysql@5.7 |
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 .
1 2 3 |
/usr/local/opt/mysql@5.7/bin/mysql_secure_installation |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Securing the MySQL server deployment. Press y|Y for Yes, any other key for No: n Password for root: Remove anonymous users? (Press y|Y for Yes, any other key for No) : y Disallow root login remotely? (Press y|Y for Yes, any other key for No) : y Remove test database and access to it? (Press y|Y for Yes, any other key for No) : y Reload privilege tables now? (Press y|Y for Yes, any other key for No) : y |
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.
1 2 3 |
sudo apachectl start |
PHP (Optional)
By default, the PHP
module is included with OSX. However, it is not enabled. Edit the httpd.conf
file.
1 2 3 |
sudo nano /etc/apache2/httpd.conf |
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.
1 2 3 |
LoadModule php7_module libexec/apache2/libphp7.so |
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).
1 2 3 |
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
.
1 2 3 4 |
sudo mkdir /var/mysqlsudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock |
Install PHPMyAdmin
1 2 3 |
brew install phpmyadmin |
Let’s edit our httpd.conf
again.
1 2 3 |
sudo nano /etc/apache2/httpd.conf |
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:
1 2 3 |
Listen 8888 |
At the very end of the httpd.conf
file, copy and paste the code from the installation instructions
Restart apache
1 2 3 |
sudo apachectl restart |
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.
1 2 3 |
curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh |
You’ll then need to provide your user password to change your terminal to ZSH.
RVM
While OSX 10.14.2 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.5.3
is the latest MRI version. This is the version that we will be using.
1 2 3 4 5 6 7 8 9 10 11 |
brew install gpg2 gpg --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB \curl -sSL https://get.rvm.io | bash -s stable source /Users/$(whoami)/.rvm/scripts/rvm rvm install 2.5.3 |
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.
1 2 3 |
echo 'gem: --no-document' >> ~/.gemrc |
Let’s install Ruby on Rails!
1 2 3 |
gem install rails |
If you get an error trying to install the gem mysql2
then try the following command. You may want to add in your version number with a -v '0.5.2'
after the gem name. Also, update the path to the MySQL directory based on your version.
1 2 3 |
gem install mysql2 -- --with-mysql-dir=/usr/local/Cellar/mysql@5.7/5.7.24/ |
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.
1 2 3 4 5 6 7 |
brew install git git config --global user.name "USERNAME" git config --global user.email "EMAILADDRESS" |
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.
1 2 3 4 |
ssh-keygen -t rsa -C "EMAILADDRESS" cat ~/.ssh/id_rsa.pub |
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
1 2 3 4 5 |
brew install puma/puma/puma-dev sudo puma-dev -setup puma-dev -install |
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.
1 2 3 |
puma-dev link -n testapp . |
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.
1 2 3 4 5 6 7 |
brew cask install homebrew/cask-versions/java8 brew install elasticsearch@5.6 brew services start elasticsearch@5.6 |
Redis (Optional)
If you will be using caching
, sidekiq
, or ActionCable
, you will need Redis.
1 2 3 4 |
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
.
With ActiveStorage and the upcoming ActionText, let’s also install vips
to ready our system with this imaging tool. Janko, the creator of the Shrine gem, has done a lot of work on the image_processing
gem which is used in the README for ActionText.
1 2 3 |
brew install imagemagick vips |
Pulling an Existing Project
I typically create a Rails folder in my home directory.
1 2 3 4 5 6 7 |
mkdir Rails cd Rails git clone <remote repo> |
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
.
As newer versions of MacOS gets released, I’ll probably update this guide to those as well. If you haven’t checked out Drifting Ruby, be sure to do so as I cover a lot of exciting topics!
Follow me on Twitter @kobaltz and @driftingruby