[Read This Fine Material] from Joshua Hoblitt

how to remove all Ruby Gems except those installed by system packages


I’m sure it’s never happened to you but for me, occasionally, bundler or even the gem command pulls in a different version of a Gem that was installed by a standard system package. Even more rarely, this breaks some other software that was installed on the system.

This one-liner will remove ALL gems installed on a system.

gem search --local --no-version | tail -n +3 | xargs gem uninstall --all

The tail -n +3 command is needed to remove the \n*** LOCAL GEMS ***\n header that gem spits out in search mode.

That one-linger does not affect the platform’s package database but does nuke all of the gem associated files that may have been installed by a regular package. Since the system package database was untouched, now it’s just a matter of finding out which packages installed a gem reinstalling them. On EL systems, the names of these package are highly consistent and will begin with rubygem-.

rpm -qa | grep '^rubygem-' | xargs yum reinstall -y

At this point, in theory, we have scrubbed the system clean of all gems that were installed and reinstalled any gem providing RPM that’s consistent with the platforms naming guidelines. However, it is possible that a poorly named package was missed so do warrant some caution in using this procedure.

Here are the two one-liners together for an easy cut’n’paste.

gem search --local --no-version | tail -n +3 | xargs gem uninstall --all
rpm -qa | grep '^rubygem-' | xargs yum reinstall -y

Leave a Reply