Uninstalling gems from .gem directory

I keep forgetting how to do this, so I thought I’d write myself some notes.

In a recent version, RubyGems changed it’s behaviour on gem install so that if it doesn’t have sufficient permission to install the gem files, it installs them in ~/.gem and generates a warning :-

$ gem install <gem-name>
WARNING:  Installing to ~/.gem since /usr/local/ruby-1.8.6-p287/lib/ruby/gems/1.8 and
  /usr/local/ruby-1.8.6-p287/bin aren't both writable.
WARNING:  You don't have /Users/jamesmead/.gem/ruby/1.8/bin in your PATH,
  gem executables will not run.

Sometimes (e.g. when you forgot to prepend sudo) this isn’t what you want. However, it’s not obvious how to uninstall the gem again to correct your mistake :-

$ gem uninstall <gem-name>
ERROR:  While executing gem ... (Gem::InstallError)
  Unknown gem <gem-name> >= 0

To make this work, you need to specify the install-dir option :-

$ gem uninstall <gem-name> --install-dir=~/.gem/ruby/1.8/
Successfully uninstalled <gem-name>-x.y.z

Note that if you are using Ruby 1.9, it looks like the RubyGems directory path includes the point release version number e.g. 1.9.0 or 1.9.1.

Update: Prevention is better than cure. Coderr suggests protecting your ~/.gem directory.