Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

installing nokogiri ruby gem with a native extension

Tags:

ruby

gem

I want to install a ruby gem which tries to build a native extension. The gem in this case is nokogiri. If I do gem install nokogiri, the native extension dynamically links against libxml, libxslt libs. I want to statically link against those libs. How should I go about this?

like image 226
user287468 Avatar asked Dec 02 '22 05:12

user287468


1 Answers

Here are some pointers, but's it's not easy unless nokogiri contains build flags to support it:

  1. If nokogiri supports it, you can pass build arguments to install gem like this

    gem install nokogiri -- --with-static-libxml
    
  2. If there is no built in support you can try tweaking linkflags used to install the gem with:

    gem install nokogiri -- --with-ldflags='-static'
    

    It's likely that the build will fail, since --with-ldflags overrides all LDFLAGS, and also '-static' tries to link everything as static, so you need to examine mkmf.log, and treat it accordingly.

  3. If you want to do it manually, one way to do it is making the gem install fail by invoking with invalid option like:

    gem install nokogiri -- --with-ldflags
    

    This will cause the installation to fail with a message like this:

    Building native extensions.  This could take a while...
    ERROR:  Error installing nokogiri:
           ERROR: Failed to build gem native extension.
    
    ruby extconf.rb --with-ldflags
    

    So you should be able to build the gem yourself then after it's done finish the installation with (see gem help install):

      gem spec ../../cache/nokogiri-1.4.1.gem --ruby > \
             ../../specifications/nokogiri-1.4.1.gemspec
    
like image 182
mfazekas Avatar answered Dec 08 '22 00:12

mfazekas