Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker build unable to install pg gem in Sinatra project

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.

like image 323
chhhris Avatar asked Nov 26 '25 14:11

chhhris


1 Answers

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
like image 71
Kirill Avatar answered Nov 28 '25 17:11

Kirill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!