Specify Gem Installation Directory

I have some trouble here. I am working with a Rails 2.3 project (working on the production server through ssh - don't ask why). Here is the Gemfile. When delayed_jobs is trying to start, the output says I need to install the bundler gem. The problem is that the gemdir is /var/lib/gems/1.8/ and I don't have the write priviliges for that directory. However there is a directory under ~/projects/shared/gems/ruby/1.8/gems where I can write.

How can I define the installation path for a gem?

3

4 Answers

To install foo gem to a specified folder, just use --install-dir option, i.e.

$ gem install --install-dir /path/to/gems/folder foo

It helps when:

  • one cannot use bundle install - e.g. if one wants to install bundle gem itself, or wants to install a gem (which is not listed in Gemfile) into the bundle folder
  • sudo gem install command fails due to lack of write-permissions for a default installation path

Hope that helps.

2

You can add the following to your config.ru file:

ENV['GEM_HOME']="#{ENV['HOME']}/projects/shared/gems/ruby/1.8/gems"
ENV['GEM_PATH']="#{ENV['GEM_HOME']}:/var/lib/ruby/gems/1.8"
require 'rubygems'
Gem.clear_paths

This will tell your rack app where to look for gems.

Also configure your server .bashrc:

export GEM_HOME="$HOME/projects/shared/gems/ruby/1.8/gems"
export GEM_PATH="$GEM_HOME:/var/lib/ruby/gems/1.8"
2

The environment variable GEM_HOME defines the gem installation location. You need to set it to desired location. The command is OS specific.

In Windows it is set

set GEM_HOME=[path]/projects/shared/gems/ruby/1.8/gems

Linux would be export

export GEM_HOME=~/projects/shared/gems/ruby/1.8/gems
3

bundler accepts a --path option.

bundle install --path vendor/bundle

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest