211

I am trying to compile Android source code under Ubuntu 10.04. I get an error saying,

/usr/bin/ld: cannot find -lz

Can you please tell me how can I fix it? What does cannot find -lz mean? Here's the full error message:

external/qemu/Makefile.android:1101: warning: overriding commands for target `external/qemu/android/avd/hw-config-defs.h'
external/qemu/Makefile.android:933: warning: ignoring old commands for target `external/qemu/android/avd/hw-config-defs.h'
host SharedLib: libneo_cgi (out/host/linux-x86/obj/lib/libneo_cgi.so)
/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../libz.so when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/gcc/i486-linux-gnu/4.4.3/../../../libz.a when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/libz.so when searching for -lz
/usr/bin/ld: skipping incompatible /usr/lib/libz.a when searching for -lz
/usr/bin/ld: cannot find -lz
collect2: ld returned 1 exit status
make: *** [out/host/linux-x86/obj/lib/libneo_cgi.so] Error 1

And my GCC version output:

scheung@scheung-virtual-box:/media/EXTDIV/mydroid$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

I already have the zlib1g-dev library installed:

$ sudo apt-get install zlib1g-dev 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
zlib1g-dev is already the newest version.

(I get that from this link.)

0

11 Answers 11

185

I had the exact same error, and like you, installing zlib1g-dev did not fix it. Installing lib32z1-dev got me past it. I have a 64 bit system and it seems like it wanted the 32 bit library.

2
  • 28
    It worked here with libz-dev (virtual package for zlib1g-dev) on Kubuntu 12.04 x64.
    – Qsiris
    Jan 22, 2013 at 16:06
  • 10
    Also helped me install/build lxml-3.4.0 for Python via pip on Ubuntu 14.04 LTS/trusty. Thanks!
    – Marian
    Sep 15, 2014 at 8:07
175

For x64 install zlib1g-dev.

sudo apt-get install zlib1g-dev

I don't need all the x86 libs ;)

8
  • 11
    can confirm this worked for install lxml via pip on ubuntu 14.04
    – mwjackson
    Mar 19, 2015 at 11:04
  • 2
    same for me as for mwjackson, although I got zlib1g-dev via the metapackage libz-dev, as was suggested in another answer.
    – mknaf
    Mar 25, 2015 at 22:15
  • 2
    If this doesn't work, try the static version zlib-static.
    – DearVolt
    Jun 27, 2017 at 13:53
  • 3
    Confirmed on amd64 Ubuntu 18.04 LTS, Bionic Beaver
    – deinerson1
    Jun 13, 2018 at 22:56
  • 1
    Works on Ubuntu 20.04 Aug 8, 2022 at 10:41
60

sudo apt-get install libz-dev in ubuntu.

12
  • 2
    Could somebody shed some light into why is this necessary? I was able to install python-lxml in a virtual environment ONLY after installing this package. However if I tried to install it globally, I wouldn't have any problem at all. :\
    – cballenar
    Jul 22, 2016 at 15:02
  • Not certain about that case specifically, but broadly python-lxml requires logic contained in the zlib library which it cannot or simply doesn't provide itself. If it cannot reach the library, either because it's missing or some permissions barrier, it will be unable to build/function.
    – ti7
    Feb 7, 2020 at 22:00
  • 1
    @cballenar : Short answer: Dependencies! python-lxml depends on any package that Provides: libz-dev. C/C++ programs are compiled and linked against libraries which provide functions, routines, & data structures. Unix systems use .so, .a, .la files & Windows use .dll. These are compiled shared library files, that contain routines that other programs can link against and use to implement some functionality. For this example: libz implements data compression & decompression functions.
    – TrinitronX
    Jul 5, 2020 at 0:08
  • @cballenar : Long answer / details (part 1): libz-dev is a virtual package on Ubuntu (verify this on your system with: apt show libz-dev). The other package zlib1g-dev provides libz-dev, which includes the shared library file /usr/lib/x86_64-linux-gnu/libz.a, and the C Header files /usr/include/zconf.h & /usr/include/zlib.h. You can view the package contents with: dpkg -L zlib1g-dev, which shows all the files it contains.
    – TrinitronX
    Jul 5, 2020 at 0:15
  • @cballenar : Long answer (part 2): As mentioned, libz-dev (also in Debian) is provided by zlib1g-dev via the Debian Package Provides: libz-dev line in the Debian source package's debian/control file. This is how the apt packaging manager knows to resolve the python-lxml compile-time virtualenv package dependency on libz-dev via zlib1g-dev.
    – TrinitronX
    Jul 5, 2020 at 0:30
35

I just encountered this problem and contrary to the accepted solution of "your make files are broken" and "host includes should never be included in a cross compile"

The android build includes many host executables used by the SDK to build an android app. In my case the make stopped while building zipalign, which is used to optimize an apk before installing on an android device.

Installing lib32z1-dev solved my problem, under Ubuntu you can install it with the following command:

sudo apt-get install lib32z1-dev
0
16

I had the exact same error, Installing zlib-devel solved my problem, Type the command and install zlib package.

On linux:

sudo apt-get install zlib*

On Centos:

sudo yum install zlib*
1
  • 1
    sudo yum install zlib* solved my problem on amazon os
    – flerka
    Jun 24, 2022 at 19:46
8

Try one of those three solution. It must work :) :

  1. sudo apt-get install zlib1g-dev
  2. sudo apt-get install libz-dev
  3. sudo apt-get install lib32z1-dev

In fact what is missing is not the lz command, but the development files for the zlib library.So you should install zlib1g-devlib for ex to get it.

For rhel7 like systems the package is zlib-devel

7

It means you asked it to include the library 'libz.a' or 'libz.so' containing a compression package, and although the compiler found some files, none of them was suitable for the build you are using.

You either need to change your build parameters or you need to get the correct library installed or you need to specify where the correct library is on the link command line with a -L/where/it/is/lib type option.

7

Another possible cause: You've passed --static to the linker, but you only have a dynamic version of libz (libz.so), but not a version that can be statically linked (libz.a).

4

This will show you clues about why the linker doesn't want the installed library:

LD_DEBUG=all make ...

I had the same problem in a different context: my system /lib/libz.so.1 had unsatisfied dependencies on libc because I was trying to relink on a different version of the OS.

2

for opensuse 12.3 (Dartmouth) (i586) sudo zypper install zlib-devel zlib-devel-static

1

Others have mentioned that lib32z-dev solves the problem, but in general the required packages can be found here:

http://source.android.com/source/initializing.html See "Installing required packages"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.