If you have been using Ruby for a while you may have quite a large collection of gems. Chances are, you have a number of different versions of each gem. The following are simple ways to clean out any old versions of your gems:

Note: Depending how you have your dev environment setup you may need to use the ‘sudo’ command.

First, let’s find out which gems you currently have installed.

gem list

The output should be something like this:

 *** LOCAL GEMS *** bigdecimal (1.2.7, 1.2.6) bundler (1.10.6) chunky_png (1.3.4) compass (1.0.3) compass-core (1.0.3) compass-import-once (1.0.5) ffi (1.9.10) io-console (0.4.3) json (1.8.3, 1.8.1) lunchy (0.10.4) minitest (5.8.1) multi_json (1.11.2) power_assert (0.2.4) psych (2.0.15, 2.0.8) rake (10.4.2) rb-fsevent (0.9.6) rb-inotify (0.9.5) rdoc (4.2.0) rubygems-update (2.4.8) sass (3.4.19) susy (2.2.6) test-unit (3.1.5) 

Great! Let’s do some ‘proper’ housekeeping before we move any forward.

gem outdated

Output should include all outdated gems, let’s update them:

gem update [outdated gem] --no-document

Note: I like to use the –no-document option for a quick gem install, using this option will not install the documentation of the gem. Saving disk space.

Finally, let’s update RubyGems:

gem update --system

After updating, let’s begin the cleanup:

gem cleanup

The cleanup will respect any dependencies on old versions that a gem might have, asking you about each as it goes. If you just want to see a list of what would be removed (without actually removing them) you can use:

gem cleanup -d

You can also cleanup just a specific gem by specifying its name:

gem cleanup [gem name]

That will remove all but the latest version (again, respecting any dependencies on older versions) of the given gem.

Other “advance” options; ha. To remove any given gem:

gem uninstall [gem name]

To remove a specific version of a gem:

gem uninstall [gem name] --version X.X.X

Now to remove all versions less than X.X.X:

gem uninstall [gem name] --version '<0.9.6'

Congratulations! You’ve learned how to keep your shit clean.