Ruby cannot find libpq (and neither can I)

nixos-version: 23.11.7503.a2e1d0414259 (Tapir)

I’m setting up a rails app and want to use Postgresql locally.

I have installed the following in my config:

  environment.systemPackages = with pkgs; [
      ruby_3_3 rubyPackages_3_3.railties postgresql_15 
  services.postgresql = 
    { enable = true;
    ensureDatabases = [ "mydatabase" ];
    authentication = pkgs.lib.mkOverride 10 ''
      #type database  DBuser  auth-method
      local all       all     trust

I add gem 'pg' to my Gemfile and run bundle install, but get the following error:

Using libpq from /nix/store/vck707ss9ihj3ygn183mlp8kl525xdm9-postgresql-15.7-lib/lib
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

So I’m trying to install libpq, but nothing I’ve found will get me past this error.

I’ve tried: libpqxx haskellPackages.postgresql-libpq postgresql.lib pkg-config, but I always get stuck at this error.

Can anyone give me pointers for where I might be going wrong?

1 Like

As far as I know, you need to be in a dev shell in order for the header files to be accessible:

# default.nix
  pkgs ? import <nixpkgs> { },
pkgs.mkShell {
  nativeBuildInputs = [

With this, I could install pg inside the shell:

$ nix-shell
$ gem install --user-install pg
Fetching pg-1.5.6.gem
WARNING:  You don't have /home/user/.local/share/gem/ruby/3.1.0/bin in your PATH,
          gem executables will not run.
Building native extensions. This could take a while...
Successfully installed pg-1.5.6
Parsing documentation for pg-1.5.6
Installing ri documentation for pg-1.5.6
Done installing documentation for pg after 4 seconds
1 gem installed

That being said, I think following the nixpkgs docs on Ruby development might be a better solution.

That’s really helpful, thanks.

1 Like