How to actually install lxml on Ubuntu 12.04



sudo apt-get install libxml2-dev libxslt1-dev python-dev zlib1g-dev && pip install lxml

Ran in to a fun issue today! According to a stackoverflow issue, all you need to install lxml on ubuntu is

apt-get install libxml2-dev libxslt1-dev python-dev

But that wasn’t true for me. I also needed zlib1g-dev (or also mentioned lib32z1-dev) installed to fix the error I was getting.

If you ever see cannot find -lz then make sure to install one of the two packages mentioned.

The log

Here is a snipped version of the log of when I tried to install lxml without the require package. As usual it feels like no one ran in to this before.

(jeev-pt) root@jeev:/home/ubuntu/jeev# pip install lxml==3.3.6
Collecting lxml==3.3.6
/home/ubuntu/jeev-pt/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see
/home/ubuntu/jeev-pt/local/lib/python2.7/site-packages/pip/_vendor/requests/packages/urllib3/util/ InsecurePlatformWarning: A true SSLContext object is not available. This prevents urllib3 from configuring SSL appropriately and may cause certain SSL connections to fail. You can upgrade to a newer version of Python to solve this. For more information, see
  Downloading lxml-3.3.6.tar.gz (3.5MB)
    100% |████████████████████████████████| 3.5MB 383kB/s
Building wheels for collected packages: lxml
  Running bdist_wheel for lxml ... error
  Complete output from command /home/ubuntu/jeev-pt/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-8XijtG/lxml/';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" bdist_wheel -d /tmp/tmp4q7F2Jpip-wheel- --python-tag cp27:
  Building lxml version 3.3.6.
  Building without Cython.
  Using build configuration of libxslt 1.1.28
  /usr/lib/python2.7/distutils/ UserWarning: Unknown distribution option: 'bugtrack_url'
  ~~~ SNIP ~~~
    creating build/temp.linux-x86_64-2.7/src
    creating build/temp.linux-x86_64-2.7/src/lxml
    x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/libxml2 -I/tmp/pip-build-8XijtG/lxml/src/lxml/includes -I/usr/include/python2.7 -c src/lxml/lxml.etree.c -o build/temp.linux-x86_64-2.7/src/lxml/lxml.etree.o -w
    x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/src/lxml/lxml.etree.o -lxslt -lexslt -lxml2 -lz -lm -o build/lib.linux-x86_64-2.7/lxml/
    /usr/bin/ld: cannot find -lz
    collect2: error: ld returned 1 exit status
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

Command "/home/ubuntu/jeev-pt/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-8XijtG/lxml/';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-0r3kiN-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/ubuntu/jeev-pt/include/site/python2.7/lxml" failed with error code 1 in /tmp/pip-build-8XijtG/lxml/

Any issues or suggestions? Tell me on github.