5

Im trying to install lxml within a virtualenv with sudo pip install lxml and also sudo pip install --upgrade lxml but getting the following in both cases:

x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -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 /usr/bin/python -c "import setuptools, 
tokenize;__file__='/tmp/pip_build_root/lxml/setup.py';
exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), 
__file__, 'exec'))" install --record /tmp/pip-nmFOYf-record/install-record.txt 
--single-version-externally-managed --compile failed with error code 1 in 
/tmp/pip_build_root/lxml
Storing debug log for failure in /root/.pip/pip.log

I have tried all the posted solutions here, which implies that I have libxml2-dev, libxslt-dev and python-dev installed and I also installed build-essential
I'm currently running Linux Mint 17 Debian Based which uses apt-get as package manager.
python-lxml was already preinstalled.

Community
  • 1
  • 1
helado
  • 825
  • 2
  • 13
  • 26
  • 1
    Why are you using `sudo` within a virtualenv? Did you create the virtualenv as root? If so, why? Or are you trying to install it to the system site-packages even though you're inside a virtualenv? If so, why not do it from outside the virtualenv? – abarnert Oct 15 '14 at 00:03
  • @abarnert Well, the first time I did it without the sudo, then I tried installing lxml globally, but didn't work. – helado Oct 15 '14 at 00:11
  • Just randomly mixing virtualenv and global installs with and without sudo is just going to lead to a broken virtualenv with screwed up permissions. Don't do that. – abarnert Oct 15 '14 at 00:28
  • 1
    FYI you don't need to edit your title to say "solved". Just marking an accepted answer (which you've done) is sufficient. – nhinkle Oct 15 '14 at 00:46
  • 1
    Also, in the future, for installation-related problems, please put the information about your platform in your question, ideally as a tag. If you'd had the Linux Mint tag in the first place, I could have just told you to `sudo apt-get install zlib1g-dev` instead of trying to explain in the abstract that you need to use your platform's installer to install your platform's zlib development package. – abarnert Oct 15 '14 at 00:51
  • @abarnert Thanks for the advice, I will keep it in mind – helado Oct 15 '14 at 01:36

2 Answers2

7

lxml depends on various C libraries, and you have to have those C libraries installed—including their development files (headers, .so or .a libraries, etc.)—to build lxml. The installation docs explain what prerequisites you need to build on your particular platform.


This error:

/usr/bin/ld: cannot find -lz

… means that the prerequisite you're missing is libz, aka zlib.

Since you're not on Windows, it's incredibly unlikely that you actually don't have zlib at all… but pretty plausible that you don't have the development files for zlib. On some platforms, most notably many linux distros, packages are typically split into separate pieces. For example, the parts of zlib needed at runtime may be in a package named zlib, while the parts needed for building other programs that need zlib in a package named zlib-dev or zlib-devel. The exact details depend on your exact platform.

That being said, if you don't have the zlib development files, you probably don't have the libxml2 or libxslt development files either, because I don't know of any platform where installing those wouldn't have pulled in the zlib files as well.

At any rate, since you haven't told us what platform you're one (and distro, if linux), I don't know what package manager to use, what the packages are named, etc., but do whatever's appropriate for your platform.


Also:

I already have python-lxml installed

You really shouldn't install the same package both with your distro's package manager and with pip; that's a great way to confuse yourself.

But at any rate, most likely you installed python-lxml from a binary package, not from source, which means you didn't need the build requirements. Now you're trying to build it from source, which means you do.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thanks for your early answer, the reason why I tried installing both with pip and package manager (which is `apt-get`) is because the first one (pip) didn't work for me, so I tried installing `python-lxml` but it was __preinstalled__. Also I installed `libxml2` and `libxslt` before I tried to install `lxml`. – helado Oct 15 '14 at 00:20
  • @helado: But did you install `libxml2-dev` and `libxslt-dev`, as both the installation docs and this answer explain? – abarnert Oct 15 '14 at 00:29
  • If you mean by `sudo apt-get install libxml2-dev libxslt-dev python-dev` as the docs says, then yes, I did it like that. – helado Oct 15 '14 at 00:37
  • First time I've encountered this error on Debian... Thanks for the help! – ngoue Aug 17 '15 at 22:28
3

On an ubuntu based linux distro you can use:

sudo apt-get install zlib1g-dev

kguest
  • 3,804
  • 3
  • 29
  • 31
helado
  • 825
  • 2
  • 13
  • 26