2

I've reviewed all the posts on SO with similar errors installing the Postgres pg gem.

My issue is unique: I can successfully add Postgres to my Sinatra Ruby project via the Gemfile and bundle install -- however when building the Docker image it fails installing pg with the following error:

ERROR:  Error installing pg:
ERROR: Failed to build gem native extension.

current directory: /usr/local/bundle/gems/pg-0.19.0/ext
/usr/local/bin/ruby -r ./siteconf20170113-7-ottv1k.rb extconf.rb
checking for pg_config... no
No pg_config... trying anyway. If building fails, please try again with
  --with-pg-config=/path/to/pg_config
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*** extconf.rb failed ***

I've tried everything suggested in similar answers:

  • Brew install / uninstall,
  • gem install / uninstall specifying path to both files: pg_config and libpq-fe.h
  • etc.

As I'm not too familiar with Docker, is there something I'm missing about installing a db with Docker?

Any help much much appreciated in advance. Thank you.

chhhris
  • 355
  • 1
  • 4
  • 16

3 Answers3

5

You got this error because of missing libpq-dev package.

Here is the minimal ubuntu-based Dockerfile for installing pg gem:

FROM ruby:2.6.6-slim-buster

RUN apt update && apt install -y \
        build-essential libpq-dev  \
     && rm -rf /var/lib/apt/lists/*

RUN gem install pg
Kirill
  • 6,762
  • 4
  • 51
  • 81
4

If this is still an issue, make sure your docker image has the postgresql-client, postgresql-dev and build-essentials installed. Include something like

...
RUN apk update && apk upgrade
RUN apk add postgresql-client build-dependencies postgresql-dev
...

in your Dockerfile before running bundle install. The postgresql-dev package contains the headers and other dev-related files.

  • 1
    To expand on this a bit more, `brew` is installing on your local machine, but Docker doesn't see these changes. It acts like a virtual machine. If the client OS doesn't have postgresql, `pg` gem will fail to compile. Glen's response gets those installed in the client. – Jack M. Nov 09 '17 at 15:26
0

I used dockerfile-rails to generate my dockerfile in Rails 7.0.4 and I was having the same issue installing the pg gem because of the missing libpq-dev package.

Just add the following command to your Dockerfile before your command to bundle install all application gems:

# Install packages needed to build gems and node modules
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential curl libpq-dev