Ruby Nokogiri Falls Back to Source Build on a Locked Platform in CI
Nokogiri publishes precompiled platform gems that install with no compiling, but your CI is building it from source anyway - usually because force_ruby_platform is set, the runner is musl/Alpine, or the Linux platform is not locked.
What this error means
Nokogiri install logs show "Building native extensions" and take minutes (or fail) when the precompiled gem should have installed instantly. The slow source build is the symptom; the precompiled gem is being skipped.
Fetching nokogiri 1.16.0
Installing nokogiri 1.16.0 with native extensions # should be (x86_64-linux)
Building native extensions. This could take a while...Common causes
force_ruby_platform forces the generic gem
bundle config force_ruby_platform true (or BUNDLE_FORCE_RUBY_PLATFORM) tells Bundler to use the pure-Ruby gem, which compiles from source instead of taking the precompiled platform build.
musl runner with no musl precompiled gem locked
On Alpine/musl the x86_64-linux gem does not match; without x86_64-linux-musl in the lockfile Nokogiri falls back to a source compile.
How to fix it
Turn off force_ruby_platform and lock the platform
bundle config set --local force_ruby_platform false
bundle lock --add-platform x86_64-linux
bundle installAdd the musl platform on Alpine
For musl-based images, lock the musl platform so the matching precompiled gem is selected.
bundle lock --add-platform x86_64-linux-musl
bundle installHow to prevent it
- Keep force_ruby_platform off unless you specifically need the pure-Ruby gem.
- Lock every platform your CI uses, including x86_64-linux-musl on Alpine.
- Prefer glibc base images where precompiled gems are most broadly available.