Reinstalling list of GEMs on a new server

When I have to migrate servers to new OS or reinstall list of gems on another box you can get a quite big list of gems to install. This post will describe how to do it “admin way” which basically means that whatever has to be done more than once should be scripted ;)

To get a list of all installed gems you can use this command:

user@computer:$ gem list

*** LOCAL GEMS ***

actionmailer (2.3.2, 2.1.0, 2.0.2, 1.3.3)
actionpack (2.3.2, 2.1.0, 2.0.2, 1.13.6, 1.13.3)
actionwebservice (1.2.6, 1.2.3)
activerecord (2.3.2, 2.1.0, 2.0.2, 1.15.6, 1.15.3)
activeresource (2.3.2, 2.1.0, 2.0.2)
activesupport (2.3.2, 2.1.0, 2.0.2, 1.4.4, 1.4.2)
archive-tar-minitar (0.5.2)
builder (2.1.2)
camping (1.5)
capistrano (2.5.5, 1.4.1)
cgi_multipart_eof_fix (2.5.0, 2.1)
chronic (0.2.3)
color (1.4.0)
columnize (0.3.0)
daemons (1.0.10, 1.0.6)
dcov (0.2.2)
fastthread (1.0.7, 1.0)
GData (0.0.4)
gem_plugin (0.2.3, 0.2.2)
googlecalendar (1.1.0)
highline (1.5.1)
hoe (1.12.2, 1.8.3)
hpricot (0.8.1, 0.6)
httpclient (2.1.5.2)
json (1.2.0, 1.1.6)
json_pure (1.2.0)
kwala (0.9.1)
linecache (0.43)
log4r (1.0.5)
markaby (0.5)
memcache-client (1.7.6)
metaid (1.0)
mocha (0.9.8, 0.9.5, 0.5.5)
mongrel (1.1.5, 1.0.1)
mongrel_cluster (1.0.5, 0.2.1)
needle (1.3.0)
net-scp (1.0.2)
net-sftp (2.0.2, 1.1.0)
net-ssh (2.0.11, 1.1.1)
net-ssh-gateway (1.0.1)
packet (0.1.15)
parseexcel (0.5.2)
passenger (2.2.5)
pdf-writer (1.1.8)
rack (1.0.0)
rails (2.3.2, 2.1.0, 2.0.2, 1.2.3)
rake (0.8.7, 0.8.6, 0.8.4, 0.8.3, 0.8.1, 0.7.3)
redgreen (1.2.2)
rfacebook (0.9.8, 0.9.7)
rmagick (2.12.2, 1.15.7)
roo (1.2.3)
ruby-debug (0.10.3)
ruby-debug-base (0.10.3)
ruby-hmac (0.3.2)
ruby-ole (1.2.8.2)
ruby-openid (2.1.6, 2.0.4)
rubyforge (2.0.3, 1.0.3, 1.0.2)
rubyzip (0.9.1)
select_with_include (0.0.1)
simple-rss (1.2, 1.1)
sources (0.0.1)
spreadsheet (0.6.3.1)
system_timer (1.0)
termios (0.9.4)
test-spec (0.10.0)
tmail (1.2.3.1)
transaction-simple (1.4.0)
will_paginate (2.2.2)
xml-simple (1.0.12, 1.0.11)

Imagine that you have to install those gems one by one…. grrrrrrrrrrrr :[

Here is a small Perl script that will create commands for you which you can use to install all of listed gems at another server:

#!/usr/bin/perl -w

# We're strict
use strict;

# Get list of installed gems
my @gems = qx(gem list);
chomp(@gems);

# Create commands
foreach my $gem (@gems)
{
	# Match gem and versions
	$gem =~ m/(\S+)\s\((.+)\)/i;
	# Gem name
	$gem = $1;
	# Save them into array	
	my @gem_versions = split(/,/, $2);
	# Print out commands
	foreach (@gem_versions)
	{
		# Remove all whitespaces
		$_ =~ s/^\s+//;
		print "gem install $gem --version=$_\n";
	}
}

Now when you run it you will se something like this:

user@computer:$ perl gems_install_list.pl
gem install actionmailer --version=2.3.2
gem install actionmailer --version=2.1.0
gem install actionmailer --version=2.0.2
gem install actionmailer --version=1.3.3
gem install actionpack --version=2.3.2
gem install actionpack --version=2.1.0
gem install actionpack --version=2.0.2
...
gem install xml-simple --version=1.0.12
gem install xml-simple --version=1.0.11

That’s a lot of gems (I’ve cut the output to shorten scrolling) ! Thanks God I wrote that script ;)

Let’s reinstall these gems:

user@computer:$ vim install_gems.sh

Now paste what you did get form the script or print (pipe) to file and run it via sh on another box:

user@computer:$ sh install_gems.sh
Successfully installed activesupport-2.3.2
Successfully installed actionpack-2.3.2
...

And when this is cooking, we can handle the real stuff, someone just send a ticket about new vhost in Apache, boy this is so much more interesting than installing gems ;)

  • http://twitter.com/JetBlackStar Diccon

    You my friend, are god!
    Clean Perl, saved me pain and you concisely and cleanly REMmed your script!!

    • Anonymous

      Thanks.

  • cing

    Thanks, dude. I just used this to install an enormous list of gems to setup my dev environment for a project which isn’t using bundler.

  • Ben Reubenstein

    Greatest. Script. Ever. Thanks!

  • John Doe

    Today, I will go to the beach, thanks to you.

  • Artur Komarov

    a short version with sh:
    gem list|sed ‘s/(//’|sed ‘s/)//’|awk ‘{print “gem install ” $1 ” –version=” $2}’

    will print the same output