views:

2131

answers:

8

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.

A: 

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

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

That works on OS X? What prefix commands etc do you use? How do you ensure the libxml2 you are installing doesn't conflict with the one that ships with OS X?
Simon Willison
A: 

Have you tried with easy_install?

I don't own a Mac so I can't test it, but easy_install is almost considered the standard way to install python modules, so if you don't know it you should have a look at it.

dalloliogm
lxml has too many dependencies for a straight easy_install to work.
Simon Willison
Here are the errors you get if you attempt an easy_install: http://gist.github.com/167770
Simon Willison
+1  A: 

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]

Nick Martin
Check out the post from Ross, you need the header files for libxml2 and libxslt. Think that's why GCC was barfing when I tried to compile from the svn trunk.
Nick Martin
A: 

This is quite up to date - march 2009: http://lsimons.wordpress.com/2008/08/31/how-to-install-lxml-python-module-on-mac-os-105-leopard/

Ross
It looks like those instructions clobber OS X's built-in libxml2, which I'd rather not do.
Simon Willison
The Apple one is in /usr/lib the one on the blog is in /usr/local *The parameter to the configure steps)
Mark
+16  A: 

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
wget http://codespeak.net/lxml/lxml-2.2.2.tgz
tar -xzvf lxml-2.2.2.tgz 
cd lxml-2.2.2
cd libs/
wget ftp://xmlsoft.org/libxml2/libxml2-2.7.3.tar.gz
wget ftp://xmlsoft.org/libxml2/libxslt-1.1.24.tar.gz
cd ..
python setup.py build --static-deps --libxml2-version=2.7.3  --libxslt-version=1.1.24 
sudo python setup.py install
Simon Willison
The 'cd libs/' step fails for me, but it along with the following two wgets seem to be unnecessary as they happen as a side-effect of the 'setup.py build...' step anyway.
fuzzyman
Hmm... it's possible that libs/ directory was created for me the first time I ran "python setup.py build --static-deps" and it failed (because my firewall didn't allow FTP) - I actually pulled the .tar.gz files down via an intermediary server to work around that problem.
Simon Willison
AFAIK, wget doesn't come stock w/ OS X. Use `curl -O` instead.
David Eyk
This seems to be failing on Snow Leopard with the error: from SAX.c:15:/Developer/SDKs/MacOSX10.4u.sdk/usr/include/stdarg.h:4:25: error: stdarg.h: No such file or directory
alxp
Ahh found the Snow Leopard solution here: http://blog.coredumped.org/2009/09/snow-leopard-and-lxml.html - must temporarily change /usr/bin/gcc to point to /usr/bin/gcc-4.0
alxp
As of 04/06/2010, this solution fails when using the most recent versions of all 3 (lxml 2.2.6, libxml 2.7.7, libxslt 1.1.26). I get an error that starts with: Undefined symbols for architecture i386: "_gzdirect", referenced from: ___xmlParserInputBufferCreateFilename in libxml2.a(xmlIO.o)
andyashton
+1  A: 

This worked for me in the past install lxml on osx

muffinresearch
+2  A: 

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/

A: 

lxml is included in the pypm repository:

$ pypm install lxml
Sridhar Ratnakumar