Recently lot of people have been asking me why they are not able to install the pg (PostgreSQL) gem even after installing PostgreSQL server in their system?

Well the answer is simple, the pg gem, requires the PostgreSQL development libraries to build native extensions to communicate with the PostgreSQL server. Native extensions refer to building ruby extensions or wrappers for existing C or C++ library.

One can install the development libraries of PostgreSQL by installing the libpg-dev package.

The command below would install the last version of PostgreSQL available in the repository and its development libraries.

Ubuntu:

1
sudo apt-get install postgresql libpq-dev

CentOS / RedHat

yum install postgresql libpg-dev

1
yum install postgresql libpg-dev

After that trying gem install pg, should install everything smootly..

Happy Coding.. 🙂

UPDATE [2020-09-05]:

The developer libraries of postgresql are now packaged as postgresql-server-dev-11, 11 referring to the version.

Ubuntu:

1
sudo apt-get install postgresql-server-dev-11