Table of Contents
Introduction to Zlib
zlib is the software library for data compression and is free software distributed under the zlib License. This library is an abstraction of the DEFLATE algorithm used in the gzip file compression program. zlib is also a critical part of many software such as Linux, MacOS and iOS.
zlib provides facilities for controlling CPU and memory usage. It is possible to play with the compression level to determine the compression speed. There are also memory containment facilities that are useful in constrained memory environments such as some embedded systems.
It comes preinstalled on many operating systems. In this article, we will explain the installation on Ubuntu with 2 different methods.
Different methods to install zlib in Ubuntu
As with every Linux operating system, there is a zlib package in the Ubuntu repositories. First of all, let's explain the package installation from the repository and then the zlib installation with the source code.
Method-1: Install zlib From Repository
Update the package list before installation:
foc@ubuntu22:~$ sudo apt update
Then look at the version in the repository:
foc@ubuntu22:~$ sudo apt search zlib zlib1g/jammy-updates,jammy-security 1:1.2.11.dfsg-2ubuntu9.2 amd64 [upgradable from: 1:1.2.11.dfsg-2ubuntu9] compression library - runtime
In Ubuntu, the zlib package is in the repository with the name zlib1g and version 1.2.11. Installation is done with the following command:
foc@ubuntu22:~$ sudo apt install zlib1g -y
After installation, the files belonging to the package are placed in the relevant directories:
foc@ubuntu22:~$ dpkg -L zlib1g /. /lib /lib/x86_64-linux-gnu /lib/x86_64-linux-gnu/libz.so.1.2.11 /usr /usr/share /usr/share/doc /usr/share/doc/zlib1g /usr/share/doc/zlib1g/changelog.Debian.gz /usr/share/doc/zlib1g/copyright /lib/x86_64-linux-gnu/libz.so.1
Method-2: Install zlib From Source Code
In this method, we will install from the source code. Version 1.2.13 is available on the official github page. Let's download and compile.
Go to version 1.2.13 and right click on the tar.gz file and copy the link address.
Then type the link address after the wget command in the terminal:
foc@ubuntu22:~$ wget https://github.com/madler/zlib/releases/download/v1.2.13/zlib-1.2.13.tar.gz
Extract the downloaded file using tar command:
foc@ubuntu22:~$ tar -xvf zlib-1.2.13.tar.gz
Enter the file created after this process:
foc@ubuntu22:~$ cd zlib-1.2.13/
Then configure the zlib library:
foc@ubuntu22:~/zlib-1.2.13$ ./configure --prefix=/usr/local/zlib Checking for gcc... Checking for shared library support... Building shared library libz.so.1.2.13 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 attribute(visibility) support... Yes.
When installing from the repository, the libraries were located in
/lib/x86_64-linux-gnu. When installing from the source code, give the above command the directory where the libraries will be placed with the
Then compile the zlib:
And finally install the zlib:
foc@ubuntu22:~/zlib-1.2.13$ sudo make install [sudo] password for foc: rm -f /usr/local/zlib/lib/libz.a cp libz.a /usr/local/zlib/lib chmod 644 /usr/local/zlib/lib/libz.a cp libz.so.1.2.13 /usr/local/zlib/lib chmod 755 /usr/local/zlib/lib/libz.so.1.2.13 rm -f /usr/local/zlib/share/man/man3/zlib.3 cp zlib.3 /usr/local/zlib/share/man/man3 chmod 644 /usr/local/zlib/share/man/man3/zlib.3 rm -f /usr/local/zlib/lib/pkgconfig/zlib.pc cp zlib.pc /usr/local/zlib/lib/pkgconfig chmod 644 /usr/local/zlib/lib/pkgconfig/zlib.pc rm -f /usr/local/zlib/include/zlib.h /usr/local/zlib/include/zconf.h cp zlib.h zconf.h /usr/local/zlib/include chmod 644 /usr/local/zlib/include/zlib.h /usr/local/zlib/include/zconf.h
Installation completed successfully. The zlib library is located under the
foc@ubuntu22:~/zlib-1.2.13$ tree /usr/local/zlib/ /usr/local/zlib/ ├── include │ ├── zconf.h │ └── zlib.h ├── lib │ ├── libz.a │ ├── libz.so -> libz.so.1.2.13 │ ├── libz.so.1 -> libz.so.1.2.13 │ ├── libz.so.1.2.13 │ └── pkgconfig │ └── zlib.pc └── share └── man └── man3 └── zlib.3 6 directories, 8 files
If you need to use an up-to-date zlib library, the installation steps from the source code will help. However, if there is no such requirement, we recommend using the packages in the Ubuntu repository. Packages in the repository are always stable and previously tested packages.
For more detailed and up-to-date information about Zlib, you can visit zlib.net.