Read more

Make nokogiri >=1.6.0 use system libxml2

Emanuel
December 17, 2015Software engineer at makandra GmbH

This has become part of our server setup and does not have to be fixed per application.

Quick check: bin/rails runner 'puts Nokogiri::VersionInfo.new.libxml2_using_system?'

Step by step instruction

Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Nokogiri uses vendored libxml2 since version 1.6.0, which means that each time a new issue with libxml2 occurs, you have to update nokogiri itself. Another approach is to use the system lib again, as it patches the libxml2 library with periodically system updates.

  1. bundle config --local build.nokogiri --use-system-libraries
  2. Ensure that .bundle is not gitignored and .bundle/config contains these lines:
---
BUNDLE_BUILD__NOKOGIRI: "--use-system-libraries"

3. Either you remove the gem on the targets manually and reinstall it or you make a minor version upgrade of nokogiri to force the update
4. After all you can verfiy several things:

  • nokogiri using system libxml2 runnning Nokogiri::VersionInfo.new.libxml2_using_system?
  • important warnings running Nokogiri::VersionInfo.new.warnings
  • all infos with Nokogiri::VersionInfo.new.to_hash

Notes

  • If you leave the local option, bundler set this config globally. List configs of bundler with bundle config.
  • If libxml2_using_system? doesn't change, you might need to stop spring wiht spring stop
  • If Nokogiri is already installed, it won't be installed again. In bundler 1.10 you can use bundle install --force to reinstall all your gems. It might take longer, but it is easier to automate as you don't have to pick out the right version and uninstall it with gem uninstall nokogiri.
  • If you deploy with capistrano, bundler ignores development and test group, which means nokogiri is not loaded. Step 4 will not work in this case.
Posted by Emanuel to makandra dev (2015-12-17 13:53)