Wednesday, May 12, 2010

A debian/ubuntu gem environment of your very own

There are some differences with the way apt-get rubygems and downloaded and 'sudo ruby setup.rb' gems behave. Here is how to bring the directory structure in conformity with the apt-get version and have your gem's bin folder in your path. This is for using gems on a per-user basis.

Add these two lines to your ~/.bashrc file:
# this will export the path to whatever version of ruby you are using:
export GEM_HOME="$HOME/.gem/ruby/`ruby -e 'x=RUBY_VERSION;print(x=~/^1.8/ ? "1.8" : x)'`"
export PATH="$PATH:$GEM_HOME/bin"

Then 'source ~/.bashrc' if needed and you can use either version of rubygems indistinguishably.

Now, you can install gems with either rubygems, you don't need sudo privileges, and your gem executables will be active. Wallah!

gem install rake


Mike Chelen said...

Had a small problem, ruby -e 'print RUBY_VERSION' returns 1.8.7 while the gems are installed under ~/.gem/ruby/1.8/

Tested on Ubuntu 10.04 after installing default ruby and rubygems packages.

These packages were installed before editing ~/.bashrc if that makes any difference?

JTP said...

Thanks for catching that Mike. I've updated the line to work properly with 1.8, although it is a tad wordier.