2 Oct 2006

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:
    /private/var/tmp/folders.501/TemporaryItems/61BE958C-12B0-476F-87D2-7BCCB0D71161-949-00000C80122F8DFF.environment.bash

    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.

Add Your Comment

Spam Protection by WP-SpamFree

Using Locomotive as your default Ruby

  1. Aral,
    I just started digging into Ruby on Rails, and I’m using Textmate, and this is just what I needed. Thank you. Really nice–cuts through the fog and helps me use the online tutorials more easily too.

    David
  2. Simple and straight-forward. As long as you’re working with only a single Locomotive Bundle, there’s no reason not to use the Locomotive Ruby and associated gems.

    Duncan Beevers
  3. Thank you!!! I just got my new apple computer and in 5 minutes i got ruby on rails up and running…

    Mads
  4. I’m an idiot. How do I reset the Terminal after not following the above steps correctly?

    ummm
  5. Two days, that’s how long it took me to find your solution. Thanks. It works!

    Chas. Porter
  6. Hi Chas, glad to hear that it helped :)

    aral
  7. dude,

    thanks so much for the awesome tip! got it up and running right quick. Now I’ve left my original ruby install on my mac as is, and I can use all the tutorials with Locomotive ease! Great job and keep up the good work!

    Serge
  8. Hi Serge,

    Great news! Have fun :)

    aral
  9. Hi aral,
    I am very new to macs so please help:

    When you say
    “Next, update (or create) your bash profile script:”
    how do you do it?
    Where do I find it?

    Chris.

    Chris
  10. Chris,
    the bash profile script is ~/.bash_profile, as given in the command above

    Mark
  11. Brilliant. Just what I was looking for!

    Alistair Holt
  12. [...] Using Locomotive as your default Ruby at Aral Balkan – How to use the (nicer) Ruby environment included with Locomotive instead of the one shipped with MacOS X. [...]

    My del.icio.us bookmarks for January 1st
  13. Hi Alistair, happy to hear you found it useful :)

    aral
  14. Thankx dood!

    RubyNeo
  15. please post some google adsense, so we can click on them for you to reimburse you for such great advice ;)

    Phil
  16. Hi Phil,

    Your kind words are thanks enough!

    aral
  17. Just adding kudos. Thank you.

    Lanny
  18. thanks Aral. I had been mucking around with the stock ruby on tiger and found this awesome tip. That was awesome .. in less than 5 mins i was up and running .. thanks so much!

    nii amon
  19. Hi Nii,

    You’re very welcome! :)

    aral
  20. Amazing. Thanks for the tip!

    Scott
  21. [...] Using Locomotive as your default Ruby at Aral Balkan (tags: tips RoR osx ruby) [...]

    links for 2007-08-07 « Amy G. Dala
  22. Thanks for posting this, but I ran into trouble following your advice. Especially since this code is likely to be used by people new to command-line programming (like me) who will copy it directly into terminal, unnecessary characters between your [code] declarations can wreak havoc. Your code block that reads:

    [code]echo “source ~/.locomotive.environment.bash” >>~/.bash_profile[/code]

    will *not* work unless you remove ">>". Maybe I have a freak installation of darwin on my version of 10.4.10, but that's how it went. After some head-scratching, I figured that those were either an artifact from pasting in code or an indication to continue on the next line. Either way, it's the only place that kind of thing appears in your post, so it's not consistent. Additionally, my system would not deal with locomotive.environment.bash as a hidden file with the leading ".", but I don't know if that's something other people would run into. Again, this is really useful code, and everyone seems to have had better results than I initially did, but I thought this was an important clarification.

    Ním
  23. [...] (Also see my related posts on Locomotive and Ruby that may come in handy: Creating applications with RoR on OS X, Using Locomotive as your default Ruby, The location of the Rails source under Locomotive, and Clearing the gem source_cache in Ruby.) [...]

    SWX Ruby Alpha Released: SWX RPC for Ruby on Rails at Aral Balkan
  24. Hi,

    Thank you for that tip. Unfortunately, or maybe fortunately, this was my first evening to be using RoR and I was just finished with the Locomotive install on my mac os x when I found your tip (I was looking for information on rails install help since I was receiving a “command not found” message upon typing “rails” (without quotes) into Terminal.

    Before I tried your tip, I wasn’t finding rails anywhere on my system by typing it in Terminal. After doing your tip, it has just installed Rails in the folder that coincided with the new project you said to “right-click” on (in your step 1).

    Now, I have all these neat (what appear to be) rails framework folders running in my photos folder. Ha! It’s kind of funny because when I typed rails from the command line, it now shows up in that photos folder. I laugh because I am assuming changing this location should be easy enough. Lord knows I can’t create all of my projects in my photos folder. I would like to move the install to the applications folder (where I thought I was putting it to begin with) or wherever it is supposed to be. I do use TextMate so would be glad if it opened up from there. I have only installed Locomotive though so rubygems is not on my computer.

    Would you mind telling me how to change it to a better location? I tried to do the cp command in Terminal (the way you mentioned in your tip above) but it didn’t work the same way this time. It kept pasting in extra code (that I did not type) with the code I had copied.

    Would you please help me move these files? I would be very appreciative!
    Holly

    Holly
  25. I’ll try to help Holly through the Locomotive-Users mailing list.

    Just a note to other visitors – check that you don’t get special characters and line feeds in the copy-and-paste from this page. Copy from this page, then paste into your text editor. Clean up the text there to look like the stuff on this page (leaving out the [code]code[/code] tags), and then paste into Terminal.

    Aral, you might want to check the process you use for editing your articles, it appears to be using smart quotes (evil, evil smart quotes!), and the renderer isn’t handling [code]code[/code] tags properly. Sorry to be a pain :\

    Alex Satrapa
  26. Hi Alex,

    Thanks for helping out and for alerting me about the code tags. I was using a different code highlighter on my old blog and these are the posts from there. I updated the code in this post so it now displays correctly.

    Aral
  27. Thanks for the update. Looking good now! =)

    Alex Satrapa