Skip to content
Latchkey

Ruby pg gem "Can't find the PostgreSQL client library (libpq)"

The pg gem compiles a native extension that links against the PostgreSQL client library, libpq. The build fails because libpq (and its headers) or pg_config is not installed on the runner.

What this error means

Installing the pg gem fails during the native build with "Can't find the PostgreSQL client library (libpq)" or a missing pg_config. The gem cannot link without the Postgres client development files.

mkmf output
checking for pg_config... no
checking for libpq-fe.h... no
Can't find the PostgreSQL client library (libpq)
*** extconf.rb failed ***

Common causes

libpq development files not installed

The pg gem needs libpq-fe.h and the libpq library to compile. The runtime Postgres package alone does not include the -dev headers.

pg_config not on PATH

When pg_config is missing or not on PATH, extconf cannot locate the Postgres installation to link against.

How to fix it

Install the libpq development package

Terminal
# Debian/Ubuntu
apt-get update && apt-get install -y libpq-dev
# Alpine
apk add --no-cache postgresql-dev
# RHEL/Fedora
dnf install -y postgresql-devel

Point the gem at pg_config explicitly

If Postgres is installed in a non-standard prefix, tell the gem where pg_config lives.

Terminal
gem install pg -- --with-pg-config=/usr/pgsql-16/bin/pg_config

How to prevent it

  • Bake libpq-dev into runner images that build the pg gem.
  • Use a precompiled pg platform gem where one is published.
  • Keep the Postgres client dev package version aligned across CI and prod.

Related guides

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