Keep Ruby Gems in Your Home Directory «

Code: , , , ,
Comments Off on 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/your username/.gems
- /home/your username/.gems
- /usr/lib/ruby/gems/1.8

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/your username/.gems
export GEM_PATH=/home/your username/.gems:/usr/lib/ruby/gems1.8

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

GEM_HOME=/home/your username/.gems
GEM_PATH=/home/your username/.gems:/usr/lib/ruby/gems1.8

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.