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