By default RubyGems installed from source on Linux puts files – including installed gems – under /usr/bin and /usr/lib. However, normal administrative practice is to leave these directories under control of the package manager (apt, rpm etc.) and place manually installed files under /usr/local. This page describes how to install RubyGems to follow this pattern.
Note: These instructions were tested under Ubuntu Dapper. I imagine they would also work on Debian. Please edit this page to add any modifications needed for other distributions.
Assuming you have downloaded the RubyGems archive, unpack it and cd into the directory created, then execute the following:
export GEM_HOME=/usr/local/lib/site_ruby/gems sudo ruby setup.rb all --prefix=/usr/local --siteruby=/usr/local/lib/site_ruby
This puts gem and the other utility commands in /usr/local/bin, and creates the gem directory structure under /usr/local/lib/site_ruby/gems (i.e. GEM_HOME).
To keep gems themselves within this structure, the environment variable GEM_HOME must be set whenever RubyGems functionality is used. One way to do this is the following:
sudo sh -c "echo GEM_HOME=$GEM_HOME >>/etc/environment"
At least on Debian/Ubuntu, /etc/environment contains global environment variable declarations. A (more portable? better?) alternative might be to add this declaration to /etc/profile instead.
Finally, note that commands provided by gems themselves (such as rails) will be placed in $GEM_HOME/bin. For convenience, you will probably either want to add this directory to your path, or simply make it a symbolic link to /usr/local/bin, like this:
sudo ln -s /usr/local/bin $GEM_HOME/bin
(If it has already been created you will have to move over any contents and remove it first.)
Note: There appears to be a bug in RubyGems such that gem uninstall will not remove gem-provided commands when set up this way; you can simply remove them by hand.