Data compression is an effective way to save disk space and reduce network bandwidth consumption. However, this comes at the expense of additional calculations. Fortunately, modern hardware is powerful enough that compression minimizes the impact on performance.
Zlib on Ubuntu
Zlib is a popular compression/decompression library that is free and open source. Additionally, zlib is free from legal obligations (not protected by patents), making it a lucrative option for developers. Some other notable features are:
- Lossless data compression
- Low memory requirements
- Never expands the data. For comparison: LZW can even double/triple the compressed data size in extreme cases
- Portable across platforms
Due to its widespread use, zlib is also available directly from the standard Ubuntu repo. Check availability with the following command:
Note that the package name for zlib is zlib1gnot to be confused with zlibca completely different package.
Install zlib on Ubuntu
There are two different ways to install zlib on Ubuntu. We can either get the zlib1g package from Ubuntu or build it from source.
Installing zlib with APT
APT allows us to install the zlib package directly from the standard Ubuntu repos. In most situations, this is the recommended way.
Run the following commands to install zlib:
If you need the development package for zlib, install the zlib1g-dev package with the following command:
Install zlib from source
Zlib is an open source project. This way we can get the source code and build the package ourselves. However, this method is associated with various inconveniences such as: B. the need to manually manage the package.
First, install the necessary tools to build zlib:
At the time of writing, the latest version of zlib is v1.2.12. Download the source code archive:
Next, extract the archive with tar:
From the source code directory, run the following commands:
Once the package has finished compiling, install it with the following command:
If you want to uninstall the package later, use the following command:
In this guide, we’ve explored different ways to install zlib on Ubuntu. We installed the zlib packages (zlib1g and zlib1g-devel) with APT. We also demonstrated building and installing zlib from source (applicable in certain scenarios).
Want to learn more about compression in Linux? Check out Linux file compression options and how they compare. Zhr is another interesting compression algorithm worth checking out.