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

浪尽此生 提交于 2019-11-27 06:19:28
Simon Willison

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

This worked for me (10.6.8):

sudo env ARCHFLAGS="-arch i386 -arch x86_64" easy_install lxml

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

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/

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
William Entriken

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

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  

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.

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.

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: akitada@macports.org openmaintainer@macports.org

lxml is included in the pypm repository:

$ pypm install lxml

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

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/

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!