Using Locomotive as your default Ruby

Requiring RubyGems in IRB

I'm hacking some Ruby this morning in TextMate and try to run a simple non-Rails application that uses ActiveRecord. ActiveRecord is an object-relational mapping library packaged as a RubyGems package that provides much of the magic of the Rails workflow, especially when generating scaffolding. I run the application and get an error when the script tries to import RubyGems. The error, of course, is because RubyGems isn't installed by default in the default installation of Ruby in OS X and that is what TextMate uses to run Ruby files.

Historically, the default Ruby installation in OS X has a bad reputation since it was apparently somewhat broken in versions 10.4.5 and earlier. Reportedly, this was fixed in 10.4.6 and later releases (I'm running OS X 10.4.8 at the moment) and I haven't had any issues with it myself (and it was recently reported that Leopard will ship with Ruby on Rails pre-installed.) The only issue I have with the default installation is that it is somewhat bare and doesn't include all the lovely goodies that come with Locomotive, the drag-and-drop OS X installer and administration client for Ruby on Rails. To get a similar set of goodies, you have to go through a lengthy and convoluted installation process. I don't do "lengthy and convoluted" unless I absolutely have to, so I decided to make the Locomotive version of Ruby the default for Terminal and TextMate. It's not hard to do at all:

  1. Right-click a Rails project in Locomotive and choose the Open Terminal command from the context menu, Locomotive opens up a bash shell and sources (runs) a bash script that sets up your environment to use the Ruby environment in the Ruby bundle in Locomotive. On my box, the script is located at:


    The trick is to source this script from your bash profile script (the script that runs when you start a new bash session.)

  2. To start with, copy the script to a nicer location. For example:

    cp /private/var/tmp/folders.501/TemporaryItems/61BE958C-12B0-476F-87D2-7BCCB0D71161-949-00000C80122F8DFF.environment.bash ~/.locomotive.environment.bash
  3. Next, update (or create) your bash profile script:

    echo "source ~/.locomotive.environment.bash"  >>~/.bash_profile
  4. Finally, remove the last line in the .locomotive.environment.bash script so that it doesn't change your working directory to the Rails project that you stole the bash script from. You can also add a message here to remind yourself that your Terminal sessions are using the Ruby environment in Locomotive and not the default Ruby environment. My final .locomotive.environment.bash file looks like this:

    export RUBYLIB=/Applications/Locomotive2/Bundles/rails112.locobundle/i386/lib/r$
    export HOME=/Users/aral
    export DYLD_FALLBACK_LIBRARY_PATH=/Applications/Locomotive2/Bundles/rails112.lo$
    export PATH=/Applications/Locomotive2/Bundles/rails112.locobundle/i386/bin:/App$
    echo "NOTE: Using Locomotive environment variables for Ruby."

That's basically all there is to it. Now, every time you start a new Terminal window, it will use the Ruby environment that's installed in Locomotive. To test this out, start the Interactive Ruby Shell (irb) in Terminal and type require "rubygems". Ruby should respond with true.