Skip to content

Instantly share code, notes, and snippets.

@manshu
Created June 18, 2016 18:34
Show Gist options
  • Save manshu/17361fec94012ec4cbcbb0d0e41b3c46 to your computer and use it in GitHub Desktop.
Save manshu/17361fec94012ec4cbcbb0d0e41b3c46 to your computer and use it in GitHub Desktop.
Ruby on Rails Development Environment

Ruby on Rails Development Environment

Introduction

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 work for Sage and it is and has been a great company to work for. My daily duties are around Ruby application development which includes some RubyMotion work for iOS and Android devices as well as the Ruby on Rails framework for web application development. I have been working through 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 recently got a MacBook Pro, and Mac Pro where I now spend my days developing Ruby applications.

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 work. I find that having my own hosted code repository (GitlabHQ) gives me a sense of security. 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 Yosemite 10.10.3. It should work for previous versions. It should also be good to note that when writing this guide, I was actively configuring a new development machine. As I wrote the commands, I copied and pasted directly from this document. You should, technically, be able to do the same on a fresh (unmodified) OSX 10.10.3 machine without any issues. However, 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.

Software Package Version Description Link Free?
iTerm2 2.1.1 Alternative Terminal https://iterm2.com/ Yes
Spectacle 0.8.10 Hotkeys for Window Movement and Placement. This is an absolute must have if you're using multiple monitors. http://spectacleapp.com/ Yes
MacDown 0.4.4.1 Markdown Editor (Used to create this guide) http://macdown.uranusjr.com/ Yes
SublimeText 3083 Preferred text editor http://www.sublimetext.com/ No
Google Chrome 43.0.2357.65 Preferred browser https://www.google.com/chrome/ Yes
XCode 6.3.2 Needed for dependencies (Command Line Tools may suffice) https://developer.apple.com/xcode/ Yes

If you're on a zero budget, you may want to look into TextMate or similar within the AppStore.

Information

One of the Virtual Machines that I host is a private Certificate Authority. I use this because I do not want to bother with requesting new SSL Certificates every time I need to spin up a new domain or subdomain. In order for me to add my Root Certificate to my Apple Keychain, I have to allow for 8192 bit certificates. Don't ask me why I used such I high encryption for my CA. I have to make daily trips to Costco for all of my tinfoil hats.

You can definitely skip this step unless if you're in a similar situation as me. However, for my sake, it's good to have this documented instead of spending 10 minutes 'googling' on how to increase the length fo the RSAMaxKeySize.

Be sure to agree to the licensing terms of XCode before proceeding.

To accept a 8192 bit Certificates, you will have to allow this as the default max size is 4096 bit.

sudo defaults write /Library/Preferences/com.apple.security RSAMaxKeySize -int 8192

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. At this point, you should already have your software programs installed from the previous section. Also, go ahead and open up XCode and accept the license term (if you agree with whatever it says...). 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.

xcode-select --install
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

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.

brew install mysql

You'll see a lot of these ln -sfv and launchctl in the various sections of this guide. This is a simply copy and paste from the output of my terminal for when the package was installed. The first command will simply start up the installed program whenever the computer is booted. The second command will immediately start up the program. I usually do these, but I recommend that you copy and paste whatever is in the output of your terminal as it can be different depending on some versions.

ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist

We then need to set our password for the mysql server. Be sure to remember this password (and keep the quotes below. You'll use this password within your database.yml file under the development settings area.

mysqladmin -u root password 'ENTERYOURPASSWORDHERE'

Apache

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

sudo apachectl start

PHP

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

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.

LoadModule php5_module libexec/apache2/libphp5.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).

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 environment because I already have Google Chrome open. 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.

sudo mkdir /var/mysql
sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock

Install PHPMyAdmin

brew install josegonzalez/php/phpmyadmin

Let's edit our httpd.conf again.

sudo nano /etc/apache2/httpd.conf

Another tool that I use very often is called pow. 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 pow 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:

Alias /phpmyadmin /usr/local/share/phpmyadmin
  <Directory /usr/local/share/phpmyadmin/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    <IfModule mod_authz_core.c>
      Require all granted
    </IfModule>
    <IfModule !mod_authz_core.c>
      Order allow,deny
      Allow from all
    </IfModule>
  </Directory>

Restart apache

sudo apachectl restart

RVM

While OSX 10.10.3 does come with a Ruby interpretor, 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.2.2 is the latest MRI version. This is the version that we will be using.

brew install gpg
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable

source /Users/kobaltz/.rvm/scripts/rvm

rvm install 2.2.2
gem update --system

I often prefer to skip the installation of my gem documenation. 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.

echo 'gem: --no-document' >> ~/.gemrc

Let's install Ruby on Rails!

gem install rails

Git

Personally, since I use a privately hosted GitlabHQ 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.

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.

ssh-keygen -t rsa -C "EMAILADDRESS" && cat ~/.ssh/id_rsa.pub

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

Pow

Pow is an amazing tool. It will spin up rack 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 pow to create a link to this application. Even on a fresh reboot, I can go to http://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.

curl get.pow.cx | sh

There is a great gem called powder which will give you three main functions (that I use it for). powder up|down will start the pow service or stop it. This is helpful if you're trying to debug something and for whatever reason, need to restart the pow service. It's good to note that by using pow, you can touch tmp/restart.txt within your application folder and it will restart the web service for that Ruby on Rails application.

gem install powder

cd into your Rails application and enter

powder link

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

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.

curl -L https://raw.github.com/robbyrussell/oh-my-zsh/master/tools/install.sh | sh

rvm get stable --auto-dotfiles

Optional: ElasticSearch

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

brew install Caskroom/cask/java
brew install elasticsearch
ln -sfv /usr/local/opt/elasticsearch/*.plist ~/Library/LaunchAgents
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.elasticsearch.plist

Optional: Memcached

Using dalli is a great way to interface with memcached. This can give your application a huge speed boost at the cost of memory. Instead of hitting the SQL Server for a query, you can cache the data into memcached.

brew install memcached
ln -sfv /usr/local/opt/memcached/*.plist ~/Library/LaunchAge
launchctl load ~/Library/LaunchAgents/homebrew.mxcl.memcached.plist

Optional: Graphiviz

I use erd to map out my models sometimes to get a graphical view of the relationships between them. This is completely optional.

brew install graphviz

Optional: FFMPEG

If you're uploading videos and need them transcoded, ffmpeg is a great tool to have. A few projects that I'm working on and support use ffmpeg to transcode uploaded videos.

brew install ffmpeg

Optional: ImageMagick

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

brew install imagemagick

Pulling an Existing Project

I typically create a Rails folder in my home directory.

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 pow URLs. I also keep a bookmark to http://localhost:8888/phpmyadmin to quickly access PHPMyAdmin.

In my dock, I keep it fairly minimal on a new development machine. I have Chrome, Safari, Sublime Text, iTerm2, and MacDown shortcuts. I have a few others, but none directly relating to development.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment