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.
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:
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:
If you need the development package for zlib, then install the zlib1g-dev package using the following command:
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:
At the time of writing, the latest version of zlib is v1.2.12. Download the source code archive:
Next, extract the archive using tar:
From the source code directory, run the following commands:
Once the package is finished compiling, install it using the following command:
If you want to uninstall the package later, then use the following command:
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.