Multi-home your gem repository

I don’t know about you but I tend to develop on multiple project in parallel that each have their own gem dependencies. Of course, some of these gem dependencies overlap; however, sometimes the dependencies vary by version.

I could keep all of these gems in the same repo, which on OS X defaults to /Library/Ruby/Gems/blahblahblah, but this occasionally becomes problematic. For instance, about a year ago, the Utility Belt gem and the Rails gems were not two flavors that go great together.

What to do?

In my case, I have a few extremely simple aliases that I have set up in my *NIX environment that allow me to quickly switch between several gem repositories.

For instance:

# This sets up my default RubyGems repo to point at ~/dev/reqall_gem_home instead of the OS X default
export GEM_HOME=~/dev/reqall_gem_home
export PATH=~/dev/reqall_gem_home/bin:$PATH

# This alias resets my RubyGems repo to point at ~/dev/over_nothing
alias over_nothing='export GEM_HOME=~/dev/over_nothing/gem_home;export GEM_PATH=$GEM_HOME;export PATH=~$GEM_HOME/bin:$PATH'  

Yes, it’s really that simple.

Sure, it could be slightly DRYer by writing a shell script to switch on a parameter and sets the environment appropriately based on the parameter. But, hey, this was something quick and dirty that I wrote after staring at the RubyGems documentation (I should’ve read the code instead….) until my eyes rolled out of my head.

If you find that gems from one project are contaminating another, consider something akin to the above.

Posted by evan on Thursday, November 06, 2008

blog comments powered by Disqus