zlib is the ubiquitous compression library behind gzip, PNG, PDF, and countless ./configure scripts that check for zlib.h. On Ubuntu you rarely hunt a package literally named zlib—the distro ships zlib1g (runtime) and zlib1g-dev (headers and pkg-config files). If you are compiling CMake projects, Python extensions, or scientific stacks and hit could not find the zlib library, you almost always need the -dev package.
This guide shows how to install zlib on Ubuntu with apt, verify the install, link a small test program with -lz, and optionally build zlib 1.3.1 from the official upstream release. I ran the commands on Ubuntu 25.04 and kept real output below.
Tested on: Ubuntu 25.04 (Plucky Puffin); kernel 6.14.0-37-generic; zlib 1.3.1 (
zlib1g/zlib1g-dev).
sudo apt install zlib fails with Unable to locate package zlib—It's FOSS and Ubuntu packaging use zlib1g and zlib1g-dev instead. Watch the 1 in zlib1g (numeral one, not lowercase L).
Quick command summary
| Task | Command |
|---|---|
| Runtime library (often preinstalled) | sudo apt install zlib1g |
| Headers + pkg-config (compile from source) | sudo apt install zlib1g-dev |
| Check apt candidates | apt-cache policy zlib1g zlib1g-dev |
| Verify version | pkg-config --modversion zlib |
| Confirm header path | ls /usr/include/zlib.h |
| Link test | gcc test.c -o test -lz |
| Remove dev package | sudo apt purge -y zlib1g-dev |
Choose an install path
| Method | Best for | Jump to |
|---|---|---|
| zlib1g (apt runtime) | Running programs that already depend on libz | Runtime package |
| zlib1g-dev (apt dev) | ./configure, gcc, CMake, fixing missing zlib.h |
Development package |
| Upstream source tarball | Newer/custom build under /usr/local |
Build from source |
For most Ubuntu developers, sudo apt install zlib1g-dev is the entire fix when a build stops with zlib errors.
Prerequisites
- Ubuntu 22.04 LTS, 24.04 LTS, or newer (25.04 tested here) on amd64 or arm64.
- sudo for apt installs.
- For source builds:
build-essential, wget or curl, and tar.
sudo apt update
sudo apt install -y build-essential pkg-config wgetzlib1g vs zlib1g-dev
| Package | Provides | When you need it |
|---|---|---|
| zlib1g | libz.so.1 shared library |
Runtime—often already installed as a dependency |
| zlib1g-dev | zlib.h, libz.a, zlib.pc |
Compiling or configuring software that links -lz |
Ask Ubuntu documents the common mistake: installing only zlib1g leaves pkg-config without zlib.pc, so ./configure reports could not find the zlib library. Install zlib1g-dev.
On Debian-derived systems the development package is zlib1g-dev (not the older GraalVM doc name libz-dev, which is not published on current Ubuntu indexes).
Install zlib1g (runtime library)
Refresh indexes and inspect what apt offers:
sudo apt update
apt-cache policy zlib1g zlib1g-devOn Ubuntu 25.04:
zlib1g:
Installed: 1:1.3.dfsg+really1.3.1-1ubuntu1
Candidate: 1:1.3.dfsg+really1.3.1-1ubuntu1
zlib1g-dev:
Installed: (none)
Candidate: 1:1.3.dfsg+really1.3.1-1ubuntu1Install or refresh the runtime package:
sudo apt install -y zlib1gKey files from dpkg -L zlib1g (paths may use /usr/lib/... on newer releases):
/usr/lib/x86_64-linux-gnu/libz.so.1.3.1
/usr/lib/x86_64-linux-gnu/libz.so.1That is enough for binaries that already link libz—it does not install zlib.h.
Install zlib1g-dev for compiling
This is the package you want before running ./configure, cmake, or pip install on modules that compile C extensions:
sudo apt install -y zlib1g-devDevelopment files land in standard locations:
/usr/include/zlib.h
/usr/lib/x86_64-linux-gnu/libz.a
/usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pcVerify with pkg-config
pkg-config --modversion zlib
pkg-config --cflags --libs zlib1.3.1
-lzSysTutorials and most build systems use this check—if pkg-config returns a version, autotools and CMake usually find zlib next.
Quick compile and link test
cat > /tmp/zlib-test.c <<'EOF'
#include <stdio.h>
#include <zlib.h>
int main(void) {
printf("zlib version: %s\n", zlibVersion());
return 0;
}
EOF
gcc -o /tmp/zlib-test /tmp/zlib-test.c -lz
/tmp/zlib-testzlib version: 1.3.1If that prints a version string, headers and -lz linking work.
Optional: build zlib from upstream source
Use upstream only when apt’s version is not enough. Download the current stable release from madler/zlib on GitHub:
cd /tmp
wget https://github.com/madler/zlib/releases/download/v1.3.1/zlib-1.3.1.tar.gz
tar -xzf zlib-1.3.1.tar.gz
cd zlib-1.3.1
./configure --prefix=/usr/local
make -j"$(nproc)"
sudo make installThe ./configure step on Ubuntu 25.04 ended with:
Checking for vsnprintf() in stdio.h... Yes.
Checking for return value of vsnprintf()... Yes.
Checking for attribute(visibility) support... Yes.After make install, confirm:
ls /usr/local/include/zlib.h /usr/local/lib/libz.so.1.3.1When linking against this custom prefix:
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"
export LD_LIBRARY_PATH="/usr/local/lib:${LD_LIBRARY_PATH}"/usr/local zlib can take precedence over Ubuntu’s zlib1g for software you compile yourself. Prefer zlib1g-dev unless you have a concrete reason to maintain a separate tree.
Uninstall
Development package only (safe for most workflows):
sudo apt purge -y zlib1g-devAvoid purging zlib1g unless you know nothing on the system needs libz—apt-cache rdepends zlib1g lists a long dependency chain.
Custom source install under /usr/local:
sudo rm -rf /usr/local/include/zlib.h /usr/local/include/zconf.h
sudo rm -f /usr/local/lib/libz.*
sudo rm -rf /usr/local/lib/pkgconfig/zlib.pc
sudo ldconfigTroubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
Unable to locate package zlib |
Wrong package name | Use zlib1g / zlib1g-dev |
configure: error: could not find the zlib library |
Missing dev package / zlib.pc |
sudo apt install zlib1g-dev |
zlib.h: No such file or directory |
Headers not installed | sudo apt install zlib1g-dev |
cannot find -lz |
Dev package missing or wrong LDFLAGS |
Install zlib1g-dev; add -lz to linker flags |
pkg-config: Package zlib not found |
zlib1g-dev not installed |
sudo apt install zlib1g-dev pkg-config |
Python zlib not available / zipimport errors |
Python built without zlib | Reinstall zlib1g-dev and rebuild Python, or use distro python3 |
Custom /usr/local zlib conflicts |
Mixed prefixes | Pick apt or /usr/local; align CPPFLAGS/LDFLAGS |
References
- zlib home and madler/zlib releases
- Ask Ubuntu — configure could not find the zlib library
- It's FOSS — install zlib on Ubuntu
- Install cURL on Ubuntu (another common build dependency)
- wget command
Summary
Installing zlib on Ubuntu means picking the right apt name: zlib1g for the shared runtime library and zlib1g-dev when you compile software that needs zlib.h or pkg-config --modversion zlib. Skip sudo apt install zlib—that package does not exist. Verify with pkg-config, link a quick test with gcc ... -lz, and reach for the 1.3.1 source tarball only when apt’s version is not enough for your project.

