Configuring a Ruby on Rails Development Environment on a Macbook pro Running OSX 10.6.2

This article took a bit of time to get together and is the result of several days of fussing around, going down dead ends, and retrying things previously abandoned out of ignorance. Not to mention the rapidly evolving so-called ‘eco-system’ surrounding both Ruby and Rails.

My day job is Director of a small sized Customs Brokerage House and International Freight Forwarder. I also am the designer and programmer of our main business application, written when I was much younger, the company employed far more people than it does today, and my management responsibilities much less. In my spare time, after dealing with continual attempted incursions of Chinese, Russians, Romanians et al, I am redesigning our existing system to run as a web application. Since this entailed learning a completely new programming language, Ruby, and working with a new and unfinished framework, Rails, naturally enough I chose to adopt a completely new methodology, Behaviour Driven Development (BDD) to round out my several dimensions of ignorance.

All this is to say that my laptop is, if not my principle design and programming platform then, an important part of my productivity. I have to be able to work in those odd moments when nothing else is demanding my attention, wherever that might be. This was the function served by my previous HPQ nx9420. Setting up the Macbook to handle my requirements was essential and this proved somewhat awkward. However, I finally managed to get everything installed and working.

As you may have read elsewhere on this web log, my Macbook Pro departed from my side for a prolonged absence shortly after the first draft of this entry was prepared. By the time it was returned the hard disk had been wiped, the operating system replaced with OSX-10.5.x and all my previous configuration work undone. In the meantime both Ruby 1.9.2 and Rails-3.0.0 had been released, both of which depart far enough from prior conditions to render what I had previously written obsolete. So, as electronic bits are cheap I scrapped most of V-1.0.0 and present V.2.0.0 of this article instead.

First, the cast of characters. We have already met Ruby and and some of the various Gems, including Rails, that I require. However, a newcomer to the ensemble was RVM, the Ruby Version Manager. This piece of software is, in essence, a shell script that, among other things, allows you to build and configure multiple versions of the Ruby programming language, and its associated version dependent gems, to co-exist quite civilly on a single system, and indeed for a single user. Additionally, along with Rails-3.0.0 the bundler gem became a dependency.

A word on these last two miscreants. While each offers a whole new world of ease of use to Ruby programmers and project managers, both products are notably immature and subject to hissy-fits between versions. The wisdom of forcing Rails users to adopt Bundler is questioned in some circles and, while I have not encountered the problems related in the reference above, using Bundler is not without its complications. Upgrades to RVM are also sometimes accompanied with major flaws and provide no easy way back to a previously working state. That said, both are already all but indispensable and no doubt, with time, the stability problems will abate.

To continue then. In this brave new world the first thing to do is to install RVM. The web site gives clear and simple instructions on how to do this. I have installed RVM successfully on Linux (CentOS-5), OSX-10.6, and Windows (CYGWin). So, RVM is a near a universal solution to Ruby installations as one is likely to encounter. Once RVM is installed, and the necessary modifications are made to the applicable shell profile file, installing any version of Ruby known to RVM is as simple as typing $ rvm install 1.8.7. Note that when using RVM you are working with your own personal Ruby environment so you must not use sudo to install gems or Ruby versions.

With that completed, assuming that your system has the necessary software installed (gcc and associated developments libraries of Linux and CYGWin, XTools for OSX), RVM will download, build and install in its own user specific directory tree (~/.rvm) the requisite Ruby and the essential gems, which as distributed are rake and bundler. Oddly enough, these two gems are specified in different files. Rake is found twice, once in in .rvm/src/rvm/gemsets/global.gems and again in .rvm/src/rvm/gemsets/default.gems. Bundler shows up in .rvm/gemsets///global.gems for every known ruby, as far as I can discover. Why this distinction is unknown to me.

Once you have built your desired rubies you may select which one to use as your default. An $rvm list command will provide your choices.

$ rvm list

rvm rubies

   ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-p0 [ x86_64 ]

$ rvm use 1.8.7 --default
Using ~/.rvm/gems/ruby-1.8.7-p302
$ rvm list

rvm rubies

=> ruby-1.8.7-p302 [ x86_64 ]
   ruby-1.9.2-p0 [ x86_64 ]

Now that the desired Ruby interpreter is built and configured as a default, the next thing to do is to install the Ruby Gems that your project requires. This can be done by using the gem command itself $ gem install gemname -v x.y.z. Or, one can create a Gemfile in any Ruby project directory and use Bundler to manage all the gems required for that project. In either case, the gems are installed in a version dependent directory managed by RVM.

At this point, you are ready to go, saving only the need for a decent programming editor. Many Macbook users swear by TextMate. I favour Vim, which in its Mac incarnation is called MacVim. I will not get into any great amount of detail respecting Vim or MacVim. Suffice to say that with suitable plugins (vividchalk.vim, vim-rails.vim, vim-surround.vim, vim-unimpaired.vim, vim-cucumber.vim, and vim-fugitive.vim) vim more than meets my needs. And it runs on all the OS platforms that support RVM so when moving from host to host I do not have to change editing gears.

Returning to RVM and Ruby Gems. I had one noticeable difficulty on the MacBook that I experienced nowhere else and that was with the Ruby Pg gem. We use PostgreSQL-8.4. On the MacBook this is most easily obtained from MacPorts. However, even with PostgeSQL installed from MacPorts building the Pg gem, with or without Bundler, is not straight forward. PostgreSQL gets its libraries installed in a non standard location which has to be specified to the gem installer. On top of that, Bundler needs to know about the underlying architecture in order to build correctly on Snow Leopard, and for all I know any other Mac system. There are numerous blogs out there that write about this. The short form solution that worked for me is:

$ export PATH=$PATH://opt/local/lib/postgresql84/bin
$ env ARCHFLAGS="-arch x86_64" bundle install

Please note carefully that if you are using RVM then you do not use sudo to install gems. If you are using RVM to manage the system Ruby then you do not need to read this anyway.