Just Enough Developed Infrastructure

GEM_HOME and GEM_PATH with Passenger

We have the habit of using a GEM_HOME and GEM_PATH per environment. This allows you to clearly separate two environments such as development, testing, staging and production.

Passenger allows you to set the RailsENV accordingly. But it does not allow you to specify the GEM_HOME or GEM_PATH.

Option 1: set the ENV before starting apache.

To have different sites running , this would require an apache daemon per site. Not ideal.

Option 2: set the ENV in the ruby script

We can create a ruby-staging, ruby-production script and set the environment in there. And by setting the "PassengerRuby /opt/ruby-enterprise-1.8.6-20090201/bin/ruby-staging" But as the site states : This option may only occur once, in the global server configuration.

Option 3: our solution

To have rails pickup the setting we can set these environment variables in $RAILS_ROOT/config/environment.rb

ENV['GEM_HOME']='/var/www/<environment>.yoursite.com-80/gems'
ENV['GEM_PATH']='/var/www/<environment>.yoursite.com-80/gems'
To make this different for each deployment we add this to the after_deploy capistrano task $RAILS_ROOT/config/deploy.rb
desc "Keep only 4 releases"
task :after_deploy do
 run "cp #{current_release}/config/environment.rb #{current_release}/config/environment.rb-"
 run "echo ENV[\\'GEM_HOME\\']=\\'#{gemhome}\\' > #{current_release}/config/environment.rb"
 run "echo ENV[\\'GEM_PATH\\']=\\'#{gemhome}\\' >> #{current_release}/config/environment.rb"
 run "cat #{current_release}/config/environment.rb- >> #{current_release}/config/environment.rb"
 run "rm #{current_release}/config/environment.rb-"
 deploy::cleanup
end

Of course we still need to set the gem_home per environment. To have multiple settings we use the capistrano-ext plugin:
in $RAILS_ROOT/config/deploy/<environment>.rb. F.i for production we have
set :deploy_to, "/var/www/www.yoursite.com-80/rails"
set :rails_env, "production"
set :gemhome, "/var/www/www.yoursite.com.be-80/gems"
We still have one problem to tackle. Rake itself doesn't use the environment.rb. .To make it pick the GEM_HOME we prepend it to the rake command
set :rake, "GEM_HOME=/var/www/www.yoursite.com-80/gems /opt/ruby-enterprise-1.8.6-20090201/bin/rake"