How do you precompile the native extensions for a ruby gem for linux?
Asked Answered
P

2

13

We have a ruby application that depends on a gem with native extensions (in this specific case Nokogiri). However, for various reasons we cannot install the build prerequisites (such as build-essential, libxslt-dev, ruby-dev, etc) for that gem onto our production host.

Is there a (standard?) way to repackage the gem with the native extensions pre-built?

It should be possible (it seems to be fairly standard to do this for Windows), but I can't find any documentation on the subject.

Note that we only need to support a single platform, with known versions of all system libraries (Ubuntu 9.04 Server 64 bit, Ruby 1.8.7).

UPDATE: We're using Bundler, so we want to still have a gem to install at the end of the day, not a debian package.

Planimeter answered 8/9, 2010 at 12:54 Comment(0)
P
21

Finally found a way to do this for gems that use rake-compiler for building their C extensions (which is most of them).

You need to do the following on a machine that is identical to the one you want to deploy to, or it simply won't work:

Install the build prerequisites for building C extensions:

# apt-get install build-essentials ruby-dev # ... etc
# gem install rake-compiler

Unpack the gem you want to rebuild:

$ gem unpack nokogiri

Build your shiny new precompiled gem:

$ rake native gem

You can now install the native gem on a machine without any build tools installed:

$ gem install pkg/nokogiri-1.4.3.1-x86-linux.gem 
Successfully installed nokogiri-1.4.3.1-x86-linux
1 gem installed
Planimeter answered 10/10, 2010 at 20:37 Comment(2)
I'll just share that I had more success checking out the sparklemotion/nokogiri repository from github, changing to the versioned git tag for the version I needed, and ran rake native gem there. Putting gems packaged this way in your vendor/cache folder with Bundler works a treat!Hyalite
@Planimeter can you please elaborate on this ? "changing to the versioned git tag for the version I needed"Priorate
J
-1

Build machine

To have the correctly platformed version for all of your gems already cached, package all gems in vendor/cache on an identical machine:

$ bundle package --all

Prduction machine

Install all gem dependecies already cached that requires to have the correctly platformed version using the gem cache:

$ bundle install --local --deployment
Jacobina answered 17/2, 2014 at 16:38 Comment(1)
This does not pre-compile native gems.Formica

© 2022 - 2024 — McMap. All rights reserved.