Painless Cleanup of Ruby Gems With Similar Names

Dec. 18, 2008

I have been riding the bleeding edge of Merb and Datamapper for about seven or eight months. Whenever the source was updated at Github , I would pull in the changes, rebuild, then reinstall the gems. There was a time when the Merb and Datamapper contributors were bumping versions regularly and I was having problems with versions interfering with each other. I wanted only the latest and greatest gems installed.

Luckily Merb gems all start with ‘merb’ and Datamapper gems all start with ‘dm’. All I needed to do was write a shell command which lists all gems beginning with ‘merb’ or ‘dm’ and remove all but the latest and greatest. Listing gems based on the beginning of their name is easy: ‘gem list STRING’. The command to list all merb gems is: ‘gem list merb’.

OK, we can list our gems, but how do we clean them up? My list looks like this:

merb (, 1.0.6, 1.0.5)
merb-action-args (, 1.0.6, 1.0.5)
merb-assets (, 1.0.6, 1.0.5)
merb-auth (, 1.0.6, 1.0.5)
merb_datamapper (, 1.0.6, 1.0.5)

Now that we have a list, we need to get rid of the version info. We do this with the shell command ‘cut’. ‘cut’ removes sections from each line of files or STDOUT. We remove sections by setting a delimiter ‘-d" "’ and a field ‘-f N’ (N is the section we want to keep). In this case, we want to cut out everything to the right of the gem name. There is a space between the gem name and the version info so we will use the [space] as a delimiter. Field 1 will be everthing to the left of the delimiter (our gem name) so we will insert ‘1’ as the field number.

Let’s use the two commands to generate a list of gems beginning with ‘merb’. We accomplish this by ‘piping’ or chaining the two commands with the ‘|’ character. Our command now looks like this ‘gem list merb | cut -d" " -f1’. The list now looks like this:


The command to uninstall all gems except the latest and greatest is: ‘gem cleanup STRING’... so cleaning up merb-helpers is: ‘gem cleanup merb-helpers’. That only cleans up one gem and we have a list which we want to iterate through and cleanup. We do this with the ‘xargs’ command which builds and executes command lines from STDIN or the list we generated with the command ‘gem list merb | cut -d" " -f1’.

Our ‘xargs’ command to run gem cleanup is ‘xargs gem cleanup’. ‘xargs’ iterates through each line of our list and cleans up the gem. The gem name will be appended (but not visible). Again, we ‘pipe’ or chain the cleanup command to our previous command.

gem list merb | cut -d" " -f1 | xargs gem cleanup’ leaves us with only the latest and greatest merb gems!

Attempting to uninstall merb-action-args-1.0.6 
Successfully uninstalled merb-action-args-1.0.6
Attempting to uninstall merb-action-args-1.0.5
Successfully uninstalled merb-action-args-1.0.5                                                                                          
Attempting to uninstall merb_datamapper-1.0.6
Successfully uninstalled merb_datamapper-1.0.6
Attempting to uninstall merb_datamapper-1.0.5
Successfully uninstalled merb_datamapper-1.0.5
Attempting to uninstall merb-core-1.0.6
Successfully uninstalled merb-core-1.0.6
Attempting to uninstall merb-core-1.0.5
Successfully uninstalled merb-core-1.0.5 

A couple of caveats: first, I use Fedora Core Linux so the commands should work for all Linux flavors. The commands should also work for Mac users using ‘sudo’. This command will remove EVERY gem listed except the latest, so be judicous. If your app depends on a version less than the latest, you will have to selectively remove the gem manually. I highly recommend you list the gems first before you cleanup (‘gem list merb | cut -d" " -f1‘).

You now have the tools to list all gems beginning with a gem name and all its versions, remove the version number leaving only the list of gems, and then executing the ‘xargs’ command to cleanup all the selected gems.

Short url :

Next entry

Previous entry

Similar entries


Comments are closed.


Pingbacks are closed.