Skip to content
Latchkey

Rust bindgen "Unable to find libclang" in CI

A crate that runs bindgen at build time could not find libclang. bindgen uses libclang to parse C headers, and a minimal runner has no clang installed, so the build script panics.

What this error means

A -sys crate (or one with a build.rs calling bindgen) fails the custom build command with Unable to find libclang and a list of library names it tried. The pure-Rust crates compile; only the bindgen step fails.

cargo
error: failed to run custom build command for `rocksdb-sys v0.6.1`
  --- stderr
  thread 'main' panicked at 'Unable to find libclang: "couldn't find any
  valid shared libraries matching: ['libclang.so', ...]"'

Common causes

libclang not installed

bindgen needs the libclang shared library. A slim image without clang/libclang-dev has nothing for it to load.

libclang present but not discoverable

On some distros or macOS the library exists in a non-default path; bindgen cannot find it without LIBCLANG_PATH pointing at the directory.

How to fix it

Install clang and libclang

Terminal
# Debian/Ubuntu
apt-get update && apt-get install -y clang libclang-dev
# Alpine
apk add --no-cache clang-dev llvm-dev
# RHEL/Fedora
dnf install -y clang clang-devel

Point bindgen at libclang explicitly

Set LIBCLANG_PATH if the library lives in a non-standard directory.

Terminal
export LIBCLANG_PATH="$(llvm-config --libdir)"
cargo build --locked

How to prevent it

  • Bake clang/libclang-dev into images that build bindgen-based -sys crates.
  • Set LIBCLANG_PATH when libclang is in a non-standard prefix.
  • Pin -sys crate versions so bindgen behavior stays stable.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →