Cannot install Lxml on Mac OS X 10.9

PythonXcodeMacosScrapyLxml

Python Problem Overview


I want to install Lxml so I can then install Scrapy.

When I updated my Mac today it wouldn't let me reinstall lxml, I get the following error:

In file included from src/lxml/lxml.etree.c:314:
/private/tmp/pip_build_root/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found
#include "libxml/xmlversion.h"
         ^
1 error generated.
error: command 'cc' failed with exit status 1

I have tried using brew to install libxml2 and libxslt, both installed fine but I still cannot install lxml.

Last time I was installing I needed to enable the developer tools on Xcode but since it's updated to Xcode 5 it doesn't give me that option anymore.

Does anyone know what I need to do?

Python Solutions


Solution 1 - Python

You should install or upgrade the commandline tool for Xcode. Try this in a terminal:

xcode-select --install

Solution 2 - Python

I solved this issue on Yosemite by both installing and linking libxml2 and libxslt through brew:

brew install libxml2
brew install libxslt
brew link libxml2 --force
brew link libxslt --force

If you have solved the problem using this method but it pops up again at a later time, you might need to run this before the four lines above:

brew unlink libxml2
brew unlink libxslt

If you are having permission errors with Homebrew, especially on El Capitan, this is a helpful document. In essence, regardless of OS X version, try running:

sudo chown -R $(whoami):admin /usr/local

Solution 3 - Python

You may solve your problem by running this on the commandline:

 STATIC_DEPS=true pip install lxml

It sure helped me. Explanations on docs

Solution 4 - Python

I tried most of the solutions above, but none of them worked for me. I'm running Yosemite 10.10, the only solution that worked for me was to type this in the terminal:

sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

EDIT: If you are using virtualenv, the sudo in beginning is not needed.

Solution 5 - Python

This has been bothering me as well for a while. I don't know the internals enough about python distutils etc, but the include path here is wrong. I made the following ugly hack to hold me over until the python lxml people can do the proper fix.

sudo ln -s  /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2/libxml/ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml

Solution 6 - Python

Installing globally... OS X 10.9.2

xcode-select --install
sudo easy_install pip
sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml

Solution 7 - Python

instalation instructions on http://lxml.de/installation.html explain:

> 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

Solution 8 - Python

None of the above worked for me on 10.9.2, as compilation bails out with following error:

clang: error: unknown argument: '-mno-fused-madd' 

Which actually lead to cleanest solution (see more details in [1]):

export CFLAGS=-Qunused-arguments
export CPPFLAGS=-Qunused-arguments

pip install lxml

or following if installing globally

sudo pip install lxml

[1] https://stackoverflow.com/questions/22313407/clang-error-unknown-argument-mno-fused-madd-python-package-installation-fa

Solution 9 - Python

xcode-select --install
sudo easy_install pip
sudo pip install lxml

Solution 10 - Python

I solved this issue on Yosemite by running the following commands:

xcode-select install #this may take several minutes.
pip install lxml

Solution 11 - Python

With homebrew, libxml2 is hidden to not interfere with the system libxml2, so pip must be helped a little in order to find it.

With bash:

LDFLAGS=-L`brew --prefix libxml2`/lib CPPFLAGS=-I`brew --prefix libxml2`/include/libxml2 pip install --user lxml

With fish:

env LDFLAGS=-L(brew --prefix libxml2)/lib CPPFLAGS=-I(brew --prefix libxml2)/include/libxml2 pip install --user lxml

Solution 12 - Python

OSX 10.9.2

sudo env ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future STATIC_DEPS=true pip install lxml

Solution 13 - Python

I tried all the answers on this page, none of them worked for me. I'm running OS X Version 10.9.2

But this definitely works....like a charm:

ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install lxml

Solution 14 - Python

Unfortunately xcode-select --install did not work for me as I already had the latest version.

It's very strange but I solved the issue by opening XCode and accepting the Terms & Conditions. Re-running pip install lxml returned no errors after.

Solution 15 - Python

After successful install from pip (lxml 3.6.4) I was getting an error when importing the lxml.etree module.

I was searching endlessly to install this as a requisite for scrapy, and tried all the options, but finally this worked for me (mac osx 10.11 python 2.7):

