Make nokogiri >=1.6.0 use system libxml2

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

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.
Emanuel Over 8 years ago