245

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?

Null
  • 1,950
  • 9
  • 30
  • 33
David O'Regan
  • 2,684
  • 2
  • 13
  • 12
  • 3
    PS: I recommend using: `pip3.4 install lxml` or `STATIC_DEPS=true pip3.4 install lxml` commands if you're targeting specific installation for Python3 (adapt to your version, bash TAB completion is your friend); so that you install for the specific version of your target program that is using this/these libs. Otherwise it's easy to spend 30mn installing the lib for the wrong interpreter ;-) – Phil L. Aug 05 '14 at 13:42
  • 3
    Be sure to mark the correct answer as the solution so others can use your question as guidance when they have the same problem :) – Parker Oct 19 '14 at 06:59

24 Answers24

512

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

xcode-select --install

If Xcode Command Line Tools are already installed, but you're still not able to install lxml, then reset xcode-select by following the command:

sudo xcode-select --reset

This will reset the path to the Xcode Command Line Tools directory and may resolve the issue.

After you've installed or updated Xcode Command Line Tools, Hopefully, this will resolve the issue and allow you to install the "lxml" package.

Ramesh Patel
  • 108
  • 2
  • 7
tinylee
  • 5,184
  • 1
  • 13
  • 6
  • 25
    I get "Can't install the software because it is not currently available from the Software Update server." – kelorek Dec 17 '13 at 15:18
  • 7
    It's worth noting that you seem to get "Can't install the software because it is not currently available from the Software Update server." when the tools are already installed. – wombat Apr 27 '14 at 10:18
  • A few notes on how to manually check if they are installed - http://stackoverflow.com/a/20834890/255961 – studgeek Jul 28 '14 at 04:34
  • 1
    Works fine on 10.10.3. Not sure why others need to manipulate cflags. – Eric Chen May 09 '15 at 17:53
  • 1
    And worked for me after upgrade to El Capitan (10.11). – Łukasz Oct 01 '15 at 13:34
  • Had a problem installing the latest version of nokogiri and looking at the logs I found an error that looked like the above. This solution worked for me. – chenware Oct 31 '16 at 22:10
  • This helped me install Nokogiri in Sierra as well. – Bad Request Jan 02 '17 at 05:20
199

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
Markus Amalthea Magnuson
  • 8,415
  • 4
  • 41
  • 49
  • 25
    The most useful answer. It's incredible how the xcode-select get so many votes. If you are using brew, you already did code-select or install xcode a long time ago. – Marcos Eliziário Santos Nov 09 '14 at 20:44
  • 5
    @MarcosEliziárioSantos The word "install" in `xcode-select --install` is a bit misleading. I found that after upgrading from Mavericks to Yosemite, I needed to run `xcode-select --install` again to upgrade to Yosemite's Command Line Tools. It (partially) solved problems I was running into, and in any case would be harmless to run redundantly. – Noach Magedman Feb 09 '15 at 10:27
  • 2
    @MarcosEliziárioSantos probably because top answer fixed the problem for most people. This solution worked as a workaround, but I ran into similar problems with libmemcache few seconds later. Top answer fixed the problem. Everyone should consider this method only after trying the top answer first. – Rafal Es Apr 04 '15 at 21:25
  • @MarcosEliziárioSantos one more thing - I am using brew but it was a fresh install (I had only XCode installed via AppStore, which is enough to install homebrew). – Rafal Es Apr 04 '15 at 21:28
  • Also, Homebrew always ruins my permissions. I used the Xcode solution. – sudo Jun 20 '15 at 18:21
  • Brew is not the right answer for everyone - did not work for me, in any case, due to permissions on /usr/local that I don't control. Thanks to the wonder of Apple, just because you installed xcode awhile ago doesn't mean you don't need to do it again. – cbmanica Dec 21 '15 at 19:55
  • I edited the answer to add a link on solving Homebrew permission errors on, especially on El Capitan. – Markus Amalthea Magnuson Dec 22 '15 at 11:03
  • It seems that Yosemite and El Capitan are having versions of libxml2 installed under the hood that Homebrew is trying to avoid clashing with by not setting up the symlinks by default. Your "link --force" did the trick, thanks! – mahmoudajawad Feb 22 '16 at 04:27
  • This seems dangerous but it's the only solution that actually worked. #livinontheedge – Tim Strijdhorst Mar 15 '18 at 10:49
  • This worked on Sierra + XCode 9, where the xcode upgrade from 8.x to 9 ruined things – DexterT. Jul 28 '18 at 00:44
108

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

 STATIC_DEPS=true pip install lxml

It sure helped me. Explanations on docs

John Mee
  • 50,179
  • 34
  • 152
  • 186
André Augusto
  • 1,345
  • 1
  • 7
  • 10
50

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.

robinsj
  • 597
  • 5
  • 5
  • 1
    This solution works on Mavericks also! If you are using `virtualenv` no sudo is needed. – Mauro Baraldi Jan 06 '15 at 02:02
  • Mine is still outputting `OSError: [Errno 1] Operation not permitted: '/usr/bin/build'` despite running the command as sudo. Any ideas? – PLPeeters Sep 14 '15 at 08:38
  • @PLPeeters Which version of python are you running and which OS version? – robinsj Sep 14 '15 at 08:41
  • I'm on the El Capitan beta (so that's 10.11) and using Python 2.7 for this. I'm guessing it might have something to do with El Capitan... – PLPeeters Sep 14 '15 at 11:48
  • You are a life saver. El capitan works, in virtualenv no sudo needed. Thanks! – jacoor Nov 06 '15 at 09:08
  • on El Capitan, the following worked for me: sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/libxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml – sami Jan 26 '16 at 13:50
  • works on El Capitan. I'm using virtualenv. (sudo is probably not needed if on virtualenv) – karantan May 25 '16 at 07:20
  • This one saved the day – John Fantastico Jan 05 '23 at 18:43
