'How to fix Rust diesel cli link libpq.lib error on install

I'm trying (for hours now) to install the cargo crate diesel_cli for postgres. However, every time I run the recommended cargo command:

cargo install diesel_cli --no-default-features --features postgres

I wait a few minutes just to see the same build fail -> abort error, with this message:

note: LINK : fatal error LNK1181: cannot open input file 'libpq.lib'


error: aborting due to previous error

error: failed to compile `diesel_cli v1.4.1`, intermediate artifacts can be found at `C:\Users\<user name here>\AppData\Local\Temp\cargo-installUU2DtT`

Caused by:
  could not compile `diesel_cli`.

I'm running postgres in a docker container and have the binaries on my C:\pgsql with the lib and bin directories both on the PATH so I can't figure out why it's not linking. What else could be required they didn't mention in the docs?



Solution 1:[1]

Adding the folder to the PATH variable didn't help, at least in my case, as by some reason it is not used in the /LIBPATH parameter passed to link.exe. In my case it was C:\Users\<username>\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib You can see it in the beginning of the error message. Copy libpq.lib in there and it will be used from there.

After installation diesel would require some other assemblies. Copy libcrypto-1_1-x64.dll, libiconv-2.dll and libssl-1_1-x64.dll into the folder showed after where diesel command execution

Solution 2:[2]

In my case the installation was successful but when I tried to run it this error occured. maybe this would work for others who have the same problem:

  • open PowerShell
  • type in setx PQ_LIB_DIR "C:\Program Files\PostgreSQL\13\lib" (or any other path to your PostgreSQL lib)
  • restart your PC
  • run again

I had the same issue with WSL, if you're on Linux probably you could find PostgreSQL lib location and add it to your environment variables.

Solution 3:[3]

I had the same error on Ubuntu and for me the following install fixed the issue:

sudo apt install libpq-dev

Solution 4:[4]

No need to move files around, just add C:\Program Files\PostgreSQL\14\lib and C:\Program Files\PostgreSQL\14\bin to your PATH. Installing and running diesel should have no problems.

Note: your paths may be different, and remember to close/reopen your terminal so the PATH variable is updated.

(Tested on Windows 10)

Solution 5:[5]

To give clear steps for windows:

  1. Add C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib in the path in environment variables

  2. Copy libpq.lib that is in C:\Program Files\PostgreSQL\14\lib (obviously this is with version 14) and paste it in C:\Users<username>.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\x86_64-pc-windows-msvc\lib

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2
Solution 3 user850010
Solution 4 Wulf
Solution 5 super IT guy