Simply make rpms from ruby gems
by Ingvar Hagelund
I tend to dislike scattering my filesystems with non-packaged script modules, be it python, perl, or ruby. So I wrap rpm packages for my systems. But packing is such a bother, you might say. That might be true, but luckily, we have good tools to help us.
gem2rpm, semi-automatic specfile generator
To build this website, we used jekyll, an excellent small ruby http server for static content. Jekyll is available as a standard ruby gem, but not as rpms for Fedora. In the following, I show how I built and installed jekyll via rpms only, on my Fedora system, using gem2rpm - a semi-automatic rpm specfile generator for ruby gems.
To build rpms from ruby gems, we need the rubygems and rubygem-gem2rpm packages. For RHEL and clones, we may find gem2rpm in the EPEL repos.
$ sudo yum install rubygems rubygems-devel rubygem-gem2rpm
Set up the rpmbuild tree
If we do not have it already, we install the rpmdevtools package collection, and set up the rpmbuild tree
$ sudo yum install rpmdevtools
$ rpmdev-setuptree
Fetch, generate, build
Note that in the following, we assume that there are no matching gems in ~/rpmbuild/SOURCES , so we can use simple name*.gem wildcards.
First fetch the gem source for jekyll.
$ cd ~/rpmbuild/SOURCES
$ gem fetch jekyll
Generate the rpm specfile. If we want, we might inspect the file before continuing. For more advanced usage, we could use ruby templates to control the result more. But for now, the defaults are good enough.
$ gem2rpm jekyll*.gem > ~/rpmbuild/SPECS/jekyll.spec
Build the rpm package
$ rpmbuild -bb ~/rpmbuild/SPECS/jekyll.spec
Dependencies
Script apps tend to require a lot of libraries or modules, so we need to check for missing dependencies. The following small script queries the packages just built for their rubygem requirements, then asks if they exist in the repos, and shows those that are missing.
$ rpm -q --requires -p ~/rpmbuild/RPMS/noarch/rubygem-jekyll*.noarch.rpm |\
awk '/^rubygem\(/ {print $1}' | sort | uniq | while read p; do \
yum whatprovides "$p" > /dev/null 2>&1 || echo $p;
done
rubygem(colorator)
rubygem(jekyll-sass-converter)
rubygem(jekyll-watch)
rubygem(mercenary)
rubygem(rouge)
So we need to build these packages as well. I incidentally know that I also need jekyll-paginate for this project, so we add that to the list.
Rinse and repeat
We build these the packages the same way. First download the gem with gem fetch. Then generate a specfile with gem2rpm, before finally building the package with rpmbuild. So let us build a loop that do them all in one go:
$ for g in \
colorator \
mercenary rouge \
jekyll-sass-converter \
jekyll-watch \
jekyll-paginate;
do gem fetch "$g"
gem2rpm "$g"*.gem > ~/rpmbuild/SPECS/rubygem-$g.spec;
rpmbuild -bb ~/rpmbuild/SPECS/rubygem-$g.spec
done
This might take a few moments, so we drink a cup of our favourite advent hot brew while waiting. If all went well, we might finally install the packages. In this case, they are all architechture independent, so they are all found in ~/rpmbuild/RPMS/noarch
$ cd ~/rpmbuild/RPMS/noarch
$ sudo yum install \
rubygem-mercenary-*.rpm \
rubygem-colorator-*.rpm \
rubygem-rouge*.rpm \
rubygem-jekyll-*.rpm
Using this, I can test this sysadvent blog locally.
More than ruby gems
So far, so good with ruby gems. What about other scripting languages? Are there similar tools? Can we de similar tricks with them? Of course we can. For Perl CPAN modules, have a look at cpanspec. PHP pear has even built-in support for generating rpms from pear modules. Check out pear make-rpm-spec. Python packages can be wrapped as rpms using pyp2rpm. If everything else fails, we might go dirty, and use fpm. Or we might even build an rpm package from scratch, it is not that hard.
Easter egg in an advent blog
That is all. There are bonus point to anyone who found the LoTR reference.
Do you want to work with Ingvar Hagelund? We're always on the lookout for people with experience in Linux and open source software. Let us know if you are interested in joining our team!
Looking for more content? Check out our techblog or read our whitepapers and how-to guides.
Subscribe via RSS