Friday, December 4, 2009

Configuring user gems installation directory


Configuring user gems installation directory


I found it useful to separate between system gems that come with ruby (eg…)and gems that I install by myself to either test them out or for personal projects. The separation allows to clean out the local user gems dir quite easily.

There are two relevant env variables that we will need to set this up. The are:
GEM_HOME - specifies where the new gems are to be installed
GEM_PATH - similar to java CLASSPATH, location where gems are to b found in a system

My first steps were to set those variables as follows:
export GEM_HOME=$HOME/ruby/1.9.1
export GEM_PATH=$GEM_HOME:/usr/local/lib/ruby/gems/1.9.1

However this did not work, when I tried to install a new gem (gem install XX) it installed it into the system gems directory (/usr/local/lib/ruby/gems/1.9.1)

Another suggestion I came across was to create .gemrc file in user home directory that contains the following:
gemhome: /Users/ilya/.gem/ruby/1.9.1
gempath:
- /Users/ilya/.gem/ruby/1.9.1
- /usr/local/lib/ruby/gems/1.9.1

Gems look for .gemrc in the user’s home directory each time it runs to load the necessary paths. This worked a treat.


GEM_PATH


The apps/scripts that are launched via calling “ruby ” and irb will not have the access to the variables set in .gemrc, hence I ensured I have GEM_PATH environment variable configured, and I unbound GEM_HOME, as its only used by “gem” command which reads the gemhome value from .gemrc.