Setting up my Rails stack on Mac OS X Lion
My main role for the Shindig project is that of a Rails programmer and as such I’ve had to build the Rails stack on a bunch of machines over the past few months. Sadly it’s easy to get tripped up so I thought I’d share my notes.
Install Mac OS X Lion
After the base system is running make sure you run Software Update and get the latest fixes.
Now there are two way to get Xcode. One way is through the Mac App Store but I generally prefer to download the DMG from the downloads directory of Apple’s developer site as it makes sharing across my collection of Macs just a little bit easier.
Since version 4.3 Xcode is now delivered as a app bundle and not an installer. If you had a previous version of Xcode installed it’ll give you the option of uninstalling the old
/Developer/directory when you launch the new version. Some of the tools that had been a part of the previous larger installation are now separate. If you are looking for Dashcode or one of the graphics tools see the download index.
Install GCC Tools
Xcode now uses LLVM as its complier but many open source projects still look for and expect GCC. To get this working I like to use Kenneth Reitz’s GCC Tools install which adds GCC but doesn’t interfere with Xcode. You can find a nice installer package linked in the project’s readme on GitHub.
With Xcode 4.3 Apple now also provides a package of command line tools for LLVM. I’ve used these tools to compile Ruby 1.9.3 without incident but still feel like GCC is a safer option and remains my recommend approach.
Lion comes installed with Ruby version 1.8.7 but I use and recommend 1.9.3 (or greater) as it’s much faster and the preferred version for Rails. In the past I’ve built Ruby from source and installed it in
/usr/local/ but these days I’m using RVM. The major advantage of RVM for me is that by installing everything including gems in my home directory is makes it just that much easier to delete if the need arises.
$ bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)
Reload the shell or start a new one:
$ source ~/.bash_profile
Install Ruby 1.9.3:
$ rvm install 1.9.3
Start using it and make it the default:
$ rvm use 1.9.3 --default
We currently use MySQL for our Rails project. The current build is 5.5.x and is listed as the “MySQL Community Server” on the download page. For Lion I download and use the “Mac OS X ver. 10.6 (x86, 64-bit), DMG Archive”. The DMG includes the main installer plus a nice simple System Preferences pane which allows you to turn the DB server on and off as well as setup autostart on boot.
To interface with MySQL you can use the command line or any number of desktop GUIs. I tend to use the free and open source Sequel Pro.
On the command line if you type
which mysql the shell might not be able to find it. It is in fact installed in
/usr/local/mysql. To help the shell find it we’ll need to update or create a
.bash_profile file at the root of our home directory. I do this with TextMate by navigating to my home folder with a
cd and then creating the new file with
mate .bash_profile. Inside of the file we want:
This is basically saying when I issue a command, first look in my personal rvm directory, then look in
/usr/local/bin/, then look in
/usr/local/sbin/, then look in
/usr/local/mysql/bin/, then look in what ever paths you would previously look into.
Before we leave I’m going to recommend another quick addition to the
As Steve Madsen write in his great write up of the bug:
The problem is that the libmysqlclient shared library in the MySQL 5.5 package does not specify a full path to the library. When something links with it, such as the MySQL gem, it won’t be able to find the library at runtime.
This is one of two ways to solve the issue. I’ve done both but this one has the added benefit of working for multiple gem complies.
Install Gems via Bundler
At this point I typically checkout my project from GitHub and run
bundle is a command of Bundler, a great system where in you list all the gem dependancies of your project and Bundler make sure those versions are installed. There is a similar system for Cocoa that is under development called CocoaPods.
Aside: One of the things that annoyed me at first but I later came to appreciate was how when using bundler with RVM you have to append commands with
bundle exec. If you have multiple version of Rails installed and want to issue a command like
rails generate model Personit’s almost a crap shoot which version of Rails will be used but by preceding the command with
bundle execwe know which version to use. If you really dislike the verbosity of using that command each time there are ways to integrate it into your shell.
When you run
bundle install it should install and compile all the gems you need. If your project required the
mysql2 gem then our previous workaround should have avoided the complier issue.
Install current Rails
If you want to do a new project then just run
$ gem install rails
Be sure you just say
gem and not
sudo gem. What you need to know is this gem command is actually hosted inside of your RVM directory whereas if you did
sudo gem the system might use the default version of gem Lion shipped with.
In the process of editing this post a friend pointed out a script the ThoughtBot guys use to prep their Rails stack on a fresh OS X install. Some of it might be overkill depending on your needs but it is very interesting and something to checkout if you do lots of Rails installs for developers.
Posted on: March 12, 2012 – 12:39 PM