how to install lxml python module on mac os 10.5 (leopard)

lxml is an xml library for python that doesn’t suck. It needs a recent libxml2 and libxslt. Mac OS X does not come with recent versions, and 10.5 breaks completely if you force it to try and use a recent version.

I used to use MacPorts for everything (which comes with py25-lxml) but ran into some issues with 10.5. So back to manual installs, it is.

  • assuming your python is the mac os x default…
  • make sure no traces of other pythons in your $PATH
  • download and install libxml2:
       ./configure --prefix=/usr/local/libxml2-2.7.0
       sudo make install
       cd /Library/Python/2.5/site-packages
       sudo ln -s /usr/local/libxml2-2.7.0/lib/python2.5/site-packages/* .
  • download and install libxslt:
       ./configure --prefix=/usr/local/libxslt-1.1.24 --with-libxml-prefix=/usr/local/libxml2-2.7.0 
       sudo make install
       cd /Library/Python/2.5/site-packages
       sudo ln -s /usr/local/libxslt-1.1.24/lib/python2.5/site-packages/* .
  • download and install lxml:
       sudo python install \
         --with-xml2-config=/usr/local/libxml2-2.7.0/bin/xml2-config \

Update March 1, 2009: in order to get libxml2 and libxslt python bindings working on 10.5.6, with a 2.6 version of python, I had to do quite a bit more fiddling. If the above doesn’t work for you, try this:

cd libxml2-2.7.0
./configure --prefix=/usr/local/libxml2-2.7.0
sudo make install
cd ../libxslt-1.1.24
./configure --prefix=/usr/local/libxslt-1.1.24 \
    --with-python \
sudo make install
cd ../libxml2-2.7.0/python

# libxml2 build doesn't support do -arch ppc
export ARCHFLAGS='-arch i386'

# supports libxslt install, sort-of
cp ../../libxslt-1.1.24/python/libxsl* .
cp ../../libxslt-1.1.24/python/
cp ../../libxslt-1.1.24/doc/libxslt-api.xml .

# hack the file to learn the dir structure
curl '' >
python build
sudo python install

And then install lxml normally.

(the use of autoreconf is explained by DarwinPorts folks, the use of ARCHFLAGS is explained by apple, and the hand-editing/hand-merging of libxslt and libxml python bindings for the build based on experimental fiddling.)