Skip to content
Latchkey

Ruby Gem Build "RbConfig" CFLAGS/Toolchain Mismatch in CI

RbConfig records the compiler and flags Ruby itself was built with, and gem extensions reuse them. When the runner’s toolchain differs from what RbConfig expects - a missing cc, incompatible flags - the extension build fails in confusing ways.

What this error means

A native gem build fails with a compiler-not-found or an unrecognized-flag error that traces back to the values in RbConfig::CONFIG (CC, CFLAGS), rather than a missing library. It often appears after switching base images or Ruby builds.

build output
$ ruby -e 'puts RbConfig::CONFIG["CC"]'
x86_64-linux-gnu-gcc-12
make: x86_64-linux-gnu-gcc-12: No such file or directory   # that gcc isn't installed
make: *** [Makefile:245: ext.o] Error 127

Common causes

RbConfig points at an absent compiler

Ruby was built with a specific compiler (e.g. gcc-12) recorded in RbConfig. If the runner only has a different gcc, mkmf invokes the recorded one and make cannot find it.

Flag/toolchain skew between Ruby and the runner

CFLAGS or hardening flags baked into RbConfig are not understood by the runner’s compiler version, so the extension compile errors on flags rather than code.

How to fix it

Install the compiler RbConfig expects, or override it

Provide the recorded compiler, or point the build at the one you have.

Terminal
ruby -e 'puts RbConfig::CONFIG["CC"]'   # see what Ruby expects
apt-get install -y gcc-12   # install the expected compiler
# or override for the build
gem install <gem> -- --with-cc=gcc

Use a Ruby built for this image

  1. Prefer the official ruby image or a version-managed Ruby built on the same base as your runner.
  2. Avoid copying a Ruby built on one distro into an image with a different toolchain.
  3. Rebuild the Ruby (ruby-build) on the target image so RbConfig matches its compiler.

How to prevent it

  • Use a Ruby built on the same base image/toolchain as your CI runner.
  • Avoid mixing a Ruby and a toolchain from different distros.
  • Pin the base image so RbConfig and the installed compiler stay aligned.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →