error: cannot find -lssl" after openssl upgrade broke a cargo build
Fixes cargo builds failing with cannot find -lssl after an OpenSSL upgrade moves the system libraries. Use when openssl-sys can no longer locate libssl at link time. Key trigger: point the build at the new OpenSSL location with OPENSSL_DIR, or install the OpenSSL development libraries.
TL;DR: The OpenSSL upgrade moved the development libraries, so the linker can no longer find libssl even though OpenSSL itself works fine. Point the build at the new location by setting OPENSSL_DIR before running cargo build. On Linux, installing the OpenSSL dev package achieves the same thing.
error: cannot find -lsslLocate the new OpenSSL install: on macOS run
brew --prefix openssl; on Linux runldconfig -p | grep libssl. Expected: you get a directory path whose lib folder actually contains libssl.On macOS with Homebrew, make sure OpenSSL is installed:
brew install openssl. Expected: install completes and prints the installation prefix.Point the build at it:
export OPENSSL_DIR=[the prefix from step 1], then runcargo buildin the same shell. Expected: the build links successfully with no -lssl error. If it still fails, also set OPENSSLLIBDIR to the lib folder and OPENSSLINCLUDEDIR to the include folder.On Debian or Ubuntu instead:
apt-get install -y libssl-dev, then rebuild. Expected: headers land in the standard include path and the build succeeds with no extra env vars.Alternative when you cannot touch the system: enable the vendored feature on the openssl-sys dependency so it builds OpenSSL from source. Expected: the build succeeds with no system OpenSSL required, at the cost of a longer compile.
Use this when
- The build fails with the exact "cannot find -lssl" message.
- The failure started right after an OpenSSL upgrade on the machine or image.
- openssl-sys appears in the dependency tree (check with
cargo tree -i openssl-sys).
Not for this skill when
- The openssl-sys build script fails with a different inner error (for example a missing C compiler); that is a separate problem.
- You get TLS errors at runtime; the build already succeeded, so linking is fine.
- The vendored build fails because perl or a C compiler is missing; install those first.
Variant phrasings
- "cargo cannot find -lssl macOS"
- "openssl-sys ld cannot find lssl after upgrade"
- "rust build error cannot find -lssl ubuntu"
- "linking with cc failed cannot find -lssl"
Why it happens
openssl-sys links against the system OpenSSL at build time. Package managers install new OpenSSL majors side by side (for example openssl@3 next to openssl@1.1) and eventually remove the old library directory. The linker search path still points at the old location, so it reports that libssl is missing even though a newer OpenSSL is installed elsewhere.
Edge cases
- pkg-config can also locate OpenSSL, but the OPENSSL_DIR env vars take precedence; conflicting settings cause confusing failures, so set one mechanism.
- Cross-compilation needs the target's OpenSSL dirs, not the host's.
- Pin the OpenSSL dev package version in CI images so a base-image upgrade does not surprise you again.
- The vendored feature downloads the OpenSSL source at build time, so it needs network access during the build.
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_Xhg5seltvwTbIxKQsRdX3A