Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 667
Jacob Avatar asked Sep 08 '10 12:09

Jacob


People also ask

What are native extensions Ruby?

Ruby native extensions are libraries written in C using the built-in functions of the RubyVM. Basically, it's C programming with a ton of functions and macros to interact with the virtual machine. Anything that can be achieved by using pure Ruby can also be implemented using the built-in instructions.

Where are RubyGems installed Linux?

When you use the --user-install option, RubyGems will install the gems to a directory inside your home directory, something like ~/. gem/ruby/1.9. 1 . The commands provided by the gems you installed will end up in ~/.

How do I add gems to Ruby?

With the --local ( -l ) option, you would perform a local search through your installed gems. To install a gem, use gem install [gem] . Browsing installed gems is done with gem list . For more information about the gem command, see below or head to RubyGems' docs.

Are RubyGems compiled?

Ruby gems can have native extensions, which require compilation. When installing via Bundler, it's very common to install with the --jobs option, which launches each gem installation in a separate job (with the disclaimer that Ruby concurrency is limited).


1 Answers

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
like image 108
Jacob Avatar answered Sep 18 '22 20:09

Jacob