Keep Ruby Gems in Your Home Directory

I like keeping my Ruby Gems in my home directory. I don’t have to type sudo in front of every gem command, it’s easier to remember the path to them when I want to read their source, and I don’t have to worry about a sysadmin on a shared host updating a gem before I test it. Here’s how to make those benefits yours:

Create or edit your .gemrc to read:

` gemhome: /home/{lang="yaml"}*your username{lang="yaml"}*/.gems gempath: - /home/{lang="yaml"}*your username{lang="yaml"}*/.gems - /usr/lib/ruby/gems/1.8 `{lang=”yaml”}

Next, set up your login environment. Add these lines to the end of your .bashrc, .bash_profile, or .profile (whichever file you happen to already have — if you use another shell, you should already know what file is the equivalent).

` export PATH=~/bin:~/.gems/bin:$PATH export GEM_HOME=/home/{lang="bash"}*your username{lang="bash"}*/.gems export GEM_PATH=/home/{lang="bash"}*your username{lang="bash"}*/.gems:/usr/lib/ruby/gems1.8 `{lang=”bash”}

Last, add the following to your .ssh/environment so that gems work properly when you’re executing commands over ssh (for example, with capistrano).

` PATH=~/bin:~/.gems/bin:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin GEM_HOME=/home/{lang="bash"}*your username{lang="bash"}*/.gems GEM_PATH=/home/{lang="bash"}*your username{lang="bash"}*/.gems:/usr/lib/ruby/gems1.8 `{lang=”bash”}

You’ll need to log out and back in to update your login environment, and then you’ll be able to run gem normally. All the files get stored in the directory \~/.gems if you want to look around.