zlib is a widely-used compression library that provides in-memory compression and decompression functions for data streams. It powers the gzip and PNG file formats and serves as a critical dependency for compiling software from source. Most users encounter zlib when building applications that require compression capabilities or when package build processes list it as a prerequisite.
On Ubuntu, zlib installs via the default APT (Advanced Package Tool, Ubuntu’s package manager similar to combining Windows Update with the Microsoft Store) repository for stable, maintained versions, or by compiling from source when you need the absolute latest features or optimizations. This guide covers both methods with concrete examples and verification steps.
Most users should install zlib via APT for automatic updates and system integration. Compile from source only when you need features unavailable in the repository version or require custom build configurations for specialized applications.
Choose Your zlib Installation Method
Pick the approach that fits your workflow before diving into the commands. Repository packages integrate with Ubuntu’s automatic updates, while source builds deliver the newest code at the cost of extra maintenance.
| Method | Version/Channel | Stability | Best For |
|---|---|---|---|
APT package (zlib1g-dev) | Ubuntu main repository | Fully tested with automated security updates | Developers who want painless system integration and predictable updates |
| Compile from source | Latest GitHub release tarballs | Depends on upstream release quality and your build options | Users who need newer compression features or custom compiler optimizations |
Method 1: Install zlib via Ubuntu’s Default Repository
Update Ubuntu Before zlib Installation
Before installing zlib, update your Ubuntu installation to ensure all system packages are current. This helps prevent potential conflicts and ensures compatibility with the latest software packages, and you can review the process in our APT maintenance guide if you need a refresher.
sudo apt update
After updating the package list, apply any available upgrades:
sudo apt upgrade
This command installs the latest versions of all packages on your system, ensuring a stable and secure environment.
Check for Existing zlib Installation
First, verify whether zlib development files are already present on your system:
dpkg -l | grep zlib1g-dev
The dpkg -l portion lists installed packages, while the grep command filters that list for zlib1g-dev. If zlib development files are already installed, you’ll see output similar to this:
ii zlib1g-dev:amd64 1:1.2.11.dfsg-2ubuntu9.2 amd64 compression library - development
The ii prefix indicates the package is installed and configured correctly. If no output appears, zlib development files are not present and you should proceed with installation.
Install zlib on Ubuntu via APT Command
Install the zlib development package, which contains header files and libraries necessary for compiling software that uses zlib compression. The zlib1g-dev package provides everything needed to build applications from source that depend on zlib, similar to how Windows SDK packages provide development files for building Windows applications.
sudo apt install zlib1g-dev
This command installs the zlib1g-dev package, ensuring your system has the necessary files to compile and link software using zlib.
Verify zlib Installation
Confirm the installation succeeded by checking the installed version. The pkg-config utility reports metadata for development libraries, so install it first if it is not already present:
sudo apt install pkg-config
Then run the command to list the installed zlib version:
pkg-config --modversion zlib
The command displays the zlib version number, confirming the development files are properly installed and available for compilation. A typical output looks like:
1.2.11
Your specific version may differ depending on your Ubuntu release, but any version output confirms successful installation. The pkg-config utility reads metadata from installed development libraries, making it the standard method for verifying library availability before compilation.
Method 2: Install zlib via Source Archive
Alternatively, you can compile zlib from source to access the latest version or when you need specific build configurations unavailable in the repository package. However, this approach requires manual maintenance: future updates require downloading and recompiling new releases rather than receiving automatic APT updates.
Download the zlib Source Code
Begin by downloading the zlib source code from the official GitHub releases page using wget. This example uses version 1.3.1, but always check the zlib GitHub releases page for the latest available version before downloading:
wget https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz
Replace
v1.3.1.tar.gzwith the latest version number from the releases page. The version shown in this guide serves as an example and may be outdated by the time you read this. Always verify you’re downloading the current stable release to benefit from the latest bug fixes and security patches.
The download progresses with status output showing transfer speed and completion percentage:
--2025-11-15 10:23:45-- https://github.com/madler/zlib/archive/refs/tags/v1.3.1.tar.gz Resolving github.com (github.com)... 140.82.121.3 Connecting to github.com (github.com)|140.82.121.3|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1487665 (1.4M) [application/octet-stream] Saving to: 'v1.3.1.tar.gz' v1.3.1.tar.gz 100%[===================>] 1.42M 3.21MB/s in 0.4s 2025-11-15 10:23:46 (3.21 MB/s) - 'v1.3.1.tar.gz' saved [1487665/1487665]
The archive size varies by version but typically ranges from 1-2 MB, downloading within seconds on most connections.
Extract zlib Source Archive
Once downloaded, extract the archive using the tar command:
tar -xvzf v1.3.1.tar.gz
Replace v1.3.1.tar.gz with your downloaded version. The extraction creates a directory matching the version number (e.g., zlib-1.3.1).
Install Build Dependencies
Source code compilation requires a compiler and build tools to transform human-readable code into executable binaries. Install the build-essential package, which bundles the GCC compiler, linker, and Make automation tool needed for building software on Ubuntu:
sudo apt install build-essential
The build-essential package includes gcc (compiler), make (build automation), and other essential development tools, eliminating the need to install them separately.
Configure, Compile, and Install zlib
Navigate to the extracted zlib source directory:
cd zlib-1.3.1
Replace zlib-1.3.1 with your version. The directory name matches the version number in the downloaded archive.
Next, configure the zlib build with a custom installation prefix. The --prefix flag sets where zlib installs, using /usr/local/zlib to keep the custom build separate from system-managed packages:
./configure --prefix=/usr/local/zlib
The configure script analyzes your system environment and generates the Makefile needed for compilation. You’ll see output showing detected features and settings:
Checking for gcc... Checking for shared library support... Building shared library libz.so.1.3.1 with gcc. Checking for size_t... Yes. Checking for off64_t... Yes. Checking for fseeko... Yes. Checking for strerror... Yes. Checking for unistd.h... Yes. Checking for stdarg.h... Yes. Checking whether to use vs[n]printf() or s[n]printf()... using vs[n]printf(). Checking for vsnprintf() in stdio.h... Yes. Checking for return value of vsnprintf()... Yes. Checking for errno.h... Yes. Checking for mmap support... Yes.
These checks ensure zlib builds with optimal settings for your system architecture. After configuration completes successfully, compile the zlib source code:
make
Finally, install the compiled zlib library to the configured prefix location:
sudo make install
The configure script generates the Makefile based on your system configuration, make compiles the source code, and make install copies the compiled library and header files to /usr/local/zlib.
Since /usr/local/zlib is not part of Ubuntu’s default linker paths, add it to a configuration snippet before refreshing the cache:
echo "/usr/local/zlib/lib" | sudo tee /etc/ld.so.conf.d/zlib.conf
After registering the custom path, refresh the dynamic linker cache so applications can find the new library:
sudo ldconfig
The ldconfig command updates the system’s shared library cache using the entries in /etc/ld.so.conf.d/, registering your custom zlib installation so the dynamic linker can locate it when applications request compression functions at runtime. This step is essential after installing libraries to non-standard paths like /usr/local/zlib. Without running ldconfig, applications may fail to find the library even though it’s installed.
Verify Source Installation
After installation completes, confirm the compiled zlib installed correctly by checking the library location:
ls -la /usr/local/zlib/lib/
Successful installation produces output showing the compiled library files:
total 480 drwxr-xr-x 3 root root 4096 Nov 15 10:35 . drwxr-xr-x 4 root root 4096 Nov 15 10:35 .. lrwxrwxrwx 1 root root 13 Nov 15 10:35 libz.so -> libz.so.1.3.1 lrwxrwxrwx 1 root root 13 Nov 15 10:35 libz.so.1 -> libz.so.1.3.1 -rwxr-xr-x 1 root root 103288 Nov 15 10:35 libz.so.1.3.1 -rw-r--r-- 1 root root 183360 Nov 15 10:35 libz.a drwxr-xr-x 2 root root 4096 Nov 15 10:35 pkgconfig
The output confirms both the shared library (libz.so.1.3.1) and static library (libz.a) installed successfully. The symbolic links (libz.so and libz.so.1) point to the versioned library, allowing applications to link against zlib using the generic name while the system loads the specific version. When compiling other software that depends on this custom zlib build, point the build system to your installation by exporting the library path:
export PKG_CONFIG_PATH=/usr/local/zlib/lib/pkgconfig:$PKG_CONFIG_PATH
This environment variable tells pkg-config where to search for library metadata files. The .pc files in the pkgconfig directory contain compiler flags, linker settings, and dependency information that configure scripts need to properly link against zlib. Without this export, build systems will find the older system version instead of your custom installation, potentially causing version mismatches or missing features.
Persist the variable for future shells so you do not need to re-export it every session:
echo 'export PKG_CONFIG_PATH=/usr/local/zlib/lib/pkgconfig:$PKG_CONFIG_PATH' >> ~/.bashrc
This command appends the export statement to your .bashrc file (the bash configuration file that runs automatically when opening new terminal sessions, similar to Windows startup programs). The >> operator appends without overwriting existing content. Reload your shell to apply the change immediately:
source ~/.bashrc
The source command re-reads the .bashrc file in your current shell session, applying the new environment variable without requiring a logout or terminal restart. Verify the variable is set correctly:
echo $PKG_CONFIG_PATH
The output should include your custom zlib path:
/usr/local/zlib/lib/pkgconfig:/usr/lib/x86_64-linux-gnu/pkgconfig:/usr/share/pkgconfig
Your custom path appears first, ensuring build systems find your zlib installation before checking system paths.
Conclusion
Both installation methods serve distinct use cases. The APT method provides automatic updates and system integration suitable for most users, while source compilation delivers access to the latest features when repository versions lag behind upstream releases. Remember that source-compiled versions require manual recompilation for updates, whereas APT-managed packages update automatically through standard system maintenance. Your Ubuntu system now has the compression library needed to build software dependencies and support applications requiring zlib functionality.
Useful Links
Here are some valuable links related to using zlib:
- zlib Official Website: General information and official download links for the latest stable releases.
- zlib Manual: Complete API documentation for developers integrating zlib into applications.
- zlib How-to Guide: Practical programming examples and implementation patterns for common compression tasks.
- zlib GitHub Releases: Latest development versions, release notes, and changelog for tracking updates.
- zlib GitHub Wiki: Community documentation, troubleshooting tips, and contributed tutorials.