Lately I’ve noticed that my Ruby programs took really long to start up. IRb, for example, needed more than 5 seconds until I got to my prompt.

The culprit turned out to be RubyGems, or rather, stale gems that gathered dust. Removing old and unused gems sped up the startup time of IRb to under a second again, and all other ruby programs I tested are snappy again. Yay!

Clean your gems

First, run sudo gem cleanup to remove any outdated versions of your installed gems. During the cleanup RubyGems might tell you that a gem that is about to be removed is a dependency of another gem. If you look at the version number of the other gem and see that it is an old one anyway, you can safely remove it. Otherwise, if you remove a dependency you didn’t mean to, simply reinstall the original gem in question, and RubyGems will make sure that all dependencies are met.

One drawback of this is that it will remove older versions of gems you might want to keep, like an older Rails version you still need. In that case I would simply reinstall the older version afterwards.

Removing stale gems

Another useful command is gem stale. This will list your installed times, sorted by last access time. This allows you to identify rarely or never used gems, which you can then remove.

You should, however, note that “last access time” seems to be only defined when used from a non-gem program or library, while a gem using another gem doesn’t seem to count towards access time. Then again, since this is, in best Ruby-tradition, largely undocumented, I could be wrong.

And I’m too tired to dive into the source code right now.

Questions, comments, constructive criticism?
JavaScript Array filter
Fork me on GitHub