Ruby Nokogiri Build Fails - Missing libxml2 / libxslt in CI
Nokogiri ships a precompiled gem for common platforms, but when forced to build from source it needs libxml2 and libxslt plus their headers. Missing those, or having the wrong platform locked, fails the build.
What this error means
Installing Nokogiri fails during the native build referencing libxml2 or libxslt, or the build takes a very long time and then errors. On a correctly locked platform the precompiled gem should install with no compiling at all.
ERROR: Failed to build gem native extension.
...
extconf.rb: libxml2 is missing. Please locate mkmf.log ...
checking for libxml/parser.h... noCommon causes
Forced source build without libxml2/libxslt -dev
When Nokogiri compiles from source (no platform gem locked, or --use-system-libraries set), it needs libxml2-dev and libxslt1-dev. Without them, the build fails.
Linux platform not in the lockfile
If x86_64-linux is missing from PLATFORMS, Bundler will not select the precompiled Linux gem and falls back to a slow, fragile source compile.
How to fix it
Use the precompiled platform gem
Lock the Linux platform so Bundler picks Nokogiri’s prebuilt gem and skips compiling.
bundle lock --add-platform x86_64-linux
bundle config set force_ruby_platform false
bundle installInstall the libraries for a source build
If you must compile (system-libraries mode), install the headers first.
# Debian/Ubuntu
apt-get update && apt-get install -y libxml2-dev libxslt1-devHow to prevent it
- Keep x86_64-linux in Gemfile.lock so the precompiled Nokogiri is used.
- Avoid --use-system-libraries in CI unless you have a specific reason.
- Bake libxml2-dev/libxslt1-dev into images that build from source.