Installing Zlib in Ubuntu 22.04

Zlib is a popular open-source compression library used by many software applications to compress and decompress data. It provides fast and efficient compression and decompression algorithms that can be used to reduce the size of data, which can improve performance and reduce storage requirements. In this post, we will discuss how to install zlib in Ubuntu 22.04. We will also demonstrated how to test that zlib is working correctly by compressing and decompressing a sample file. After these steps, you should have zlib installed on your Ubuntu 22.04 system.

Update the package list

Before we begin, it’s a good idea to update the package list on your Ubuntu system to ensure that you have the latest information on available packages. You can do this by running the following command in the terminal:

sudo apt update

Install zlib

To install zlib in Ubuntu 22.04, you can use the apt package manager. The zlib package is available in the default Ubuntu repositories, so you can install it by running the following command in the terminal:

sudo apt install zlib1g zlib1g-dev

This command will download and install the zlib1g-dev package, which contains the development files and headers needed to compile software that uses zlib.

Example output:

$ sudo apt install zlib1g-dev
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
  zlib1g-dev
0 upgraded, 1 newly installed, 0 to remove and 13 not upgraded.
Need to get 164 kB of archives.
After this operation, 606 kB of additional disk space will be used.
Get:1 http://security.ubuntu.com/ubuntu jammy-security/main amd64 zlib1g-dev amd64 1:1.2.11.dfsg-2ubuntu9.2 [164 kB]
Fetched 164 kB in 1s (124 kB/s)      
Selecting previously unselected package zlib1g-dev:amd64.
(Reading database ... 509962 files and directories currently installed.)
Preparing to unpack .../zlib1g-dev_1%3a1.2.11.dfsg-2ubuntu9.2_amd64.deb ...
Unpacking zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu9.2) ...
Setting up zlib1g-dev:amd64 (1:1.2.11.dfsg-2ubuntu9.2) ...
Processing triggers for man-db (2.10.2-1) ...

Verify the installation of zlib dev

Once the installation is complete, you can verify that zlib is installed on your system by checking files. You can do this by running the following command in the terminal:

dpkg -L zlib1g-dev

This command should output the files of zlib that is installed on your system. One example output is as follows.

$ dpkg -L zlib1g-dev 
/.
/usr
/usr/include
/usr/include/zconf.h
/usr/include/zlib.h
/usr/lib
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libz.a
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/zlib.pc
/usr/share
/usr/share/doc
/usr/share/doc/zlib1g-dev
/usr/share/doc/zlib1g-dev/FAQ.gz
/usr/share/doc/zlib1g-dev/README.gz
/usr/share/doc/zlib1g-dev/algorithm.txt.gz
/usr/share/doc/zlib1g-dev/copyright
/usr/share/doc/zlib1g-dev/examples
/usr/share/doc/zlib1g-dev/examples/README.examples
/usr/share/doc/zlib1g-dev/examples/crc32_test.c
/usr/share/doc/zlib1g-dev/examples/enough.c
/usr/share/doc/zlib1g-dev/examples/example.c
/usr/share/doc/zlib1g-dev/examples/fitblk.c
/usr/share/doc/zlib1g-dev/examples/gun.c
/usr/share/doc/zlib1g-dev/examples/gzappend.c
/usr/share/doc/zlib1g-dev/examples/gzjoin.c
/usr/share/doc/zlib1g-dev/examples/gzlog.c
/usr/share/doc/zlib1g-dev/examples/gzlog.h
/usr/share/doc/zlib1g-dev/examples/infcover.c
/usr/share/doc/zlib1g-dev/examples/minigzip.c
/usr/share/doc/zlib1g-dev/examples/zlib_how.html
/usr/share/doc/zlib1g-dev/examples/zpipe.c
/usr/share/doc/zlib1g-dev/examples/zran.c
/usr/share/doc/zlib1g-dev/txtvsbin.txt.gz
/usr/share/man
/usr/share/man/man3
/usr/share/man/man3/zlib.3.gz
/usr/lib/x86_64-linux-gnu/libz.so
/usr/share/doc/zlib1g-dev/changelog.Debian.gz

Build a C program that uses zlib dev

We can also build a C program calling zlib to verify it is ready to use by C++ program.

wget http://zlib.net/zpipe.c
gcc zpipe.c -o zpipe -lz

If gcc returns no error, congratulations that Zlib is ready in your Ubuntu 22.04.

Leave a Reply

Your email address will not be published. Required fields are marked *