Contents
lxml is generally distributed throughPyPI.
MostLinux platforms come with some version of lxml readilypackaged, usually namedpython-lxml for the Python 2.x versionandpython3-lxml for Python 3.x. If you can use that version,the quickest way to install lxml is to use the system packagemanager, e.g.apt-get on Debian/Ubuntu:
sudo apt-get install python3-lxml
ForMacOS-X, amacport of lxml is available.Try something like
sudo port install py27-lxml
To install a newer version or to install lxml on other systems,see below.
You need Python 3.6+ for lxml 5.0 and later.lxml versions before 5.0 support Python 2.7 and 3.6+.
Unless you are using a static binary distribution (e.g. from aWindows binary installer), lxml requires libxml2 and libxslt tobe installed, in particular:
Newer versions generally contain fewer bugs and are thereforerecommended. XML Schema support is also still worked on in libxml2,so newer versions will give you better compliance with the W3C spec.
To install the required development packages of these dependencieson Linux systems, use your distribution specific installation tool,e.g. apt-get on Debian/Ubuntu:
sudo apt-get install libxml2-dev libxslt-dev python-dev
For Debian based systems, it should be enough to install the knownbuild dependencies of the provided lxml package, e.g.
sudo apt-get build-dep python3-lxml
If your system does not provide binary packages or you want to installa newer version, the best way is to get thepip package management tool(or use avirtualenv) andrun the following:
pip install lxml
If you are not using pip in a virtualenv and want to install lxml globallyinstead, you have to run the above command as admin, e.g. on Linux:
sudo pip install lxml
To install a specific version, either download the distributionmanually and let pip install that, or pass the desired versionto pip:
pip install lxml==5.0.0
To speed up the build in test environments, e.g. on a continuousintegration server, disable the C compiler optimisations by settingtheCFLAGS environment variable:
CFLAGS="-O0" pip install lxml
(The option reads "minus Oh Zero", i.e. zero optimisations.)
For MS Windows, we try to provide binary wheels with reasonably up-to-datelibraries, although you might still want to take a look at the relatedFAQ entry.Since it is generally difficult to build software on Windows, the libraryversions (libxml2, libxslt, libiconv, zlib) might not always be at thesame version level as the builds on Linux or macOS. This usually meansthat theWinLibs projecthas not updated their repositories yet. If you need a more recent version,please file a ticket on their side to update it.
On Linux (and most other well-behaved operating systems),pip willmanage to build the source distribution as long as libxml2 and libxsltare properly installed, including development packages, i.e. header files,etc. See the requirements section above and use your system packagemanagement tool to look for packages likelibxml2-dev orlibxslt-devel. If the build fails, make sure they are installed.
Alternatively, settingSTATIC_DEPS=true will download and buildboth libraries automatically in their latest version, e.g.STATIC_DEPS=true pip install lxml.
On MacOS-X, we provide binary wheels ("universal2" for Python 3.9+),so just use:
sudo pip3 install lxml
To build the source distribution, use the following andmake sure you have a working Internet connection, as this willdownload libxml2 and libxslt in order to build them:
STATIC_DEPS=true sudo pip install lxml
If you want to build lxml from the GitHub repository, you should readhow to build lxml from source (or the filedoc/build.txt in thesource tree). Building from developer sources or from modifieddistribution sources requiresCython to translate the lxml sourcesinto C code. The source distribution ships with pre-generated Csource files, so you do not need Cython installed to build fromrelease sources.
If you have read these instructions and still cannot manage to install lxml,you can check the archives of themailing list to see if your problem isknown or otherwise send a mail to the list.
If you want to use lxml together with the official libxml2 Pythonbindings (maybe because one of your dependencies uses it), you mustbuild lxml statically. Otherwise, the two packages will interfere inplaces where the libxml2 library requires global configuration, whichcan have any kind of effect from disappearing functionality to crashesin either of the two.
To get a static build, either pass the--static-deps option to thesetup.py script, or runpip with theSTATIC_DEPS orSTATICBUILD environment variable set to true, i.e.
STATIC_DEPS=true pip install lxml
TheSTATICBUILD environment variable is handled equivalently totheSTATIC_DEPS variable, but is used by some other extensionpackages, too.
Most MS Windows systems lack the necessarily tools to build software,starting with a C compiler already. Microsoft leaves it to users toinstall and configure them, which is usually not trivial and meansthat distributors cannot rely on these dependencies being availableon a given system. In a way, you get what you've paid for and makeothers pay for it.
Due to the additional lack of package management of this platform,it is best to link the library dependencies statically if you decideto build from sources, rather than using a binary installer. Forthat, lxml can use thebinary distribution of libxml2 and libxslt, which it downloadsautomatically during the static build. It needs both libxml2 andlibxslt, as well as iconv and zlib, which are available from thesame download site. Further build instructions are in thesource build documentation.
If you are not using macports or want to use a more recent lxmlrelease, you have to build it yourself. While the pre-installed systemlibraries of libxml2 and libxslt are less outdated in recent MacOS-Xversions than they used to be, so lxml should work with them out of thebox, it is still recommended to use a static build with the most recentlibrary versions.
Luckily, lxml'ssetup.py script has built-in support for buildingand integrating these libraries statically during the build. Pleaseread theMacOS-X build instructions.