06 July 2015

Create a RPM from a Ruby gem

I needed to get a few Ruby gems integrated into my team developer VM builds recently, so I thought I would see how difficult it was to turn a Gem into an RPM. Puppet is much happier dealing with RPMs than anything else. Plus, I have a rather annoying proxy in the way when attempting to install from rubygems.org, making a rpm on a locally hosted yum repo much easier.

After a bit of goggling, I came across a few gists and tools that claim to do the job, but it wasn't immediately clear how to use them.

Having previously build rpms for Ruby, Java, Maven and a few other bits and pieces, I know its a pretty simple process. Instead of using any special gem rpm tools, I decided to use rpm-build and a spec file just like I have in the past. Using this method, creating the RPM is simple.

Setup

First, make sure the rpm-build tool is installed:

$ yum install rpm-build

Then as root, in /root create the rpmbuild directory structure:

$ mkdir -p ~/rpmbuild/{BUILD,RPMS,SOURCES,SPECS,SRPMS}

Spec file

Taking the ruby-oci8 gem (Oracle drivers) as an example, I used the following spec file in rpmbuild/SPECS/ruby-oci8.spec:

Summary: ruby-oci8
Name: ruby-oci8
Version: 2.1.8
Release: 0
Group: Software Development
Distribution: ruby-oci8 for Ruby
Vendor: Redhat
Packager: Stephen ODOnnell
License: GPL
# Skip autogenerating RPM dependencies
AutoReqProv: no

%description
The Ruby gem ruby-oci8 packaged as a gem

%prep
rm -rf $RPM_BUILD_ROOT/*

%build

%install
mkdir -p $RPM_BUILD_ROOT/usr/lib/ruby/gems/1.8/gems
gem install -y -V --no-ri --no-rdoc --install-dir $RPM_BUILD_ROOT/usr/lib/ruby/gems/1.8 --local $RPM_SOURCE_DIR/%{name}-%{version}.gem

%files
/usr/lib/ruby/gems/1.8/gems/%{name}-%{version}
/usr/lib/ruby/gems/1.8/cache/%{name}-%{version}.gem
/usr/lib/ruby/gems/1.8/specifications/%{name}-%{version}.gemspec

%post

Download the ruby-oci8 gem and copy it into the rpmbuild/SOURCES directory and then build the RPM:

$ rpmbuild -ba SPECS/ruby-oci8.spec

If successful, the final RPM will be in rpmbuild/RPMS/x8664/ruby-oci8-2.1.8-0.x8664.rpm

Potential Issues

This RPM is coupled to a fairly specific Ruby install path, which is probably OK, but is something to be aware of.

Watch out for version changes on the source gem - you need to edit the Version section of the spec file each time the gem version changes.

blog comments powered by Disqus