Ubuntu

How To Install Zlib on Ubuntu 22.04

Data compression is an effective way of saving disk space and reducing network bandwidth consumption. However, it comes at the cost of additional computation. Thankfully, modern hardware is powerful enough so that compression minimizes impact on performance.

In this guide, we will showcase installing zlib on Ubuntu 22.04.

Zlib on Ubuntu

Zlib is a popular compression/decompression library that’s free and open-source. In addition, zlib is free from legal liabilities (not covered by any patent), making it a lucrative option for developers. Some other notable features include:

  • Lossless data compression
  • Small memory footprint
  • Never expands the data. For comparison, LZW can increase the compressed data size in extreme cases, even double/triple
  • Portable across platforms

Because of its widespread usage, zlib is also directly available from the default Ubuntu repo. Check the availability using the following command:

$ apt search zlib1g

Note that the package name for zlib is zlib1g, not to be confused with zlibc, a completely different package.

Installing zlib on Ubuntu

There are two different ways to install zlib on Ubuntu. We can either grab the zlib1g package from Ubuntu or build it from the source code.

Installing zlib Using APT

Using APT, we can install the zlib package directly from the default Ubuntu repos. In most situations, this is the recommended way to go.

To install zlib, run the following commands:

$ sudo apt update

$ sudo apt install zlib1g -y

If you need the development package for zlib, then install the zlib1g-dev package using the following command:

$ sudo apt install zlib1g-dev -y

Installing zlib From Source

Zlib is an open-source project. This allows us to grab the source code and build the package ourselves. However, this procedure comes with various inconveniences, such as requiring manual management of the package.

First, install the necessary tools for building zlib:

$ sudo apt install build-essential -y

At the time of writing, the latest version of zlib is v1.2.12. Download the source code archive:

$ wget https://zlib.net/zlib-1.2.12.tar.gz

Next, extract the archive using tar:

$ tar -xvf zlib-1.2.12.tar.gz

From the source code directory, run the following commands:

$ ./configure

$ make -j$(nproc)

Once the package is finished compiling, install it using the following command:

$ sudo make install

If you want to uninstall the package later, then use the following command:

$ sudo make uninstall

Conclusion

In this guide, we explored various ways of installing zlib on Ubuntu. We installed the zlib packages (zlib1g and zlib1g-devel) using APT. We also showcased building and installing zlib from source (applicable in specific scenarios).

Interested in learning more about compression in Linux? Check out Linux file compression options and their comparisons. Zstd is another interesting compression algorithm worth looking into.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.