| 1 |
Installing lxml |
| 2 |
=============== |
| 3 |
|
| 4 |
.. contents:: :depth: 1 |
| 5 |
.. |
| 6 |
1 Where to get it |
| 7 |
2 Requirements |
| 8 |
3 Installation |
| 9 |
4 Building lxml from dev sources |
| 10 |
5 Using lxml with python-libxml2 |
| 11 |
6 Source builds on MS Windows |
| 12 |
7 Source builds on MacOS-X |
| 13 |
|
| 14 |
|
| 15 |
Where to get it |
| 16 |
--------------- |
| 17 |
|
| 18 |
lxml is generally distributed through PyPI_. |
| 19 |
|
| 20 |
.. _PyPI: http://pypi.python.org/pypi/lxml |
| 21 |
|
| 22 |
Most **Linux** platforms come with some version of lxml readily |
| 23 |
packaged, usually named ``python-lxml`` for the Python 2.x version |
| 24 |
and ``python3-lxml`` for Python 3.x. If you can use that version, |
| 25 |
the quickest way to install lxml is to use the system package |
| 26 |
manager, e.g. ``apt-get`` on Debian/Ubuntu:: |
| 27 |
|
| 28 |
sudo apt-get install python3-lxml |
| 29 |
|
| 30 |
For **MacOS-X**, a `macport <http://macports.org/>`_ of lxml is available. |
| 31 |
Try something like |
| 32 |
|
| 33 |
:: |
| 34 |
|
| 35 |
sudo port install py39-lxml |
| 36 |
|
| 37 |
To install a newer version or to install lxml on other systems, |
| 38 |
see below. |
| 39 |
|
| 40 |
|
| 41 |
Requirements |
| 42 |
------------ |
| 43 |
|
| 44 |
You need Python 3.8+ for lxml 6.0 and later. |
| 45 |
You need Python 3.6+ for lxml 5.0 and later. |
| 46 |
lxml versions before 5.0 support Python 2.7 and 3.6+. |
| 47 |
|
| 48 |
Unless you are using a static binary distribution (e.g. from a |
| 49 |
Windows binary installer), lxml requires libxml2 and libxslt to |
| 50 |
be installed, in particular: |
| 51 |
|
| 52 |
* `libxml2 <http://xmlsoft.org/>`_ version 2.9.2 or later. |
| 53 |
|
| 54 |
* `libxslt <http://xmlsoft.org/XSLT/>`_ version 1.1.27 or later. |
| 55 |
|
| 56 |
* We recommend libxslt 1.1.28 or later. |
| 57 |
|
| 58 |
Newer versions generally contain fewer bugs and are therefore |
| 59 |
recommended. XML Schema support is also still worked on in libxml2, |
| 60 |
so newer versions will give you better compliance with the W3C spec. |
| 61 |
|
| 62 |
To install the required development packages of these dependencies |
| 63 |
on Linux systems, use your distribution specific installation tool, |
| 64 |
e.g. apt-get on Debian/Ubuntu:: |
| 65 |
|
| 66 |
sudo apt-get install libxml2-dev libxslt-dev python-dev |
| 67 |
|
| 68 |
For Debian based systems, it should be enough to install the known |
| 69 |
build dependencies of the provided lxml package, e.g. |
| 70 |
|
| 71 |
:: |
| 72 |
|
| 73 |
sudo apt-get build-dep python3-lxml |
| 74 |
|
| 75 |
|
| 76 |
Installation |
| 77 |
------------ |
| 78 |
|
| 79 |
If your system does not provide binary packages or you want to install |
| 80 |
a newer version, the best way is to get the pip_ package management tool |
| 81 |
(or use a `virtualenv <https://pypi.python.org/pypi/virtualenv>`_) and |
| 82 |
run the following:: |
| 83 |
|
| 84 |
pip install lxml |
| 85 |
|
| 86 |
If you are not using pip in a virtualenv and want to install lxml globally |
| 87 |
instead, you have to run the above command as admin, e.g. on Linux:: |
| 88 |
|
| 89 |
sudo pip install lxml |
| 90 |
|
| 91 |
To install a specific version, either download the distribution |
| 92 |
manually and let pip install that, or pass the desired version |
| 93 |
to pip:: |
| 94 |
|
| 95 |
pip install lxml==5.0.0 |
| 96 |
|
| 97 |
.. _pip: http://pypi.python.org/pypi/pip |
| 98 |
|
| 99 |
To speed up the build in test environments, e.g. on a continuous |
| 100 |
integration server, disable the C compiler optimisations by setting |
| 101 |
the ``CFLAGS`` environment variable:: |
| 102 |
|
| 103 |
CFLAGS="-O0" pip install lxml |
| 104 |
|
| 105 |
(The option reads "minus Oh Zero", i.e. zero optimisations.) |
| 106 |
|
| 107 |
MS Windows |
| 108 |
.......... |
| 109 |
|
| 110 |
For MS Windows, we try to provide binary wheels with reasonably up-to-date |
| 111 |
libraries, although you might still want to take a look at the related |
| 112 |
`FAQ entry <FAQ.html#where-are-the-binary-builds>`_. |
| 113 |
Since it is generally difficult to build software on Windows, the library |
| 114 |
versions (libxml2, libxslt, libiconv, zlib) might not always be at the |
| 115 |
same version level as the builds on Linux or macOS. This usually means |
| 116 |
that the `WinLibs project <https://github.com/orgs/winlibs>`_ |
| 117 |
has not updated their repositories yet. If you need a more recent version, |
| 118 |
please file a ticket on their side to update it. |
| 119 |
|
| 120 |
Linux |
| 121 |
..... |
| 122 |
|
| 123 |
On Linux (and most other well-behaved operating systems), ``pip`` will |
| 124 |
manage to build the source distribution as long as libxml2 and libxslt |
| 125 |
are properly installed, including development packages, i.e. header files, |
| 126 |
etc. See the requirements section above and use your system package |
| 127 |
management tool to look for packages like ``libxml2-dev`` or |
| 128 |
``libxslt-devel``. If the build fails, make sure they are installed. |
| 129 |
|
| 130 |
Alternatively, setting ``STATIC_DEPS=true`` will download and build |
| 131 |
both libraries automatically in their latest version, e.g. |
| 132 |
``STATIC_DEPS=true pip install lxml``. |
| 133 |
|
| 134 |
MacOS-X |
| 135 |
....... |
| 136 |
|
| 137 |
On MacOS-X, we provide binary wheels ("universal2" for Python 3.9+), |
| 138 |
so just use:: |
| 139 |
|
| 140 |
sudo pip3 install lxml |
| 141 |
|
| 142 |
To build the source distribution, use the following and |
| 143 |
make sure you have a working Internet connection, as this will |
| 144 |
download libxml2 and libxslt in order to build them:: |
| 145 |
|
| 146 |
STATIC_DEPS=true sudo pip install lxml |
| 147 |
|
| 148 |
|
| 149 |
Building lxml from dev sources |
| 150 |
------------------------------ |
| 151 |
|
| 152 |
If you want to build lxml from the GitHub repository, you should read |
| 153 |
`how to build lxml from source`_ (or the file ``doc/build.txt`` in the |
| 154 |
source tree). Building from developer sources or from modified |
| 155 |
distribution sources requires Cython_ to translate the lxml sources |
| 156 |
into C code. The source distribution ships with pre-generated C |
| 157 |
source files, so you do not need Cython installed to build from |
| 158 |
release sources. |
| 159 |
|
| 160 |
.. _Cython: http://www.cython.org |
| 161 |
.. _`how to build lxml from source`: build.html |
| 162 |
|
| 163 |
If you have read these instructions and still cannot manage to install lxml, |
| 164 |
you can check the archives of the `mailing list`_ to see if your problem is |
| 165 |
known or otherwise send a mail to the list. |
| 166 |
|
| 167 |
.. _`mailing list`: http://lxml.de/mailinglist/ |
| 168 |
|
| 169 |
|
| 170 |
Using lxml with python-libxml2 |
| 171 |
------------------------------ |
| 172 |
|
| 173 |
If you want to use lxml together with the official libxml2 Python |
| 174 |
bindings (maybe because one of your dependencies uses it), you must |
| 175 |
build lxml statically. Otherwise, the two packages will interfere in |
| 176 |
places where the libxml2 library requires global configuration, which |
| 177 |
can have any kind of effect from disappearing functionality to crashes |
| 178 |
in either of the two. |
| 179 |
|
| 180 |
To get a static build, either pass the ``--static-deps`` option to the |
| 181 |
setup.py script, or run ``pip`` with the ``STATIC_DEPS`` or |
| 182 |
``STATICBUILD`` environment variable set to true, i.e. |
| 183 |
|
| 184 |
:: |
| 185 |
|
| 186 |
STATIC_DEPS=true pip install lxml |
| 187 |
|
| 188 |
The ``STATICBUILD`` environment variable is handled equivalently to |
| 189 |
the ``STATIC_DEPS`` variable, but is used by some other extension |
| 190 |
packages, too. |
| 191 |
|
| 192 |
|
| 193 |
Source builds on MS Windows |
| 194 |
--------------------------- |
| 195 |
|
| 196 |
Most MS Windows systems lack the necessary tools to build software, |
| 197 |
starting with a C compiler already. Microsoft leaves it to users to |
| 198 |
install and configure them, which is usually not trivial and means |
| 199 |
that distributors cannot rely on these dependencies being available |
| 200 |
on a given system. In a way, you get what you've paid for and make |
| 201 |
others pay for it. |
| 202 |
|
| 203 |
Due to the additional lack of package management of this platform, |
| 204 |
it is best to link the library dependencies statically if you decide |
| 205 |
to build from sources, rather than using a binary installer. For |
| 206 |
that, lxml can use the `binary distribution of libxml2 and libxslt |
| 207 |
<http://www.zlatkovic.com/libxml.en.html>`_, which it downloads |
| 208 |
automatically during the static build. It needs both libxml2 and |
| 209 |
libxslt, as well as iconv and zlib, which are available from the |
| 210 |
same download site. Further build instructions are in the |
| 211 |
`source build documentation <build.html>`_. |
| 212 |
|
| 213 |
|
| 214 |
Source builds on MacOS-X |
| 215 |
------------------------ |
| 216 |
|
| 217 |
If you are not using macports or want to use a more recent lxml |
| 218 |
release, you have to build it yourself. While the pre-installed system |
| 219 |
libraries of libxml2 and libxslt are less outdated in recent MacOS-X |
| 220 |
versions than they used to be, so lxml should work with them out of the |
| 221 |
box, it is still recommended to use a static build with the most recent |
| 222 |
library versions. |
| 223 |
|
| 224 |
Luckily, lxml's ``setup.py`` script has built-in support for building |
| 225 |
and integrating these libraries statically during the build. Please |
| 226 |
read the |
| 227 |
`MacOS-X build instructions <build.html#building-lxml-on-macos-x>`_. |