Skip to content
Latchkey

pip "Could not build wheels for lxml" - libxml2/libxslt Missing

lxml binds libxml2 and libxslt. When pip builds it from source without those libraries’ development headers, the compile fails on a missing xmlversion.h (or libxslt header).

What this error means

Installing lxml fails with fatal error: libxml/xmlversion.h: No such file or directory (or a libxslt header), then "Could not build wheels for lxml". A wheel would avoid the build, so this appears on platforms lxml has no wheel for or when forcing a source build.

pip output
  src/lxml/etree.c:96:10: fatal error: libxml/xmlversion.h: No such file or directory
   96 | #include "libxml/xmlversion.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~
  compilation terminated.
ERROR: Could not build wheels for lxml

Common causes

libxml2/libxslt dev headers missing

lxml compiles against libxml2 and libxslt. Without libxml2-dev and libxslt1-dev (and their headers) the build cannot find xmlversion.h and aborts.

Forced or fallback source build

With no compatible wheel (musl, exotic arch, very new Python) pip builds lxml from sdist and exposes the missing system libraries.

How to fix it

Prefer a prebuilt wheel

lxml publishes manylinux/macOS/Windows wheels. Upgrade pip so it selects one and skips the C build.

Terminal
python -m pip install --upgrade pip
pip install --only-binary lxml lxml

Install libxml2/libxslt dev packages

Terminal
# Debian/Ubuntu
apt-get update && apt-get install -y build-essential python3-dev libxml2-dev libxslt1-dev zlib1g-dev
# Alpine
apk add --no-cache gcc musl-dev libxml2-dev libxslt-dev python3-dev
pip install lxml

How to prevent it

  • Prefer wheels for lxml in CI.
  • Bake libxml2-dev/libxslt1-dev into images that build from source.
  • Use a glibc base so manylinux wheels apply.

Related guides

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