Gems, GEM_HOME and many Ruby on Rails Projects

I have several current, and archived Ruby on Rails projects. Having a central gem home doesn’t work since some of these projects require older versions.

To work around this each project uses its own GEM_HOME.

My project directories are setup like the below example, for “Project X”.

HOME/projects/project_x
HOME/projects/project_x/gems
HOME/projects/project_x/rails

Then in my .bash_profile I have an alias for each project, and a function which sets up the environment.

function set_gem_home {
  export PATH=$HOME/projects/$1/gems/bin:$PATH; export GEM_HOME=$HOME/Sites/$1/gems;
}

alias project_x='set_gem_home project_x; cd ~/projects/project_x/rails'

Then in the terminal I enter project_x and I am good to go.