$ STATIC_DEPS=true sudo easy_install-2.7 "lxml==2.3.5"

The older version of lxml seem to work with etree module.

Pip can often ignore the specified version of a package, for example when you have the newer version in the pip cache, thus the easy_install. The '-2.7' option is for python version, omit this if you are installing for python 3.x.

Solution 16 - Python

In my case, I must shutdown Kaspersky Antivirus before installing lxml by:

pip install lxml

Solution 17 - Python

I am using OSX 10.9.2 and I get the same error.

Installation of the Xcode command line tools does not help for this particular version of OSX.

I think a better approach to fix this is to install with the following command:

$ CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2 pip install lxml

This is similar to jdkoftinoff' fix, but does not alter your system in a permanent way.

Solution 18 - Python

I met the same question and after days of working I resolved this problem on my OS X 10.9.4, with Python 3.4.1.

Here's my solution,

According to installing lxml from lxml.de,

> A macport of lxml is available. Try something like port install py25-lxml

If you do not have MacPort, install it from MacPort.org. It's quite easy. You may also need a compiler, to install Xcode compiling tools, use xcode-select --install

Firstly I updated my port to the latest version via sudo port selfupdate,

Then I just type sudo port install libxml2 and several minutes later you should see libxml2 installed successfully. Probably you may also need libxslt to install lxml. To install libxslt, use:sudo port install libxslt.

Now, just type pip install lxml, it should work fine.

Solution 19 - Python

before compiling add the path that to xmlversion.h into your environment.

$ set INCLUDE=$INCLUDE:/private/tmp/pip_build_root/lxml/src/lxml/

But make sure the path I've provided has the xmlversion.h file located inside. Then,

$ python setup.py install

Solution 20 - Python

pip did not work for me. I went to https://pypi.python.org/pypi/lxml/2.3 and downloaded the macosx .egg file:

https://pypi.python.org/packages/2.7/l/lxml/lxml-2.3-py2.7-macosx-10.6-intel.egg#md5=52322e4698d68800c6b6aedb0dbe5f34

Then used command line easy_install to install the .egg file.

Solution 21 - Python

Solution 22 - Python

After much tearing of the hair and gnashing of the teeth, I uninstalled Xcode with pip and ran:

easy_install lxml

And all was well.

Solution 23 - Python

Try:

% STATIC_DEPS=true pip install lxml

Or:

% STATIC_DEPS=true sudo pip install lxml

It works!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid O'ReganView Question on Stackoverflow
Solution 1 - PythontinyleeView Answer on Stackoverflow
Solution 2 - PythonMarkus Amalthea MagnusonView Answer on Stackoverflow
Solution 3 - PythonAndré AugustoView Answer on Stackoverflow
Solution 4 - PythonrobinsjView Answer on Stackoverflow
Solution 5 - PythonjdkoftinoffView Answer on Stackoverflow
Solution 6 - PythonSamy VilarView Answer on Stackoverflow
Solution 7 - PythonpanchicoreView Answer on Stackoverflow
Solution 8 - PythonokiganView Answer on Stackoverflow
Solution 9 - PythonRemembranceNNView Answer on Stackoverflow
Solution 10 - PythonholysView Answer on Stackoverflow
Solution 11 - PythonGaëtan LehmannView Answer on Stackoverflow
Solution 12 - PythonnpivView Answer on Stackoverflow
Solution 13 - PythonHexatonicView Answer on Stackoverflow
Solution 14 - PythonAdam SheridanView Answer on Stackoverflow
Solution 15 - PythonRyszard CetnarskiView Answer on Stackoverflow
Solution 16 - PythonThoView Answer on Stackoverflow
Solution 17 - PythonvaloView Answer on Stackoverflow
Solution 18 - PythonPiscesView Answer on Stackoverflow
Solution 19 - PythonknittledanView Answer on Stackoverflow
Solution 20 - PythonwisbuckyView Answer on Stackoverflow
Solution 21 - Pythonuser9869932View Answer on Stackoverflow
Solution 22 - PythonduhaimeView Answer on Stackoverflow
Solution 23 - PythonGeek4ITView Answer on Stackoverflow