Quickly Mirroring your Ruby Gem Installation

July 10, 2008

A friend just complained that he’d setup a server with all his gems and it would be nice if there was an easy way to do the same install on another server without having to retype everything. He wanted something like:

gem install -l from_file.txt

It’s not quite that, but this works pretty well:

gem list -l |\
sed -e '1,3d' \
    -e 's/\([^ ]*\) (\([^,)]*\).*/gem install \1 -v "= \2" -y/'

That will turn this:

*** LOCAL GEMS ***

actionmailer (1.3.6)
actionpack (1.13.6)
actionwebservice (1.2.6)
activerecord (1.15.6)
activesupport (1.4.4)
...

Into this:

gem install actionmailer -v "= 1.3.6" -y
gem install actionpack -v "= 1.13.6" -y
gem install actionwebservice -v "= 1.2.6" -y
gem install activerecord -v "= 1.15.6" -y
gem install activesupport -v "= 1.4.4" -y
...

Copy. Paste. And you’re mirrored.