19

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 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?

kmdreko
  • 42,554
  • 6
  • 57
  • 106
CoderLee
  • 3,079
  • 3
  • 25
  • 57

8 Answers8

31

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.

Frostack
  • 661
  • 11
  • 13
  • 1
    Got stuck on this a while, for databases other than PG this link has the appropriate path commands https://steemit.com/programming/@mrblueberry/installing-rust-and-diesel-for-rocket-on-windows-10 – gt124 Apr 28 '21 at 03:58
  • 2
    This works for me. I don't know where to find libpq.lib file as mentioned in the 'answered' and typing the command (or copy pasting in my case) is much easier and it works. – Eko Andri Apr 30 '21 at 10:35
  • In my case I have also to link bin directory of postgres – Bourdier Jonathan Jul 17 '21 at 11:03
  • This works for me. First time I tried I used the autocomplete of Powershell which automatically adds a slash `\\` to the end of the file. This does **not** work. It is important to have no slash at the end of the above command. – Marco Dec 30 '22 at 13:19
21

Update:

The answer below is a work around for older versions. Please check the possibility to execute cargo clean first


Original Version

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

ASpirin
  • 3,601
  • 1
  • 23
  • 32
  • 2
    This should not be considered the accepted answer since it's only a workaround as mentioned by @Balen in an answer below and the right answer is actually adding the lib path to the PATH environment variable and in case you are still running into issues use `cargo clean` first to actually get proper results. – sikkiv Aug 03 '22 at 15:22
8

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

sudo apt install libpq-dev
user850010
  • 6,311
  • 12
  • 39
  • 60
  • This worked for me on Ubuntu 22.04, postgres 14.6. `libpq-dev` seems to be binaries and headers postgres. https://pypi.org/project/libpq-dev/ https://launchpad.net/ubuntu/jammy/+package/libpq-dev – onx2 Feb 14 '23 at 15:36
5

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)

Wulf
  • 153
  • 1
  • 8
4

If you've attempted to cargo build (or anything that runs the build scripts for libpq rust crate) when your environment was invalid, then you need to do a cargo clean after fixing your environment otherwise you'll still get the libpq.lib not found error even when it's in your PATH. The other answers where you copy the file into another directory are just hacks

Balen
  • 41
  • 1
3

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

super IT guy
  • 195
  • 2
  • 12
3

You can instead add the path to .../lib to the compilers' library search paths, using RUSTFLAGS environment variable. It works both for installing diesel_cli and for building your projects.

RUSTFLAGS='-L /usr/local/pgsql/lib' cargo build

On Windows with EDB installer, the path contains a space, so use CARGO_ENCODED_RUSTFLAGS instead. For PowerShell:

$env:CARGO_ENCODED_RUSTFLAGS = "-L`u{1f}C:\Program Files\PostgreSQL\14\lib"
Jonathan Giroux
  • 377
  • 3
  • 5
  • Is there a way to insert the `0x1f` special character in a Windows command prompt (instead of using Powershell)? – Miscreant Apr 13 '23 at 14:20
  • Using `RUSTFLAGS="-L /usr/local/opt/libpq/lib" cargo test [package]` fixed my issue on Mac OS (Ventura)! – BanjoFox Jul 12 '23 at 05:15
0

This is what I had to do in my case:

Edit $HOME/.cargo/config.toml:

[target.x86_64-pc-windows-msvc.pq]
rustc-link-search = ["C:\\Program Files\\PostgreSQL\\13\\lib"]
rustc-link-lib = ["libpq"]

Make sure to replace the rustc-link-search with the path containing the libpq.lib file.

diogovk
  • 2,108
  • 2
  • 19
  • 24