24

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
jdkoftinoff
  • 2,391
  • 1
  • 17
  • 17
19

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
Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
13

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
panchicore
  • 11,451
  • 12
  • 74
  • 100
11

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] clang error: unknown argument: '-mno-fused-madd' (python package installation failure)

Community
  • 1
  • 1
okigan
  • 1,559
  • 2
  • 18
  • 33
  • thanks. this helped me. wish answers could sometimes contain meta about the os versions. – frankV May 03 '14 at 06:11
  • If installing globally you may need to `/etc/sudoers` with `sudo visudo` to preserve the environment variables. – swandog May 18 '14 at 04:07
10
xcode-select --install
sudo easy_install pip
sudo pip install lxml
RemembranceNN
  • 280
  • 3
  • 8
  • It's interesting that some people needed to `sudo` the pip install while others didn't. (I had permission problems when running pip install directly, and had to sudo.) I can only assume that I had installed something python-related as root in the distant past, which created certain (system) folders as root instead of as myself. – Noach Magedman Feb 09 '15 at 10:35
  • This worked for me, I had just set up a new mac for work and even though i installed XCode, i guess it didn't install the command line tools? Anyway, running this, having it install the tools and then just rerunning 'pip3.4 install lxml --user' worked, no sudo or EXPORTING any shell variables. – mgrandi Jul 08 '15 at 22:51
  • This worked perfectly on Yosemite in a virtualenv. I didn't need `sudo` though. – nils Oct 20 '15 at 07:52
  • @NoachMagedman I'm installing into a virtual environment, so I cannot use sudo. If I used sudo, it would install it into my global python packages, which is what i'm trying to avoid. – Esteban Aug 18 '16 at 22:29
7

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

xcode-select install #this may take several minutes.
pip install lxml
holys
  • 13,869
  • 15
  • 45
  • 50
  • I had enough of homebrew creating a tangled mess of things on my old system, so I vowed not to use it with my new Yosemite 10.10.2 setup. I already installed the whole Xcode package, so no need for `xcode-select install` for me. I downloaded the latest libxml2 and libxlst packages (required for lxml), but before installing them I thought I'd check if Yosemite already has those files some where. I created a virtual env(using virtualenvwrapper), and then I did `pip install lxml`, and lxml installed error free. I tested lxml by scraping google's home page, and that worked fine. – 7stud Apr 10 '15 at 23:37
4

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
Gaëtan Lehmann
  • 868
  • 1
  • 10
  • 11
3

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

Hexatonic
  • 2,251
  • 2
  • 21
  • 26
  • 2
    This merely disables hard errors for unused arguments, which I've encountered during many other library compilations on OS X Mavericks. I've found it useful to add the line to my bash profile, so I don't have to look it up every time I encounter it: `export ARCHFLAGS="-Wno-error=unused-command-line-argument-hard-error-in-future"` or you can add architecture types explicitly like this: `export ARCHFLAGS="-arch x86_64 -arch i386 -Wno-error=unused-command-line-argument-hard-error-in-future"` – Michiel Kauw-A-Tjoe Aug 27 '14 at 12:42
2

OSX 10.9.2

sudo env ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future STATIC_DEPS=true pip install lxml
npiv
  • 1,837
  • 15
  • 24
2

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.

Adam
  • 121
  • 1
  • 2
  • Do you have XCode installed as an app itself? I have the same issue: `xcode-select --install` fails because I have it installed already and `libxml2` is actually located under path returned by `xcode-select -p`. But I do not have XCode app installed (not found in the list of apps). Thus, I do not quite understand where may I accept the terms too :) – Anton Bryzgalov Feb 17 '23 at 12:29
  • With MacOS Ventura, this is the one that is required. No other answers solved the issue for me. – ZephyRr Mahesh Apr 12 '23 at 17:19
1

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.

Ryszard Cetnarski
  • 1,952
  • 19
  • 20
1

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

pip install lxml
Tho
  • 23,158
  • 6
  • 60
  • 47
0

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.

shim
  • 9,289
  • 12
  • 69
  • 108
valo
  • 1,712
  • 17
  • 12
0

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.

shim
  • 9,289
  • 12
  • 69
  • 108
Pisces
  • 26
  • 3
0

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
furins
  • 4,979
  • 1
  • 39
  • 57
knittledan
  • 754
  • 2
  • 9
  • 23
0

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.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
0

This post links to a solution that worked for me Python3, lxml and "Symbol not found: _lzma_auto_decoder" on Mac OS X 10.9

hth

Community
  • 1
  • 1
user9869932
  • 6,571
  • 3
  • 55
  • 49
0

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.

shim
  • 9,289
  • 12
  • 69
  • 108
duhaime
  • 25,611
  • 17
  • 169
  • 224
0

I have solved it by following problem

pip install --upgrade --force-reinstall --use-pep517 lxml --install-option="--static-deps=true" --install-option="--libxml2-version=2.11.4"
cane
  • 892
  • 1
  • 10
  • 16
-1

Try:

% STATIC_DEPS=true pip install lxml

Or:

% STATIC_DEPS=true sudo pip install lxml

It works!

John Mee
  • 50,179
  • 34
  • 152
  • 186
Geek4IT
  • 592
  • 5
  • 13