| 1 |
How to build lxml from source |
| 2 |
============================= |
| 3 |
|
| 4 |
To build lxml from source, you need libxml2 and libxslt properly |
| 5 |
installed, *including the header files*. These are likely shipped in |
| 6 |
separate ``-dev`` or ``-devel`` packages like ``libxml2-dev``, which |
| 7 |
you must install before trying to build lxml. |
| 8 |
|
| 9 |
.. contents:: |
| 10 |
.. |
| 11 |
1 Cython |
| 12 |
2 Github, git and hg |
| 13 |
3 Building the sources |
| 14 |
4 Running the tests and reporting errors |
| 15 |
5 Building an egg |
| 16 |
6 Building lxml on MacOS-X |
| 17 |
7 Static linking on Windows |
| 18 |
8 Building Debian packages from SVN sources |
| 19 |
|
| 20 |
|
| 21 |
Cython |
| 22 |
------ |
| 23 |
|
| 24 |
.. _pip: http://pypi.python.org/pypi/pip |
| 25 |
.. _Cython: http://cython.org |
| 26 |
.. _wheel: https://wheel.readthedocs.io/en/latest/ |
| 27 |
|
| 28 |
The lxml.etree and lxml.objectify modules are written in Cython_. |
| 29 |
Since we distribute the Cython-generated .c files with lxml releases, |
| 30 |
however, you do not need Cython to build lxml from the normal release |
| 31 |
sources. We even encourage you to *not install Cython* for a normal |
| 32 |
release build, as the generated C code can vary quite heavily between |
| 33 |
Cython versions, which may or may not generate correct code for lxml. |
| 34 |
The pre-generated release sources were tested and therefore are known |
| 35 |
to work. |
| 36 |
|
| 37 |
So, if you want a reliable build of lxml, we suggest a) using a |
| 38 |
source release of lxml and b) disabling or uninstalling Cython for the |
| 39 |
build. |
| 40 |
|
| 41 |
*Only* if you are interested in building lxml from a checkout of the |
| 42 |
developer sources (e.g. to test a bug fix that has not been released |
| 43 |
yet) or if you want to be an lxml developer, then you do need a |
| 44 |
working Cython installation. You can use pip_ to install it:: |
| 45 |
|
| 46 |
pip install -r requirements.txt |
| 47 |
|
| 48 |
https://github.com/lxml/lxml/blob/master/requirements.txt |
| 49 |
|
| 50 |
lxml 5.x currently uses at least Cython 3.0. Later release versions |
| 51 |
are generally preferred. lxml 4.x used Cython 0.29.x instead. |
| 52 |
|
| 53 |
|
| 54 |
Github, git and hg |
| 55 |
------------------- |
| 56 |
|
| 57 |
The lxml package is developed in a repository on Github_ using |
| 58 |
Mercurial_ and the `hg-git`_ plugin. You can retrieve the current |
| 59 |
developer version using:: |
| 60 |
|
| 61 |
hg clone git+ssh://[email protected]/lxml/lxml.git lxml |
| 62 |
|
| 63 |
Or, using git:: |
| 64 |
|
| 65 |
git clone ssh://[email protected]/lxml/lxml.git lxml |
| 66 |
|
| 67 |
This will create a directory ``lxml`` and download the source into it, |
| 68 |
including the complete development history. Don't be afraid, the |
| 69 |
repository download is fairly quick. You can also browse the |
| 70 |
`lxml repository`_ through the web or download a ZIP archive with the |
| 71 |
`latest master branch <https://github.com/lxml/lxml/archive/master.zip>`_. |
| 72 |
|
| 73 |
.. _Github: https://github.com/lxml/ |
| 74 |
.. _Mercurial: http://mercurial.selenic.com/ |
| 75 |
.. _`hg-git`: http://hg-git.github.com/ |
| 76 |
.. _`lxml repository`: https://github.com/lxml/lxml |
| 77 |
.. _`source tar-ball`: https://github.com/lxml/lxml/tarball/master |
| 78 |
|
| 79 |
|
| 80 |
Building the sources |
| 81 |
--------------------- |
| 82 |
|
| 83 |
Clone the source repository as described above (or download |
| 84 |
the `source tar-ball`_ and unpack it) and then type:: |
| 85 |
|
| 86 |
python setup.py build |
| 87 |
|
| 88 |
or:: |
| 89 |
|
| 90 |
python setup.py bdist_egg # requires 'setuptools' or 'distribute' |
| 91 |
|
| 92 |
To (re-)build the C sources with Cython, you must additionally pass the |
| 93 |
option ``--with-cython``:: |
| 94 |
|
| 95 |
python setup.py build --with-cython |
| 96 |
|
| 97 |
If you want to test lxml from the source directory, it is better to build it |
| 98 |
in-place like this:: |
| 99 |
|
| 100 |
python setup.py build_ext -i --with-cython |
| 101 |
|
| 102 |
or, in Unix-like environments:: |
| 103 |
|
| 104 |
make inplace |
| 105 |
|
| 106 |
To speed up the build in test environments (e.g. on a continuous |
| 107 |
integration server), set the ``CFLAGS`` environment variable to |
| 108 |
disable C compiler optimisations (e.g. "-O0" for gcc, that's |
| 109 |
minus-oh-zero), for example:: |
| 110 |
|
| 111 |
CFLAGS="-O0" make inplace |
| 112 |
|
| 113 |
If you get errors about missing header files (e.g. ``Python.h`` or |
| 114 |
``libxml/xmlversion.h``) then you need to make sure the development |
| 115 |
packages of Python, libxml2 and libxslt are properly installed. On |
| 116 |
Linux distributions, they are usually called something like |
| 117 |
``libxml2-dev`` or ``libxslt-devel``. If these packages were |
| 118 |
installed in non-standard places, try passing the following option to |
| 119 |
setup.py to make sure the right config is found:: |
| 120 |
|
| 121 |
python setup.py build --with-xslt-config=/path/to/xslt-config |
| 122 |
|
| 123 |
There are also env vars to allow overriding the config tool:: |
| 124 |
|
| 125 |
env XML2_CONFIG=/path/to/xml2-config python build |
| 126 |
|
| 127 |
You may also use ``pkg-config`` as the tools:: |
| 128 |
|
| 129 |
env XSLT_CONFIG="pkg-config libxslt" python setup.py build |
| 130 |
|
| 131 |
If this doesn't help, you may have to add the location of the header |
| 132 |
files to the include path like:: |
| 133 |
|
| 134 |
python setup.py build_ext -i -I /usr/include/libxml2 |
| 135 |
|
| 136 |
where the file is in ``/usr/include/libxml2/libxml/xmlversion.h`` |
| 137 |
|
| 138 |
For static builds, if you get an error saying "recompile with -fPIC", |
| 139 |
do so by adding it to your `CFLAGS` environment variable: |
| 140 |
``env CFLAGS="$CFLAGS -fPIC"``, such as:: |
| 141 |
|
| 142 |
env CFLAGS="$CFLAGS -fPIC" python3 setup.py build_ext -i --with-cython --static-deps |
| 143 |
|
| 144 |
To use lxml.etree in-place, you can place lxml's ``src`` directory |
| 145 |
on your Python module search path (PYTHONPATH) and then import |
| 146 |
``lxml.etree`` to play with it:: |
| 147 |
|
| 148 |
# cd lxml |
| 149 |
# PYTHONPATH=src python3 |
| 150 |
Python 3.10.2 |
| 151 |
Type "help", "copyright", "credits" or "license" for more information. |
| 152 |
>>> from lxml import etree |
| 153 |
>>> |
| 154 |
|
| 155 |
For non-static builds, you may have to set ``LD_LIBRARY_PATH`` to where the |
| 156 |
shared object files for libxml2 and libxslt are, such as ``/usr/local/lib``. For |
| 157 |
example:: |
| 158 |
|
| 159 |
PYTHONPATH=src LD_LIBRARY_PATH=/usr/local/lib python3 |
| 160 |
|
| 161 |
To make sure everything gets recompiled cleanly after changes, you can |
| 162 |
run ``make clean`` or delete the file ``src/lxml/etree.c``. |
| 163 |
|
| 164 |
|
| 165 |
Running the tests and reporting errors |
| 166 |
-------------------------------------- |
| 167 |
|
| 168 |
The source distribution (tgz) and the source repository contain a test |
| 169 |
suite for lxml. You can run it from the top-level directory:: |
| 170 |
|
| 171 |
python test.py |
| 172 |
|
| 173 |
Note that the test script only tests the in-place build (see distutils |
| 174 |
building above), as it searches the ``src`` directory. You can use the |
| 175 |
following one-step command to trigger an in-place build and test it:: |
| 176 |
|
| 177 |
make test |
| 178 |
|
| 179 |
This also runs the ElementTree and cElementTree compatibility tests. To call |
| 180 |
them separately, make sure you have lxml on your PYTHONPATH first, then run:: |
| 181 |
|
| 182 |
python selftest.py |
| 183 |
|
| 184 |
and:: |
| 185 |
|
| 186 |
python selftest2.py |
| 187 |
|
| 188 |
If the tests give failures, errors, or worse, segmentation faults, we'd really |
| 189 |
like to know. Please contact us on the `mailing list`_, and please specify |
| 190 |
the version of lxml, libxml2, libxslt and Python you were using, as well as |
| 191 |
your operating system type (Linux, Windows, MacOS-X, ...). |
| 192 |
|
| 193 |
.. _`mailing list`: https://lxml.de/mailinglist/ |
| 194 |
|
| 195 |
|
| 196 |
Building an egg or wheel |
| 197 |
------------------------ |
| 198 |
|
| 199 |
This is the procedure to make an lxml egg or wheel_ for your platform. |
| 200 |
It assumes that you have ``setuptools`` or ``distribute`` installed, as well |
| 201 |
as the ``wheel`` package. |
| 202 |
|
| 203 |
First, download the lxml-x.y.tar.gz release. This contains the pregenerated |
| 204 |
C files so that you can be sure you build exactly from the release sources. |
| 205 |
Unpack them and ``cd`` into the resulting directory. Then, to build a wheel, |
| 206 |
simply run the command |
| 207 |
|
| 208 |
:: |
| 209 |
|
| 210 |
python setup.py bdist_wheel |
| 211 |
|
| 212 |
or, to build a statically linked wheel with all of libxml2, libxslt and |
| 213 |
friends compiled in, run |
| 214 |
|
| 215 |
python setup.py bdist_wheel --static-deps |
| 216 |
|
| 217 |
The resulting .whl file will be written into the ``dist`` directory. |
| 218 |
|
| 219 |
To build an egg file, run |
| 220 |
|
| 221 |
:: |
| 222 |
|
| 223 |
python setup.py build_egg |
| 224 |
|
| 225 |
If you are on a Unix-like platform, you can first build the extension modules |
| 226 |
using |
| 227 |
|
| 228 |
:: |
| 229 |
|
| 230 |
python setup.py build |
| 231 |
|
| 232 |
and then ``cd`` into the directory ``build/lib.your.platform`` to call |
| 233 |
``strip`` on any ``.so`` file you find there. This reduces the size of |
| 234 |
the binary distribution considerably. Then, from the package root directory, |
| 235 |
call |
| 236 |
|
| 237 |
:: |
| 238 |
|
| 239 |
python setup.py bdist_egg |
| 240 |
|
| 241 |
This will quickly package the pre-built packages into an egg file and |
| 242 |
drop it into the ``dist`` directory. |
| 243 |
|
| 244 |
|
| 245 |
Building lxml on MacOS-X |
| 246 |
------------------------ |
| 247 |
|
| 248 |
Apple regularly ships new system releases with horribly outdated |
| 249 |
system libraries. This is specifically the case for libxml2 and |
| 250 |
libxslt, where the system provided versions used to be too old |
| 251 |
to even build lxml for a long time. |
| 252 |
|
| 253 |
While the Unix environment in MacOS-X makes it relatively easy to |
| 254 |
install Unix/Linux style package management tools and new software, it |
| 255 |
actually seems to be hard to get libraries set up for exclusive usage |
| 256 |
that MacOS-X ships in an older version. Alternative distributions |
| 257 |
(like macports) install their libraries in addition to the system |
| 258 |
libraries, but the compiler and the runtime loader on MacOS still see |
| 259 |
the system libraries before the new libraries. This can lead to |
| 260 |
undebuggable crashes where the newer library seems to be loaded but |
| 261 |
the older system library is used. |
| 262 |
|
| 263 |
Apple discourages static building against libraries, which would help |
| 264 |
working around this problem. Apple does not ship static library |
| 265 |
binaries with its system and several package management systems follow |
| 266 |
this decision. Therefore, building static binaries requires building |
| 267 |
the dependencies first. The ``setup.py`` script does this |
| 268 |
automatically when you call it like this:: |
| 269 |
|
| 270 |
python setup.py build --static-deps |
| 271 |
|
| 272 |
This will download and build the latest versions of libxml2 and |
| 273 |
libxslt from the official FTP download site. If you want to use |
| 274 |
specific versions, or want to prevent any online access, you can |
| 275 |
download both ``tar.gz`` release files yourself, place them into a |
| 276 |
subdirectory ``libs`` in the lxml distribution, and call ``setup.py`` |
| 277 |
with the desired target versions like this:: |
| 278 |
|
| 279 |
python setup.py build --static-deps \ |
| 280 |
--libxml2-version=2.9.12 \ |
| 281 |
--libxslt-version=1.1.34 \ |
| 282 |
|
| 283 |
sudo python setup.py install |
| 284 |
|
| 285 |
Instead of ``build``, you can use any target, like ``bdist_egg`` |
| 286 |
if you want to use setuptools to build an installable egg, or |
| 287 |
``bdist_wheel`` for a wheel package. |
| 288 |
|
| 289 |
Note that this also works with pip_. Since you can't pass |
| 290 |
command line options in this case, you have to use an environment |
| 291 |
variable instead:: |
| 292 |
|
| 293 |
STATIC_DEPS=true pip install lxml |
| 294 |
|
| 295 |
To install the package into the system Python package directory, |
| 296 |
run the installation with "sudo":: |
| 297 |
|
| 298 |
STATIC_DEPS=true sudo pip install lxml |
| 299 |
|
| 300 |
The ``STATICBUILD`` environment variable is handled equivalently to |
| 301 |
the ``STATIC_DEPS`` variable, but is used by some other extension |
| 302 |
packages, too. |
| 303 |
|
| 304 |
If you decide to do a non-static build, you may also have to install |
| 305 |
the command line tools in addition to the XCode build environment. |
| 306 |
They are available as a restricted download from here: |
| 307 |
|
| 308 |
https://developer.apple.com/downloads/index.action?=command%20line%20tools# |
| 309 |
|
| 310 |
Without them, the compiler may not find the necessary header files of |
| 311 |
the XML libraries, according to the second comment in this ticket: |
| 312 |
|
| 313 |
https://bugs.launchpad.net/lxml/+bug/1244094 |
| 314 |
|
| 315 |
|
| 316 |
Static linking on Windows |
| 317 |
------------------------- |
| 318 |
|
| 319 |
Most operating systems have proper package management that makes installing |
| 320 |
current versions of libxml2 and libxslt easy. The most famous exception is |
| 321 |
Microsoft Windows, which entirely lacks these capabilities. To work around |
| 322 |
the limits of this platform, lxml's installation can download pre-built |
| 323 |
packages of the dependencies and build statically against them. Assuming |
| 324 |
you have a proper C compiler setup to build Python extensions, this should |
| 325 |
work:: |
| 326 |
|
| 327 |
python setup.py bdist_wininst --static-deps |
| 328 |
|
| 329 |
It should create a windows installer in the ``pkg`` directory. |
| 330 |
|
| 331 |
|
| 332 |
Building Debian packages from SVN sources |
| 333 |
----------------------------------------- |
| 334 |
|
| 335 |
`Andreas Pakulat`_ proposed the following approach. |
| 336 |
|
| 337 |
.. _`Andreas Pakulat`: http://thread.gmane.org/gmane.comp.python.lxml.devel/1239/focus=1249 |
| 338 |
|
| 339 |
* ``apt-get source lxml`` |
| 340 |
* remove the unpacked directory |
| 341 |
* tar.gz the lxml SVN version and replace the orig.tar.gz that lies in the |
| 342 |
directory |
| 343 |
* check md5sum of created tar.gz file and place new sum and size in dsc file |
| 344 |
* do ``dpkg-source -x lxml-[VERSION].dsc`` and cd into the newly created directory |
| 345 |
* run ``dch -i`` and add a comment like "use trunk version", this will |
| 346 |
increase the debian version number so apt/dpkg won't get confused |
| 347 |
* run ``dpkg-buildpackage -rfakeroot -us -uc`` to build the package |
| 348 |
|
| 349 |
In case ``dpkg-buildpackage`` tells you that some dependencies are missing, you |
| 350 |
can either install them manually or run ``apt-get build-dep lxml``. |
| 351 |
|
| 352 |
That will give you .deb packages in the parent directory which can be |
| 353 |
installed using ``dpkg -i``. |