How to install lxml on Ubuntu
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The lxml library is one of the fastest and most feature-rich XML and HTML processing libraries available for Python, built on top of the C libraries libxml2 and libxslt. Because it includes C extensions, installing it on Ubuntu is not always as simple as running pip install. Understanding why the installation can fail and what system-level dependencies are needed will save you from frustrating build errors.
Why pip install lxml Can Fail
When you run pip install lxml, pip downloads the source distribution and attempts to compile the C extensions on your machine. This compilation step requires C header files and development libraries that are not installed on a fresh Ubuntu system by default. Without them, you will see errors like fatal error: libxml/xmlversion.h: No such file or directory or error: command 'gcc' failed.
The fix is straightforward: install the system-level build dependencies before running pip.
Installing System Dependencies
Before installing lxml through pip, install the required development libraries and a C compiler:
Here is what each package provides:
build-essential-- the GCC compiler and related build toolspython3-dev-- Python header files needed to compile C extensionslibxml2-dev-- development headers for the libxml2 XML parserlibxslt1-dev-- development headers for the libxslt XSLT processorzlib1g-dev-- compression library headers used by libxml2
Once those are in place, pip can compile lxml without errors:
Installing lxml via pip in a Virtual Environment
Using a virtual environment is the recommended practice because it isolates your project dependencies from the system Python. The system-level C libraries still need to be installed globally, but the Python package itself lives inside the virtual environment:
This avoids conflicts between projects that may need different versions of lxml.
Using apt install as an Alternative
If you do not need the latest version of lxml or want to avoid compilation entirely, Ubuntu ships a pre-built package:
This installs a version packaged by Ubuntu maintainers, so it may lag behind the latest PyPI release. It also installs into the system Python, not into a virtual environment. This approach works well for system scripts or quick prototyping where having the absolute latest version is not critical.
Verifying the Installation
After installation, confirm that lxml is working correctly:
Expected output:
If the import succeeds and the version prints, your installation is working.
Troubleshooting Common Compilation Errors
If you still see build failures after installing the dependencies, try these steps:
If pkg-config cannot find libxml-2.0, the development headers are missing or not properly registered. Reinstalling libxml2-dev usually resolves this.
On older Ubuntu versions (18.04 or earlier), you may also need to install python3-pip explicitly:
Common Pitfalls
- Running
pip install lxmlwithout system dependencies results in cryptic C compilation errors. Always installlibxml2-devandlibxslt1-devfirst. - Forgetting
python3-devmeans the Python header files are missing, causing the build to fail even when the XML libraries are present. - Mixing system lxml with virtualenv lxml can lead to import confusion. Choose one approach and stick with it for each project.
- Using
sudo pip installinstalls packages into the system Python and can break system tools. Use a virtual environment orapt install python3-lxmlinstead. - Assuming
apt install python3-lxmlgives the latest version can cause issues if your code depends on recent lxml features. Check the version withpython3 -c "import lxml; print(lxml.__version__)".
Summary
- Install system build dependencies (
libxml2-dev,libxslt1-dev,python3-dev,zlib1g-dev) before runningpip install lxml. - Use a virtual environment to keep project dependencies isolated while still relying on system-level C libraries.
- Use
sudo apt install python3-lxmlas a quick alternative that avoids compilation entirely, at the cost of a potentially older version. - Verify the installation by importing
lxmland running a basic XML operation. - Keep
pip,setuptools, andwheelup to date to minimize build issues.

