Cannot Deploy Rails with PostgreSQL

Hello! Unfortunately, I’m having trouble deploying a Ruby on Rails application with a PostgreSQL database. I’m using Ruby 3.0.0 and only the automatically generated Dockerfile. Here’s the error I’m getting in the monitor -

Gem::LoadError: pg is not part of the bundle. Add it to your Gemfile.

Any idea why this is happening? Here’s the automatically generated Dockerfile I’m using -

# syntax = docker/dockerfile:1

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version and Gemfile
ARG RUBY_VERSION=3.0.0
FROM ruby:$RUBY_VERSION-slim as base

LABEL fly_launch_runtime="rails"

# Rails app lives here
WORKDIR /rails

# Set production environment
ENV RAILS_ENV="production" \
    BUNDLE_WITHOUT="development:test" \
    BUNDLE_DEPLOYMENT="1"

# Update gems and bundler
RUN gem update --system --no-document && \
    gem install -N bundler


# Throw-away build stage to reduce size of final image
FROM base as build

# Install packages needed to build gems
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y build-essential pkg-config

# Install application gems
COPY --link Gemfile Gemfile.lock ./
RUN bundle install && \
    bundle exec bootsnap precompile --gemfile && \
    rm -rf ~/.bundle/ $BUNDLE_PATH/ruby/*/cache $BUNDLE_PATH/ruby/*/bundler/gems/*/.git

# Copy application code
COPY --link . .

# Precompile bootsnap code for faster boot times
RUN bundle exec bootsnap precompile app/ lib/

# Precompiling assets for production without requiring secret RAILS_MASTER_KEY
RUN SECRET_KEY_BASE=DUMMY ./bin/rails assets:precompile


# Final stage for app image
FROM base

# Install packages needed for deployment
RUN apt-get update -qq && \
    apt-get install --no-install-recommends -y curl libsqlite3-0 && \
    rm -rf /var/lib/apt/lists /var/cache/apt/archives

# Copy built artifacts: gems, application
COPY --from=build /usr/local/bundle /usr/local/bundle
COPY --from=build /rails /rails

# Run and own only the runtime files as a non-root user for security
RUN useradd rails --create-home --shell /bin/bash && \
    mkdir /data && \
    chown -R rails:rails db log storage tmp /data
USER rails:rails

# Deployment options
ENV DATABASE_URL="sqlite3:///data/production.sqlite3" \
    RAILS_LOG_TO_STDOUT="1" \
    RAILS_SERVE_STATIC_FILES="true"

# Entrypoint prepares the database.
ENTRYPOINT ["/rails/bin/docker-entrypoint"]

# Start the server by default, this can be overwritten at runtime
EXPOSE 3000
VOLUME /data
CMD ["./bin/rails", "server"]

When I add gem 'pg' to my Gemfile, run bundle install (on my computer) again, and then run fly deploy, I get this error instead -

#0 10.94 Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
#0 10.94 
#0 10.94     current directory: /rails/vendor/bundle/ruby/3.0.0/gems/pg-1.5.4/ext
#0 10.94 /usr/local/bin/ruby extconf.rb
#0 10.94 Calling libpq with GVL unlocked
#0 10.94 checking for pg_config... no
#0 10.94 checking for libpq per pkg-config... no
#0 10.94 Using libpq from 
#0 10.94 checking for libpq-fe.h... no
#0 10.94 Can't find the 'libpq-fe.h header
#0 10.94 *****************************************************************************
#0 10.94 
#0 10.94 Unable to find PostgreSQL client library.
#0 10.94 
#0 10.94 Please install libpq or postgresql client package like so:
#0 10.94   sudo apt install libpq-dev
#0 10.94   sudo yum install postgresql-devel
#0 10.94   sudo zypper in postgresql-devel
#0 10.94   sudo pacman -S postgresql-libs
#0 10.94 
#0 10.94 or try again with:
#0 10.94   gem install pg -- --with-pg-config=/path/to/pg_config
#0 10.94 
#0 10.94 or set library paths manually with:
#0 10.94 gem install pg -- --with-pg-include=/path/to/libpq-fe.h/
#0 10.94 --with-pg-lib=/path/to/libpq.so/
#0 10.94 
#0 10.94 *** extconf.rb failed ***
#0 10.94 Could not create Makefile due to some reason, probably lack of necessary
#0 10.94 libraries and/or headers.  Check the mkmf.log file for more details.  You may
#0 10.94 need configuration options.
#0 10.94 
#0 10.94 Provided configuration options:
#0 10.94        --with-opt-dir
#0 10.94        --without-opt-dir
#0 10.94        --with-opt-include
#0 10.94        --without-opt-include=${opt-dir}/include
#0 10.94        --with-opt-lib
#0 10.94        --without-opt-lib=${opt-dir}/lib
#0 10.94        --with-make-prog
#0 10.94        --without-make-prog
#0 10.94        --srcdir=.
#0 10.94        --curdir
#0 10.94        --ruby=/usr/local/bin/$(RUBY_BASE_NAME)
#0 10.94        --with-pg
#0 10.94        --without-pg
#0 10.94        --enable-gvl-unlock
#0 10.94        --disable-gvl-unlock
#0 10.94        --enable-windows-cross
#0 10.94        --disable-windows-cross
#0 10.94        --with-pg-config
#0 10.94        --without-pg-config
#0 10.94        --with-pg_config
#0 10.94        --without-pg_config
#0 10.94        --with-libpq-config
#0 10.94        --without-libpq-config
#0 10.94        --with-pkg-config
#0 10.94        --without-pkg-config
#0 10.94        --with-pg-dir
#0 10.94        --without-pg-dir
#0 10.94        --with-pg-include
#0 10.94        --without-pg-include=${pg-dir}/include
#0 10.94        --with-pg-lib
#0 10.94        --without-pg-lib=${pg-dir}/lib
#0 10.94 
#0 10.94 To see why this extension failed to compile, please check the mkmf.log which can
#0 10.94 be found here:
#0 10.94 
#0 10.94 /rails/vendor/bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.5.4/mkmf.log
#0 10.94 
#0 10.94 extconf failed, exit code 1
#0 10.94 
#0 10.94 Gem files will remain installed in /rails/vendor/bundle/ruby/3.0.0/gems/pg-1.5.4
#0 10.94 for inspection.
#0 10.94 Results logged to
#0 10.94 /rails/vendor/bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.5.4/gem_make.out
#0 10.94 
#0 10.94 An error occurred while installing pg (1.5.4), and Bundler cannot continue.
#0 10.94 Make sure that `gem install pg -v '1.5.4' --source 'https://rubygems.org/'`
#0 10.94 succeeds before bundling.
#0 10.94 
#0 10.94 In Gemfile:
#0 10.94   pg

Can you try:

bin/rails generate dockerfile

This should suggest changes that will resolve this problem:

1 Like

That does the trick. Thank you for your help!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.