Uninstalling gems from .gem directory

Posted by James Mead Mon, 16 Feb 2009 15:56:48 GMT

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.

Tags , , , , , , , ,  | 6 comments

Comments

  1. Tom Lea said about 3 hours later:

    Personally I just go for the rm -Rf ~/.gem option. But glad to see it’s not just me making this mistake ;)

  2. James Mead said 1 day later:

    Fair enough.

  3. Jon Phillips said 16 days later:

    Many thanks for this! You just saved me an hour figuring that out.

  4. James Mead said 17 days later:

    Jon: Glad you found it useful.

  5. John5342 said 19 days later:

    Also if you prefer to install gems in your home directory just put:

    gemhome: /home//.gem/ruby/1.8

    into ~/.gemrc

    Installing as your own user will install and uninstall from your home directory by default and su -c will install and uninstall from the global directory.

  6. Mike D said 38 days later:

    @Tom Lea: Actually I found out the hard way that simply removing the gem via the file system can lead to wicked headaches since the GemPathSearcher builds gemspec objects which includes their respective load path. This info is cached somehow (I’m not sure of those details yet). I found a handful of gems that ruby thought were in my user/.gem directory that I had removed via rm -Rf. I ran the following to see what was supposedly in my user/.gem directory:

    require ‘rubygems’

    locals = Gem::GemPathSearcher.new().instance_eval(”@lib_dirs”).select{|k,v| v.match(/username/)}

    Using the uninstall that James mentions will get rid of those entries.

    I spent forever on this today—hopefully I’ll save someone else’s day.

Comments are disabled