/usr/bin/ld: cannot find -lz

I'm trying to install lxml on a virtual machine running ubuntu/trusty64 in a virtualenv however i get this error below and can't get my head around it.

/usr/bin/ld: cannot find -lz

the first time this error occurred i looked into installing lz. i installed it using sudo apt-get install mtools which contains lz however the error still occurs therefore i'm thinking this could be a symbolic link problem maybe a file is supposed to be linked to lz but not sure what file. lz has a symlink to uz but i dont know what effect that has and don't want to change that incase it breaks something else. any ideas to point me in the right direction?

building 'lxml.etree' extension

creating build/temp.linux-x86_64-2.7

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 -Isrc/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/etree.so

/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

----------------------------------------
Cleaning up...
Command /home/vagrant/.virtualenvs/devenv/bin/python -c "import setuptools, tokenize;__file__='/home/vagrant/.virtualenvs/devenv/build/lxml/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-yUqdLy-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/vagrant/.virtualenvs/devenv/include/site/python2.7 failed with error code 1 in /home/vagrant/.virtualenvs/devenv/build/lxml
Storing debug log for failure in /home/vagrant/.pip/pip.log

(devenv)vagrant@vagrant-ubuntu-trusty-64:/vagrant/woowoo/woowoo_webapp$ ls -l /usr/bin/ld
lrwxrwxrwx 1 root root 6 Sep 16 16:11 /usr/bin/ld -> ld.bfd
(devenv)$ vim /usr/bin/ld.bfd 
(devenv)$ ls -l /usr/bin/ld.bfd
-rwxr-xr-x 1 root root 1050912 Sep 16 16:10 /usr/bin/ld.bfd
(devenv)$ ls -l /usr/bin/ld
lrwxrwxrwx 1 root root 6 Sep 16 16:11 /usr/bin/ld -> ld.bfd
(devenv)$ ls -l /usr/bin/lzlrwxrwxrwx 1 root root 2 Oct 10  2013 /usr/bin/lz -> uz
(devenv)$ ls -l /usr/bin/uz
-rwxr-xr-x 1 root root 2496 Oct 10  2013 /usr/bin/uz

Solution 1:

You need to install the build dependencies of lxml in order to compile it. Try doing sudo apt-get build-dep python-lxml if you have source package data enabled in your apt sources.

What is missing is not the lz command, but the development files for the zlib library. You need to install zlib1g-dev to get this.