Installing lxml
===============

For special installation instructions regarding MS Windows and
MacOS-X, see the specific sections below.

.. contents::
..
   1  Requirements
   2  Installation
   3  Building lxml from sources
   4  Using lxml with python-libxml2
   5  MS Windows
   6  MacOS-X


Requirements
------------

You need Python 2.6 or later.

Unless you are using a static binary distribution (e.g. from a
Windows binary installer), you need to install libxml2 and libxslt,
in particular:

* libxml2 2.7.0 or later. It can be found here:
  http://xmlsoft.org/downloads.html

  * We recommend libxml2 2.9.0 or a later version.

  * If you want to use the feed parser interface, especially when
    parsing from unicode strings, do not use libxml2 2.7.4 through
    2.7.6.

* libxslt 1.1.23 or later. It can be found here:
  http://xmlsoft.org/XSLT/downloads.html

  * We recommend libxslt 1.1.26 or later. Version 1.1.25 will not
    work due to a missing library symbol.

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

.. _PyPI: http://pypi.python.org/pypi/lxml


Installation
------------

The best way to install lxml is to get the pip_ package management
tool and run the following as super-user (or administrator)::

  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==3.1.2

.. _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

* For **MS Windows**, recent lxml releases feature community donated
  binary distributions, although you might still want to take a look
  at the related `FAQ entry <FAQ.html#where-are-the-binary-builds>`_.
  If you fail to build lxml on your MS Windows system from the signed
  and tested sources that we release, consider using the binary builds
  from PyPI or the `unofficial Windows binaries
  <http://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml>`_
  that Christoph Gohlke generously provides.

* 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.  Use your package
  management tool to look for packages like ``libxml2-dev`` or
  ``libxslt-devel`` if the build fails, and make sure they are
  installed.  Alternatively, setting ``STATIC_DEPS=true`` will
  download and build both libraries automatically.

* On **MacOS-X**, use the following to build the source distribution,
  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 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.


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>`_.


MacOS-X
-------

A `macport <http://macports.org/>`_ of lxml is available.  Try
something like ``port install py25-lxml``.

If you want to use a more recent lxml release, you may 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 them them out of the box, it is still
recommended to use a static build with the most recent 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>`_.
