4

I am trying to deploy Rails to Elastic Beanstalk using a simple 3-step CodePipeline process. Step 1 takes the source from GitHub, gives it to CodeBuild, which then should create the build and pass it to EB.

For some reason, when the CodeBuild process reaches the pg gem, it fails with the following error:

Fetching pg 1.3.5
Installing pg 1.3.5 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5/ext
/root/.rbenv/versions/2.7.2/bin/ruby -I
/root/.rbenv/versions/2.7.2/lib/ruby/2.7.0 -r ./siteconf20220419-56-10cqcfm.rb
extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... yes
Using config values from /usr/bin/pg_config
Using libpq from /usr/lib64
checking for libpq-fe.h... yes
checking for libpq/libpq-fs.h... yes
checking for pg_config_manual.h... yes
checking for PQconnectdb() in -lpq... yes
checking for PQconninfo() in libpq-fe.h... no
Your PostgreSQL is too old. Either install an older version of this gem or
upgrade your database to at least PostgreSQL-9.3.
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/root/.rbenv/versions/2.7.2/bin/$(RUBY_BASE_NAME)
    --with-pg
    --without-pg
    --enable-gvl-unlock
    --disable-gvl-unlock
    --enable-windows-cross
    --disable-windows-cross
    --with-pg-config
    --without-pg-config
    --with-pg_config
    --without-pg_config
    --with-pg-dir
    --without-pg-dir
    --with-pg-include
    --without-pg-include=${pg-dir}/include
    --with-pg-lib
    --without-pg-lib=${pg-dir}/lib
    --with-pq-dir
    --without-pq-dir
    --with-pq-include
    --without-pq-include=${pq-dir}/include
    --with-pq-lib
    --without-pq-lib=${pq-dir}/lib
    --with-pqlib
    --without-pqlib

To see why this extension failed to compile, please check the mkmf.log which can
be found here:

/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/gems/pg-1.3.5 for inspection.
Results logged to
/root/.rbenv/versions/2.7.2/lib/ruby/gems/2.7.0/extensions/x86_64-linux/2.7.0/pg-1.3.5/gem_make.out

An error occurred while installing pg (1.3.5), and Bundler cannot continue.
Make sure that `gem install pg -v '1.3.5' --source 'https://rubygems.org/'`
succeeds before bundling.

In Gemfile:
  pg

[Container] 2022/04/19 14:56:37 Command did not exit successfully bundle install exit status 5
[Container] 2022/04/19 14:56:37 Phase complete: INSTALL State: FAILED
[Container] 2022/04/19 14:56:37 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: bundle install. Reason: exit status 5

I have the following barebones buildspec.yml:

version: 0.2

phases:
  install:
    commands: 
      - bundle install
artifacts:
  files:
    - '**/*'

The bundle process runs without error on my local machine.

zcserei
  • 577
  • 7
  • 30
  • The error "`Your PostgreSQL is too old. Either install an older version of this gem or upgrade your database to at least PostgreSQL-9.3`" explains you problem doesn't it? In your local machine what version of `PostgreSQL` do you have? – Riz Apr 19 '22 at 15:08
  • @Riz on my local machine I have 14.1, and RDS is also newer than 9.3 -- it seems like the virtual machine used by CodeBuild is acting up, but I have previously deployed Rails + pg setups this way and never had to manually install a newer version of `pg` during the build process, seems like quite the waste of time / resources. – zcserei Apr 19 '22 at 15:10
  • maybe the other times you had the update image of the os? For this you can check postgresql version in your ec2 and if it's 9.3+ then you need to check you path or give the whole path in the build process instead of just `pg` – Riz Apr 19 '22 at 15:23
  • @Riz this is not the final EC2 instance where the app is going to get deployed, this is just inside CodeBuild. There should be no need for postgres to be installed for Bundler to be able to install the `pg` gem and then pass the whole bundle to EB. – zcserei Apr 19 '22 at 15:27
  • maybe you need to provide some more details about your codebuild env. What os and runtime and also the source to see the requirements etc – Riz Apr 19 '22 at 16:14

1 Answers1

7

I had the same problem you had above too (except without using CodeBuild). The issue is that you're trying to install the pg gem at version 1.3.5. At the time of writing, Elastic Beanstalk on Amazon Linux 2 (AL2) ships with Postgres version 9.2 by default (incredibly old ). Whereas the pg gem dropped support for 9.2 after version 1.2.3. See here.

So your 2 options are either:

  1. Enhance EB to install a more recent version of Postgres during the build & deploy process, for example as described here: https://stackoverflow.com/a/63204453/1852005.
  2. Or you could just use version 1.2.3 of the pg gem by configuring that version in your Gemfile. That version shipped in 2020-03-18.

Either of those options should get you past that error.

Note that your application may also connect to Amazon's RDS which has a more recent Postgres version. However that RDS lives on another server, so don't get the two confused! You can see the difference if you eb ssh to the server and run e.g: this on the shell directly:

[root@ip-XXX-XX-XX-XXX current]# psql --version
psql (PostgreSQL) 9.2.24

compared with running this on a Rails console which shows the RDS version:

irb(main):001:0> ActiveRecord::Base.connection.select_value('SELECT version()')
=> "PostgreSQL 11.13 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 7.3.1 20180712 (Red Hat 7.3.1-12), 64-bit"
stwr667
  • 1,566
  • 1
  • 16
  • 31
  • 3
    tried second options by using `gem "pg", "= 1.2.3"` works. thanks for the thorough explanation! – rick Jun 11 '22 at 12:35
  • Documentation for AWS [install a software package from the Extras Library](https://aws.amazon.com/premiumsupport/knowledge-center/ec2-install-extras-library-software/) – Miguel Hargreaves Pimenta Aug 07 '22 at 22:06