Cannot install Lxml on Mac OS X 10.9

95,998

Solution 1

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

xcode-select --install

Solution 2

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

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

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

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
Share:
95,998

Related videos on Youtube

David O'Regan
Author by

David O'Regan

Started with visual basic and have been chasing the high ever since. Im David, an Irish software developer from Ireland. I explain with words and code. Former head of frontend at VSWare LTD. Passionate about JavaScript, React, Functional Programming and all things maths.

Updated on July 17, 2022

Comments

  • David O'Regan
    David O'Regan almost 2 years

    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?

    • Phil L.
      Phil L. almost 10 years
      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 ;-)
    • Parker
      Parker over 9 years
      Be sure to mark the correct answer as the solution so others can use your question as guidance when they have the same problem :)
  • kelorek
    kelorek over 10 years
    I get "Can't install the software because it is not currently available from the Software Update server."
  • wombat
    wombat about 10 years
    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.
  • frankV
    frankV about 10 years
    thanks. this helped me. wish answers could sometimes contain meta about the os versions.
  • swandog
    swandog about 10 years
    If installing globally you may need to /etc/sudoers with sudo visudo to preserve the environment variables.
  • erickthered
    erickthered almost 10 years
    This worked for me, had to update xcode dev tools first using: xcode-select --install
  • Steven Mercatante
    Steven Mercatante almost 10 years
    This did the trick. I already had xcode command line tools installed, but looks like it needed to be updated.
  • João Pereira
    João Pereira almost 10 years
    Worked flawless for me. +1
  • studgeek
    studgeek almost 10 years
    A few notes on how to manually check if they are installed - stackoverflow.com/a/20834890/255961
  • Michiel Kauw-A-Tjoe
    Michiel Kauw-A-Tjoe over 9 years
    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-erro‌​r-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‌​"
  • Marcos Eliziário Santos
    Marcos Eliziário Santos over 9 years
    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.
  • Mauro Baraldi
    Mauro Baraldi over 9 years
    This solution works on Mavericks also! If you are using virtualenv no sudo is needed.
  • lukecampbell
    lukecampbell over 9 years
    Thank you so much. This saved me an incredibly amount of time.
  • Noach Magedman
    Noach Magedman over 9 years
    @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
    Noach Magedman over 9 years
    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.
  • Kar
    Kar about 9 years
    Duplicate of Andre's answer.
  • Rafal Es
    Rafal Es about 9 years
    @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
    Rafal Es about 9 years
    @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).
  • 7stud
    7stud about 9 years
    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.
  • Eric Chen
    Eric Chen about 9 years
    Works fine on 10.10.3. Not sure why others need to manipulate cflags.
  • sudo
    sudo almost 9 years
    Also, Homebrew always ruins my permissions. I used the Xcode solution.
  • mgrandi
    mgrandi almost 9 years
    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.
  • PLPeeters
    PLPeeters over 8 years
    Mine is still outputting OSError: [Errno 1] Operation not permitted: '/usr/bin/build' despite running the command as sudo. Any ideas?
  • robinsj
    robinsj over 8 years
    @PLPeeters Which version of python are you running and which OS version?
  • PLPeeters
    PLPeeters over 8 years
    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...
  • Łukasz
    Łukasz over 8 years
    And worked for me after upgrade to El Capitan (10.11).
  • nils
    nils over 8 years
    This worked perfectly on Yosemite in a virtualenv. I didn't need sudo though.
  • jacoor
    jacoor over 8 years
    You are a life saver. El capitan works, in virtualenv no sudo needed. Thanks!
  • cbmanica
    cbmanica over 8 years
    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.
  • Markus Amalthea Magnuson
    Markus Amalthea Magnuson over 8 years
    I edited the answer to add a link on solving Homebrew permission errors on, especially on El Capitan.
  • sami
    sami over 8 years
    on El Capitan, the following worked for me: sudo CPATH=/Applications/Xcode.app/Contents/Developer/Platforms/M‌​acOSX.platform/Devel‌​oper/SDKs/MacOSX10.1‌​1.sdk/usr/include/li‌​bxml2 CFLAGS=-Qunused-arguments CPPFLAGS=-Qunused-arguments pip install lxml
  • mahmoudajawad
    mahmoudajawad about 8 years
    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!
  • karantan
    karantan almost 8 years
    works on El Capitan. I'm using virtualenv. (sudo is probably not needed if on virtualenv)
  • Esteban
    Esteban almost 8 years
    @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.
  • user9869932
    user9869932 over 7 years
    Works fine on OSX 10.9.5
  • chenware
    chenware over 7 years
    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.
  • Bad Request
    Bad Request over 7 years
    This helped me install Nokogiri in Sierra as well.
  • Tim Strijdhorst
    Tim Strijdhorst about 6 years
    This seems dangerous but it's the only solution that actually worked. #livinontheedge
  • DexterT.
    DexterT. almost 6 years
    This worked on Sierra + XCode 9, where the xcode upgrade from 8.x to 9 ruined things
  • jolvera
    jolvera over 5 years
    Thanks! Works fine on macOS 10.14