Here is a quick and painless solution for setting your own Ruby GEM_HOME in Ubuntu Feisty.
First, I’ll assume that you have installed the ruby and rubygems packages, and set your new GEM_HOME:
$ sudo apt-get install ruby rubygems
...
$ mkdir -p /home/mars/lib/ruby/gems
$ export GEM_HOME=/home/mars/lib/ruby/gems
All looks well, until we try to install something with the ‘gem’ command:
mars@sol:~/tmp$ gem install rake
/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- sources (LoadError)
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `require'
from /usr/lib/ruby/1.8/rubygems/remote_installer.rb:462:in `sources'
from /usr/lib/ruby/1.8/rubygems/remote_installer.rb:472:in `source_index_hash'
from /usr/lib/ruby/1.8/rubygems/remote_installer.rb:436:in `install'
from /usr/lib/ruby/1.8/rubygems/gem_commands.rb:263:in `execute'
from /usr/lib/ruby/1.8/rubygems/gem_commands.rb:225:in `each'
from /usr/lib/ruby/1.8/rubygems/gem_commands.rb:225:in `execute'
from /usr/lib/ruby/1.8/rubygems/command.rb:69:in `invoke'
from /usr/lib/ruby/1.8/rubygems/cmd_manager.rb:117:in `process_args'
from /usr/lib/ruby/1.8/rubygems/cmd_manager.rb:88:in `run'
from /usr/lib/ruby/1.8/rubygems/gem_runner.rb:28:in `run'
from /usr/bin/gem:23
mars@sol:~/tmp$
Oops. We are missing a file called ‘sources.rb’. That file, curiously enough, is contained in the ‘sources’ gem.
So, we try this:
mars@sol:~/tmp$ gem install sources
/usr/lib/ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- sources (LoadError)
...
Same error. Drat!
Thankfully, there is a simple solution. The sources gem was installed in feisty’s default gem cache:
mars@sol:~/tmp$ locate sources.rb
/var/lib/gems/1.8/gems/sources-0.0.1/lib/sources.rb
mars@sol:~/tmp$ ls /var/lib/gems/1.8/cache/
sources-0.0.1.gem
We can install the original gem into our new GEM_HOME. Just make sure that you pass the ‘–local’ switch to the gem command, so that it doesn’t check for remote sources!
mars@sol:~/tmp$ gem install --local /var/lib/gems/1.8/gems/cache/sources-0.0.1.gem
Successfully installed sources, version 0.0.1
Now everything works as expected; we can install gems without using sudo:
mars@sol:~/tmp$ gem install rake
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed rake-0.7.3
P.S. Don’t forget to set your PATH!
$ export PATH=$GEM_HOME/bin:$PATH