13

I am having trouble installing lxml to my Mac OS. I am having the following error when building it. This is the error I have when using pip install lxml

/private/var/folders/9s/s5hl5w4x7zjdjkdljw9cnsrm0000gn/T/pip-build-khuevu/lxml/src/lxml/includes/etree_defs.h:9:10: fatal error: 'libxml/xmlversion.h' file not found

I have installed libxml2 with brew:

brew install libxml2
brew link libxml2 --force

I'm new to Mac. In Ubuntu, it would mean libxml2-dev package must be installed.

Updated: here is the pip.log:

"~/.pip/pip.log" 124L, 8293C requirement_set.install(install_options, global_options, root=options.root_path) File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg/pip/req.py", line 1185, in install requirement.install(install_options, global_options, *args, **kwargs) File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg/pip/req.py", line 592, in install cwd=self.source_dir, filter_stdout=self._filter_install, show_stdout=False) File "/usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pip-1.3.1-py2.7.egg/pip/util.py", line 662, in call_subprocess % (command_desc, proc.returncode, cwd)) InstallationError: Command /usr/local/Cellar/python/2.7.5/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python -c "import setuptools;file='/private/var/folders/9s/s5hl5w4x7zjdjkdljw9cnsrm0000gn/T/pip-build-khuevu/lxml/setup.py';exec(compile(open(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record /var/folders/9s/s5hl5w4x7zjdjkdljw9cnsrm0000gn/T/pip-nsV0iT-record/install-record.txt --single-version-externally-managed failed with error code 1 in /private/var/folders/9s/s5hl5w4x7zjdjkdljw9cnsrm0000gn/T/pip-build-khuevu/lxml

Any idea ? Thanks a lot

Khue Vu
  • 3,112
  • 4
  • 37
  • 40
  • 1
    Is there an error message in `~/.pip/pip.log`? After installing `libxml2` with `brew`, do you see the correct header files in `/usr/include/libxml2/libxml`? Are the `libxml2` headers definitely on your path? – ChrisP Jul 25 '13 at 12:42
  • @ChrisP, I updated my question with the log. There is no /usr/include folder in my machine. – Khue Vu Jul 25 '13 at 13:29
  • Hopefully someone comes along who knows what's up. My only recommendation right now is to make sure the libxml2 headers are accessible via PATH. Homebrew should install to `/usr/local`, which is on PATH by default. Can you find the `xmlversion.h` file on your machine, just to make sure it's there? If you keep having trouble, I recommend the [Enthought](http://www.enthought.com) Python distribution, which comes with many hard to build tools (including `lxml`) by default. I know that doesn't solve your exact problem. – ChrisP Jul 25 '13 at 13:37
  • Thanks @ChrisP. It is true that the file is not in the path for compilation even though I have a version of libxml installed by Brew, and a version installed with Xcode. Both are in PATH. I will update the answer below. – Khue Vu Jul 25 '13 at 17:33

6 Answers6

22

Turn out xmlversion.h is not included in compilation path even though it is in PATH. Modify the C_INCLUDE_PATH env fix the error for me:

C_INCLUDE_PATH=/usr/local/Cellar/libxml2/2.9.1/include/libxml2:$C_INCLUDE_PATH

Khue Vu
  • 3,112
  • 4
  • 37
  • 40
  • 3
    Just a tip for anyone that stumbles upon this: I had the same issue and this was the fix. ```export C_INCLUDE_PATH=/usr/local/Cellar/libxml2/2.9.1/include/libxml2:$C_INCLUDE_PATH``` – zgohr Aug 16 '13 at 15:14
  • This got lxml compiling for me on 10.9 and a self-installed version of libxml2. Thanks! – synthesizerpatel Oct 26 '13 at 16:50
  • If you're using macports: export C_INCLUDE_PATH=/usr/local/include/libxml2:$C_INCLUDE_PATH – Petri Nov 05 '13 at 06:16
  • 1
    On OS X 10.10.2 this worked (note different libxml2 version): export C_INCLUDE_PATH=/usr/local/Cellar/libxml2/2.9.2/include/libxml2:$C_INCLUDE_PATH – J Starr Mar 10 '15 at 21:22
  • The different version is just because Homebrew has updated the installed package; other than that you're doing exactly the same thing and for me as well this does not work under 10.10.2. – Bryson Mar 23 '15 at 21:30
21

If you are running Mavericks with Xcode installed, you can also use:

export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include/libxml2:$C_INCLUDE_PATH
azymux
  • 311
  • 2
  • 3
  • 1
    With Xcode 6, this command is now: export C_INCLUDE_PATH=/Applications/Xcode.app//Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/usr/include/libxml2:$C_INCLUDE_PATH – Markus Sep 22 '14 at 14:10
  • 5
    For OSX Yosemite it is now ``export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2:$C_INCLUDE_PATH`` – Filipe Correia Oct 31 '14 at 14:26
  • 1
    For MacOS Catalina it is now `export C_INCLUDE_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/usr/include/libxml2:$C_INCLUDE_PATH` – Ashley Kleynhans Nov 27 '19 at 09:32
  • 1
    @AshleyKleynhans thank you, that worked for me to get the python ovirt sdk installed. Much appreciated. – Matthew Jan 10 '20 at 16:13
6

I have Python and libxml installed via brew, this worked for me after trying everything above:

sudo mkdir /usr/include # If /usr/include is missing
sudo ln -s /usr/local/Cellar/libxml2/2.9.2/include/libxml2 /usr/include/libxml2

Note: You'll need to check your own paths for latest versions.

Since the headers are available elsewhere, and being looked for in a particular directory (spotted in the command calling clang to compile) - and for some reason the alternate place is not being picked up, I made a softlink to the right place.

Hiway
  • 440
  • 5
  • 10
5

None of the other answers (exporting C_INCLUDE_PATH) worked for me. However, setting the CFLAGS environment variable to the built-in OS X 10.10 libxml2 root worked like a charm.

export CFLAGS=-I/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/libxml2
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
  • 1
    This is the one which worked for me on 10.10.2 with Fish shell. I first opened a `bash` shell, then did the `export` listed here, then re-ran `pip install lxml` and the install finally completed successfully. Thanks Dan! – Nick K9 Feb 15 '15 at 13:53
  • Same here, this is the only one that works on 10.10.2 – Chris Koston Feb 27 '15 at 00:08
  • In Yosemite 10.10.2, I had already installed the whole Xcode package, and nothing additional was required for me to get lxml working. I created a virtual env using virtualenvwrapper, and `pip3.4 install lxml` installed error free. I tested out the lxml install by scraping google's home page, as well as searching for an xpath in an xml document, and everything seems to work fine. – 7stud Apr 11 '15 at 01:56
4
brew install libxml2
brew link libxml2 --force
export C_INCLUDE_PATH=/usr/local/Cellar/libxml2/2.9.2/include/libxml2:$C_INCLUDE_PATH
lxg
  • 12,375
  • 12
  • 51
  • 73
guest
  • 41
  • 1
1

there is something with the permission to mkdir in /usr/ when the system is Mac EI captain. Maybe we can not modify this directory?

See What is the “rootless” feature in El Capitan, really? on AskDifferent

Community
  • 1
  • 1
yxl
  • 23
  • 3