Getting Postgres working in a ruby application

Added libpqxx to my replit.nix file:

{ pkgs }: {
	deps = [
        pkgs.nano
        pkgs.ruby_3_0
        pkgs.rubyPackages_3_0.solargraph
        pkgs.rufo
        pkgs.libpqxx
	];
}

Added the pg gem to my Gemfile:

gem 'pg'

Bundler cannot build pg w/ a rather common linux error (it can’t find the libpq library). How do other replit users do this?

 bundle install
system temporary path is world-writable: /tmp
/tmp is world-writable: /tmp
Fetching gem metadata from https://rubygems.org/..........
Resolving dependencies...
Using rake 13.0.6
Using concurrent-ruby 1.1.9
Using minitest 5.15.0
Using builder 3.2.4
Using strscan 3.0.1
Using erubi 1.10.0
Using nio4r 2.5.8
Using marcel 1.0.2
Using mini_mime 1.1.2
Using digest 3.1.0
Using bundler 2.2.32
Using rack 2.2.3
Using websocket-extensions 0.1.5
Using io-wait 0.2.1
Using racc 1.6.0
Using crass 1.0.6
Using public_suffix 4.0.6
Using awesome_print 1.9.2
Using rouge 4.1.1
Using bindex 0.8.1
Using debug_inspector 1.1.0
Using timeout 0.2.0
Using matrix 0.4.2
Using regexp_parser 2.2.0
Using childprocess 4.1.0
Using io-console 0.5.11
Using faraday-net_http 3.0.2
Fetching pg 1.5.3
Using ruby2_keywords 0.0.5
Using rexml 3.2.5
Using multi_xml 0.6.0
Using mini_magick 4.12.0
Using tilt 2.1.0
Using method_source 1.0.0
Using i18n 1.8.11
Using thor 1.2.1
Using mail 2.7.1
Using rack-mini-profiler 3.1.0
Using multipart-post 2.3.0
Using ffi 1.15.5
Using sprockets 4.0.2
Using rubyzip 2.3.2
Using better_errors 2.10.0
Using binding_of_caller 1.0.0
Using addressable 2.8.0
Using reline 0.3.1
Using faraday 2.7.5
Using redis 4.8.1
Using nokogiri 1.13.1 (x86_64-linux)
Using websocket-driver 0.7.5
Using puma 5.6.0
Using tzinfo 2.0.4
Using zeitwerk 2.5.3
Using plissken 2.0.1
Using loofah 2.13.0
Using activesupport 7.0.1
Using net-protocol 0.1.2
Using ordinare 0.4.0
Using xpath 3.2.0
Using activemodel 7.0.1
Using net-imap 0.2.3
Using net-pop 0.1.1
Using net-smtp 0.3.1
Using rails-html-sanitizer 1.4.2
Using globalid 1.0.0
Using sassc 2.4.0
Using faraday-multipart 1.0.4
Using irb 1.4.1
Using activerecord 7.0.1
Using debug 1.4.0
Using ruby-openai 4.1.0
Using selenium-webdriver 4.1.0
Using ruby-vips 2.1.4
Using annotate 3.2.0
Using image_processing 1.12.2
Using webdrivers 5.0.0
Using httparty 0.21.0
Using activejob 7.0.1
Using rack-test 1.1.0
Using isbndb-ruby 0.1.3
Using rails-dom-testing 2.0.3
Using capybara 3.36.0
Using actionview 7.0.1
Using jbuilder 2.11.5
Using actionpack 7.0.1
Using actionmailer 7.0.1
Using railties 7.0.1
Using sprockets-rails 3.4.2
Using importmap-rails 1.0.2
Using stimulus-rails 1.0.2
Using web-console 4.2.0
Using actioncable 7.0.1
Using sassc-rails 2.1.2
Using turbo-rails 1.0.1
Using activestorage 7.0.1
Using actionmailbox 7.0.1
Using actiontext 7.0.1
Using rails 7.0.1
Installing pg 1.5.3 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

current directory:
/home/runner/BookTracker/.bundle/ruby/3.0.0/gems/pg-1.5.3/ext
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/bin/ruby -I
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0 -r
./siteconf20230526-13437-hs67dh.rb extconf.rb
Calling libpq with GVL unlocked
checking for pg_config... no
checking for libpq per pkg-config... no
Using libpq from 
checking for libpq-fe.h... no
Can't find the 'libpq-fe.h header
*****************************************************************************

Unable to find PostgreSQL client library.

Please install libpq or postgresql client package like so:
  sudo apt install libpq-dev
  sudo yum install postgresql-devel
  sudo zypper in postgresql-devel
  sudo pacman -S postgresql-libs

or try again with:
  gem install pg -- --with-pg-config=/path/to/pg_config

or set library paths manually with:
gem install pg -- --with-pg-include=/path/to/libpq-fe.h/
--with-pg-lib=/path/to/libpq.so/

*** 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=/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/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-libpq-config
    --without-libpq-config
    --with-pkg-config
    --without-pkg-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

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

/home/runner/BookTracker/.bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.5.3/mkmf.log

extconf failed, exit code 1

Gem files will remain installed in
/home/runner/BookTracker/.bundle/ruby/3.0.0/gems/pg-1.5.3 for inspection.
Results logged to
/home/runner/BookTracker/.bundle/ruby/3.0.0/extensions/x86_64-linux/3.0.0/pg-1.5.3/gem_make.out

/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/builder.rb:92:in
`run'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/ext_conf_builder.rb:47:in
`block in build'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/tempfile.rb:317:in
`open'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/ext_conf_builder.rb:26:in
`build'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/builder.rb:158:in
`build_extension'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/builder.rb:192:in
`block in build_extensions'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/builder.rb:189:in
`each'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/ext/builder.rb:189:in
`build_extensions'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/rubygems/installer.rb:837:in
`build_extensions'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/rubygems_gem_installer.rb:71:in
`build_extensions'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/rubygems_gem_installer.rb:28:in
`install'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/source/rubygems.rb:200:in
`install'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/installer/gem_installer.rb:54:in
`install'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/installer/gem_installer.rb:16:in
`install_from_spec'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/installer/parallel_installer.rb:186:in
`do_install'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/installer/parallel_installer.rb:177:in
`block in worker_pool'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/worker.rb:62:in
`apply_func'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/worker.rb:57:in
`block in process_queue'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/worker.rb:54:in
`loop'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/worker.rb:54:in
`process_queue'
/nix/store/nv8k6c8503cbs1z5j9x39bmg69f56xbm-ruby-3.0.3/lib/ruby/3.0.0/bundler/worker.rb:91:in
`block (2 levels) in create_threads'

An error occurred while installing pg (1.5.3), and Bundler cannot
continue.

In Gemfile:
  pg

This is because nix goes has these channels basically

  1. Bin, binary CLI tools
  2. Dev, header files etc.
  3. Out, everything, including dynamic libraries

You have to use the .dev channel, and link the proper include path to the proper c path variable.

2 Likes