How do you install lxml on OS X Leopard without using MacPorts or Fink?

Question:

I’ve tried this and run in to problems a bunch of times in the past. Does anyone have a recipe for installing lxml on OS X without MacPorts or Fink that definitely works?

Preferably with complete 1-2-3 steps for downloading and building each of the dependencies.

Asked By: Simon Willison

||

Answers:

I compile it in /usr/local without any issues whatsoever.

Install Python, libxml2, libxslt and then lxml. You might need setuptools installed too.

Answered By: Svemirska pahuljica

Try installing Cython and installing from source, easy_install does fail. I haven’t tried on my mac yet though.

Failing that the ports version isn’t that ancient. You can see the dependencies, some of which had to be updated for my Linux build of lxml.

info py25-lxml
py25-lxml @2.1.5 (python, devel)

lxml is a Pythonic binding for the libxml2 and libxslt libraries. It is unique
in that it combines the speed and feature completeness of these libraries with
the simplicity of a native Python API, mostly compatible but superior to the
well-known ElementTree API.
Homepage: http://codespeak.net/lxml/

Library Dependencies: python25, libxml2, libxslt, py25-hashlib, py25-setuptools,
py25-zlib
Platforms: darwin
Maintainers: [email protected] [email protected]

Answered By: Nick Martin

Thanks to @jessenoller on Twitter I have an answer that fits my needs – you can compile lxml with static dependencies, hence avoiding messing with the libxml2 that ships with OS X. Here’s what worked for me:

cd /tmp
curl -O http://lxml.de/files/lxml-3.6.0.tgz
tar -xzvf lxml-3.6.0.tgz 
cd lxml-3.6.0
python setup.py build --static-deps --libxml2-version=2.7.3  --libxslt-version=1.1.24 
sudo python setup.py install
Answered By: Simon Willison

Easy_install can work using this:

STATIC_DEPS=true easy_install ‘lxml>=2.2beta4’

you may then need to run, depending on permissions;

STATIC_DEPS=true sudo easy_install ‘lxml>=2.2beta4’

see
http://muffinresearch.co.uk/archives/2009/03/05/install-lxml-on-osx/

Answered By: Jon

lxml is included in the pypm repository:

$ pypm install lxml
Answered By: Sridhar Ratnakumar

I’ve had excellent luck with Homebrew to install the libxml2 dependency:

brew install libxml2

Homebrew doesn’t seem to have libxslt available, but I’ve not yet had a need for XSLT. YMMV.

Once you have the dependency(s), then the usual methods work just fine:

pip install lxml

or

easy_install lxml
Answered By: David Eyk

This worked for me (10.6.8):

sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxml
Answered By: Andrei

I had this working fine with Snow Lepoard but after I upgraded to Lion I had to symlink gcc-4.2 to gcc. Running sudo env ARCHFLAGS=”-arch i386 -arch x86_64″ easy_install lxml was looking for gcc-4.2 instead of gcc.

Answered By: chuckles

To install with up to date versions of libxml2 and libxslt:

ARCHFLAGS="-arch i386 -arch x86_64" STATIC_DEPS=true pip install lxml

To install with specific versions of libraries:

ARCHFLAGS="-arch i386 -arch x86_64" STATIC_DEPS=true LIBXML2_VERSION=2.7.3 LIBXSLT_VERSION=1.1.24 pip install lxml

CentOS 64 bit (a bit off question, but hard won):

CFLAGS=-fPIC STATIC_DEPS=true pip install lxml

or

CFLAGS=-fPIC STATIC_DEPS=true LIBXML2_VERSION=2.7.3 LIBXSLT_VERSION=1.1.24 pip install lxml
Answered By: m.kocikowski

This worked for me on 10.8.5

  1. Install Xcode from Mac App Store
  2. Xcode -> Preferences -> Downloads -> Command Line Tools
  3. Install homebrew using
  4. ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"
  5. brew install libxml2
  6. sudo easy_install lxml

This comprises suggestions from:

But I wanted to compile it into one answer rather than leave comments everywhere

Answered By: William Entriken

On OS X 10.9.1 the suggested answer above errors out during install — following changes had to be made:

cd /tmp  
curl -o lxml-3.3.0.tgz http://lxml.de/files/lxml-3.3.0.tgz  
tar -xzvf lxml-3.3.0.tgz  
cd lxml-3.3.0  
python setup.py build --static-deps --libxml2-version=2.8.0  --libxslt-version=1.1.24  
sudo python setup.py install  
Answered By: user785278

A lot of pain went into this for an outdated 10.6.8 os x but here it goes for anyone running Snow Leopard!

First you have to install a different version of libxml2 from homebrew and install –with-python. You can do this by typing in the following commands.

brew update
brew edit libxml2

Then find the line that says “–without-python” and change to “–with-python”.

system "./configure", "--disable-dependency-tracking",
                      "--prefix=#{prefix}",
                      "--with-python"

Now you can install libxml2.

brew install libxml2

Next check your new install of libxml2 in the default homebrew location. You want to find the libxml2 config.

YOURS MAY BE DIFFERENT:

“/usr/local/Cellar/libxml2/VERSION_/bin/xml2-config”

Now use the following command to install lxml with pip using the newly installed libxml2 config and not the Mac OS X version.

ARCHFLAGS="-arch i386 -arch x86_64" pip install lxml --install-option="--with-xml2-config=/usr/local/Cellar/libxml2/2.9.1/bin/xml2-config"

Worked for me on my 10.6.8 Python 2.6. Thanks.

Credit goes to this page for showing me pip –install-option …

http://natanyellin.com/page/4/

Answered By: ganicus

I’m using OSX 10.11 El Capitan and Homebrew. Using pip install lxml would give me “fatal error: ‘libxml/xmlversion.h’ file not found” and “failed with error code 1” blah blah.

According to the official website, I should use STATIC_DEPS=true pip install lxml (add sudo before pip if you need that), and that solved my problem.

I ran brew install libxml2 and brew install libxslt to install the dependencies while troubleshooting. I’m not sure if those two commands are necessary.

Answered By: feilong

using homebrew (0.9.5) on el capitan (10.11.1) the following worked for me:

brew install libxml2
LD_FLAGS=-L/usr/local/opt/libxml2/lib CPPFLAGS=-I/usr/local/opt/libxml2/include/libxml2 pip install lxml
Answered By: mizerlou