How to Install zlib on Ubuntu

Install zlib on Ubuntu with sudo apt install zlib1g-dev for headers and pkg-config, fix configure could not find the zlib library and zlib.h not found errors, verify with pkg-config --modversion zlib, and optionally build zlib 1.3.1 from source.

Published

Updated

Read time 5 min read

Reviewed byDeepak Prasad

Install zlib on Ubuntu banner with apt install zlib1g-dev and pkg-config version output

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).

IMPORTANT
sudo apt install zlib fails with Unable to locate package zlibIt'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.
bash
sudo apt update
sudo apt install -y build-essential pkg-config wget

zlib1g 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:

bash
sudo apt update
apt-cache policy zlib1g zlib1g-dev

On Ubuntu 25.04:

text
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-1ubuntu1

Install or refresh the runtime package:

bash
sudo apt install -y zlib1g

Key files from dpkg -L zlib1g (paths may use /usr/lib/... on newer releases):

text
/usr/lib/x86_64-linux-gnu/libz.so.1.3.1
/usr/lib/x86_64-linux-gnu/libz.so.1

That 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:

bash
sudo apt install -y zlib1g-dev

Development files land in standard locations:

text
/usr/include/zlib.h
/usr/lib/x86_64-linux-gnu/libz.a
/usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc

Verify with pkg-config

bash
pkg-config --modversion zlib
pkg-config --cflags --libs zlib
text
1.3.1
-lz

SysTutorials and most build systems use this check—if pkg-config returns a version, autotools and CMake usually find zlib next.

bash
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-test
text
zlib version: 1.3.1

If 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:

bash
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 install

The ./configure step on Ubuntu 25.04 ended with:

text
Checking for vsnprintf() in stdio.h... Yes.
Checking for return value of vsnprintf()... Yes.
Checking for attribute(visibility) support... Yes.

After make install, confirm:

bash
ls /usr/local/include/zlib.h /usr/local/lib/libz.so.1.3.1

When linking against this custom prefix:

bash
export CPPFLAGS="-I/usr/local/include"
export LDFLAGS="-L/usr/local/lib"
export LD_LIBRARY_PATH="/usr/local/lib:${LD_LIBRARY_PATH}"
WARNING
A /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):

bash
sudo apt purge -y zlib1g-dev

Avoid 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:

bash
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 ldconfig

Troubleshooting

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


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.


Frequently Asked Questions

1. How do I install zlib on Ubuntu?

There is no apt package named zlib. Install the runtime library with sudo apt install zlib1g and development files with sudo apt install zlib1g-dev. Most compile errors need zlib1g-dev, which provides zlib.h, libz.a, and zlib.pc for pkg-config.

2. Why does apt say Unable to locate package zlib?

Ubuntu names the library zlib1g (runtime) and zlib1g-dev (headers). The digit before g is the numeral 1, not the letter L—typing zlib or libz alone will not match. Run apt search zlib1g to see available packages.

3. How do I fix configure error could not find the zlib library?

Install zlib1g-dev, not only zlib1g. The runtime package ships libz.so but not zlib.h or zlib.pc. After apt install zlib1g-dev, rerun ./configure and confirm pkg-config --modversion zlib prints a version.

4. How do I verify zlib is installed on Ubuntu?

Run pkg-config --modversion zlib, ls /usr/include/zlib.h, and ldconfig -p | grep libz. For a compile check, gcc test.c -lz -o test && ./test using #include <zlib.h> and zlibVersion().

5. What is the difference between zlib1g and zlib1g-dev?

zlib1g is the shared runtime library other packages depend on at execution time. zlib1g-dev adds development headers, static libz.a, and pkg-config metadata so gcc, cmake, and ./configure scripts can find and link against zlib.

6. Is zlib already installed on Ubuntu?

Often yes for the runtime. zlib1g is a core dependency of apt, OpenSSL tooling, and many desktop packages. dpkg -l zlib1g shows ii when it is present. You still need zlib1g-dev separately when compiling software from source.

7. When should I build zlib from source on Ubuntu?

Only when you need a newer upstream release than Ubuntu ships, a custom --prefix, or patches. For normal development use sudo apt install zlib1g-dev. Source builds go to /usr/local by default and can shadow distro packages if you are not careful.

8. How do I fix cannot find -lz when linking?

Install zlib1g-dev and pass -lz to the linker (gcc myapp.c -o myapp -lz). If you built zlib under /usr/local, export LDFLAGS="-L/usr/local/lib" and CPPFLAGS="-I/usr/local/include" before compiling.

9. How do I uninstall zlib from Ubuntu?

Run sudo apt purge -y zlib1g-dev for the development package. Do not purge zlib1g unless you understand the dependency impact—many system packages require the runtime library. Remove custom /usr/local/zlib trees manually if you compiled from source.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive …