RubyGems "Failed to build gem native extension" (pg / libpq) in CI
The pg gem compiles a C extension against libpq, and the build failed because the PostgreSQL client library or pg_config is missing on the runner. The mkmf check for libpq fails before make even runs.
What this error means
bundle install fails building pg with "Can't find the PostgreSQL client library (libpq)" or "checking for pg_config... no", ending in a Gem::Ext::BuildError.
bundler
checking for pg_config... no
checking for libpq-fe.h... no
Can't find the PostgreSQL client library (libpq)
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.Common causes
libpq development headers are not installed
The runner lacks libpq-dev (or postgresql client dev), so the pg extension cannot find libpq-fe.h.
pg_config is not on PATH
Without pg_config the build cannot locate the PostgreSQL client library to link against.
How to fix it
Install the PostgreSQL client dev package
- Install libpq-dev so headers and pg_config are present.
- Re-run bundle install to compile pg.
- Confirm the database client is on PATH for runtime too.
Terminal
sudo apt-get update
sudo apt-get install -y libpq-devPoint the build at pg_config explicitly
If PostgreSQL is installed in a nonstandard prefix, tell the gem where pg_config lives.
Terminal
bundle config set --local build.pg --with-pg-config=/usr/pgsql-16/bin/pg_config
bundle installHow to prevent it
- Install libpq-dev before bundle install when pg builds from source.
- Ensure pg_config is on PATH in the build step.
- Bake the PostgreSQL client into a custom runner image.
Related guides
RubyGems "Failed to build gem native extension" (nokogiri) in CIFix "Gem::Ext::BuildError: ERROR: Failed to build gem native extension" for nokogiri in CI - the source build…
RubyGems "Failed to build gem native extension" (mysql2) in CIFix the mysql2 gem "Failed to build gem native extension" / "cannot find -lmysqlclient" in CI - the build nee…
ActiveRecord::NoDatabaseError / PG::ConnectionBad in CIFix "ActiveRecord::NoDatabaseError" and "PG::ConnectionBad: could not connect to server" in CI - Rails could…