A Canvas for Thought

July 13, 2007

Setting a custom Ruby GEM_HOME on Ubuntu Feisty

Filed under: Uncategorized — vednis @ 1:23 pm

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

About these ads

6 Comments

  1. I was having this problem on my mac, and found your page via Google. hanks for taking the time to post this – you saved me!

    Comment by D — February 26, 2008 @ 10:54 am

  2. Errr…I mean “thanks for taking the time to post this”….it’s too early in the morning…

    Comment by D — February 26, 2008 @ 10:55 am

  3. This is great! I don’t have root at my work machine/ work cluster so I manage my own gem repo (getting IT to do it is like pulling teeth) and I’d run into this problem from time to time. I had to make sure paths were pointing to system-intalled stuff first or do some trickery with absolute paths (my gems more current than system ones). This makes that problem go away. Brilliant!

    side rant: it irritates me to no end that all installations assume everyone has root.

    Comment by mikshir — May 23, 2008 @ 9:47 am

  4. I found even simpler way here: http://ronallo.wordpress.com/2007/11/20/rubygems-without-sources-gem/

    Comment by emmek — May 13, 2009 @ 10:23 am

  5. Old article by now, but for those still coming across it… Taking a custom GEM_HOME a step further, I needed to build/test many apps in isolated gem environments, and I found myself longing for something like Python’s virtualenv. Turns out, Jacob Radford done did that:

    http://github.com/nkryptic/sandbox

    See the README on GitHub, but basically sandbox helps you create multiple gem environments and generates shell scripts for each one that you can source to set and unset GEM_HOME, GEM_PATH, etc.

    Comment by Ches Martin — October 25, 2009 @ 12:23 pm

  6. Although I guess rip is really the new hotness for that purpose :-)

    Comment by Ches Martin — October 25, 2009 @ 12:52 pm


RSS feed for comments on this post.

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: