Uninstalling a Gem

Problem

You want to remove an installed gem from your Ruby installation.

Solution

From the command line, use the gem uninstall command:

$ gem uninstall blinkenlights Attempting to uninstall gem linkenlights Successfully uninstalled blinkenlights version 0.0.2

From Ruby code, the most reliable way to uninstall a gem is to simulate a command-line invocation with the Gem::GemRunner class. This code installs a gem, then immediately removes it:

require ubygems require ubygems/installer require ubygems/remote_installer Gem::RemoteInstaller.new.install(linkenlights) require ubygems/gem_runner require ubygems/doc_manager Gem.manage_gems Gem::GemRunner.new.run([uninstall, linkenlights]) # Successfully uninstalled blinkenlights version 0.0.4

Uninstalling a gem can disrupt the normal workings of your Ruby programs, so I recommend you only uninstall gems from the command line. That way, theres less chance of a bug wiping out all your gems.

Discussion

Since rubygems can manage multiple installed versions of the same gem, you won usually have to remove old copies of gems. There are three main reasons to remove gems:

  1. You find out that a particular version of a gem is buggy, and you want to make sure it never gets used.

  2. You want to save disk space.

  3. You want to clean up the list of installed gems so that its more obvious which gems you actually use.

If uninstalling a gem would leave another installed gem with an unmet dependency, youll be told about the dependency and asked whether you want to go through with the uninstall anyway. Youll get this interactive prompt whether you run the gem uninstall command or whether you use the Gem::Uninstaller class from Ruby code.

Gem::Uninstaller.new(actionpack, {}).uninstall # You have requested to uninstall the gem: # actionpack-1.8.1 # actionmailer-0.9.1 depends on [actionpack (= 1.8.1)] # If you remove this gem, the dependency will not be met. # Uninstall anyway? [yN]

The sources gem is a special gem that tells rubygems to look for remotely installable gems at http://gems.rubyforge.org/ by default. If you uninstall this gem, you won be able to install any more gems, except through complicated hacks of the classes in the Gem module. Just don do it. Not even if you never plan to install any gems from rubyforge.org. Not even if youd never thought of doing it until I brought it up in this recipe, and now you e curious.

You did it, didn you? Now youll have to reinstall rubygems by rerunning its setup.rb script.

Категории