# Ruby: ERROR: Failed to build gem native extension. current directory: /gems/pg-1.5.4/ext/pg — An error occurred while installing pg

**Code:** `Failed to build gem native extension` · **Category:** Ruby / Rails · **Seen on:** Ruby, Linux

A gem with C code couldn't compile, usually because a system library or header is missing.

## Meaning

Read `mkmf.log`/the lines before the error: missing `libpq-dev` (pg), `libmysqlclient-dev` (mysql2), `libxml2/libxslt` (nokogiri source build), `libsqlite3-dev`, `build-essential`/Xcode CLT, `ruby-dev` headers. Install the dev package and re-run `bundle install`.

## Common causes

- Missing dev headers (libpq-dev, libmysqlclient-dev, ruby-dev)
- No compiler/build tools
- Apple Silicon/Homebrew library paths
- Wrong Ruby version for the gem
- Outdated gem version incompatible with new Ruby

## Quick fix

1. Debian: `sudo apt install build-essential ruby-dev libpq-dev` (as needed)
2. macOS: `xcode-select --install; brew install libpq` and `bundle config build.pg --with-pg-config=$(brew --prefix libpq)/bin/pg_config`
3. Upgrade the gem

## Detailed fix by platform

### Linux

1. `less $(gem env gemdir)/extensions/*/*/pg-*/mkmf.log`

## How to diagnose

1. **Gem** — Which?
2. **Header** — Missing lib?
3. **Tools** — Compiler installed?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/ruby/gem-ext-build-error-native-extension)
