How to fix “zipimport.ZipImportError: can’t decompress data; zlib not available”

The error “zipimport.ZipImportError: can’t decompress data; zlib not available” can occur when trying to install python3 or any such zipped applications for example using a command like “get-pip.py”. The major cause of this problem is that the system does not have a decompressor to extract the files for download. So, we need to download a library that can perform compression and decompression on these files.

What is zlib?

The zlib is a library used for compressing and decompressing files by performing read and write functions. However, it only works with certain compatible files.

Installing this library will help us to access any compressed files and hence remove the error that we are facing.

Prerequisites

Make sure your terminal has sudo privileges.

Make sure that python is correctly installed on your system first. Run the following command to get the current version of your python:

$ python3 --version

If it is not installed, then first install it on the Ubuntu/Debian-based operating system by following our dedicated article.

Some tools are required for the respective operating systems to help with the dependencies which are needed to install packages.

Ubuntu/Debian

$ sudo apt install build-essential

Centos/Fedora

$ sudo dnf groupinstall "Development Tools"

Once python and basic tools are installed, let’s move ahead towards the installation of the zlib library.

Install zlib Dependency Library

After checking that you have a functional python installed in your system, you need to install the zlib library on your system.

For Ubuntu/Debian

Execute the command given below to install zlib library dependency:

$ sudo apt install zlib1g-dev

This command works for Ubuntu/Debian.

Missing the OpenSSL lib?

After installing zlib you might catch yourself in an edge case error with OpenSSL, so to get rid of this error execute the commands provided below:

$ sudo apt install libssl-dev
$ sudo apt install libssl1.1 || sudo apt install libssl1.0

For Centos/Fedora

Run the command typed below for Centos/Fedora operating systems:

$ sudo dnf install zlib-devel

By using the above-mentioned commands based on your Operating systems, zlib will be installed on your system successfully.

Conclusion

The zlib is usually pre-installed alongside several other packages that are installed with python. But if not then errors like “zipimport.ZipImportError: can’t decompress data; zlib not available” occurs and each package must be installed manually as we did in this tutorial.