How to Uninstall All System Ruby Gems (Because RVM Is Better)

Brook always shows me the way. And then, sometimes, the way changes and he shows me the new one. But what about the old way? What about all the littering of the old way all throughout my /usr/local/lib and elsewhere?

I am speaking, of course, of the outdated practice of installing Ruby gems to the system, which has been replaced by the method of installing gems using RVM and Ruby-version- and project-specific gem sets. Hooray, RVM! Hooray, bundler! Such has been particularly important for my current, local dev environment, on which I must run multiple apps on different versions of Ruby & Rails.

Okay, first: I currently have 12.79 GB available on my disk. Let’s not get started on the size of my music directory.

Now, let’s uninstall all “sudo” installed system gems:

$ rvm use system && sudo gem list --no-versions | xargs sudo gem uninstall -a

What does this command do?  “rvm use system” guarantees that the subsequent command is executed in the system ruby context. I want to uninstall system gems, not the gems used by any specific project or gem set.

“sudo gem list –no-versions” outputs a list of all installed Ruby gems, which is then piped “|” to “xargs sudo gem uninstall -a” as arguments. Each will be uninstalled in turn. You won’t miss them.

Some gems seems to not like this chaining, and report:

ERROR: While executing gem ... (NoMethodError)
 undefined method `name' for nil:NilClass

If you run the command again, the uninstall will pick up where it left off and continue uninstalling.

I check my free disk again: 14.05 GB available. WHUT. Nice: I just uninstalled 1.25 GB of unused gems.

No comments yet.

Leave a Reply