I recently ran into a frustrating issue while trying to install pip
on Linux Mint. My python3
lived at /usr/local/bin/python3
, and every time I ran get-pip.py
, it blew up with this error:
zipimport.ZipImportError: can't decompress data; zlib not available
At first, I thought reinstalling zlib
using apt-get
would solve it. But even after that, the error didn’t go away. After a bit of digging, I figured out what was really going on and how to fix it. In this blog, I’ll walk you through the entire process the error, why it happens, and multiple fixes (from quick to thorough). I’ll also share some extra tools I wrote for practice and diagnostics.
First Check Does Python Have zlib
The first thing I did was verify if my python3
had zlib support. Run this snippet:
python3 -c "import sys; print(sys.executable); import zlib; print('OK:', zlib.ZLIB_VERSION)"
Expected Output
- If zlib is available:
/usr/bin/python3 OK: 1.2.11
- If zlib is missing (like my case):
/usr/local/bin/python3 Traceback (most recent call last): ... ModuleNotFoundError: No module named 'zlib'
Or you’ll see the same zipimport.ZipImportError
when running get-pip.py
.
Why This Error Happens
That scary error actually means:
Your Python binary was compiled without zlib support.
Here’s why:
pip
andget-pip.py
rely on zipped wheel files.- If Python doesn’t have zlib built in, it can’t decompress them.
Even though I installed zlib1g
and zlib1g-dev
with apt, my /usr/local/bin/python3
was a custom build. It had been compiled before the zlib development headers were installed. That meant the interpreter was permanently missing zlib.
Either use the system Python (which has zlib) or rebuild my custom one with zlib support.
Fixes (Pick Your Approach)
Fastest: Use the Distro Python & Pip
If you don’t need a custom Python, just use Linux Mint’s packaged tools:
sudo apt update
sudo apt install python3-pip
Then make sure you’re using the distro Python:
/usr/bin/python3 -m pip --version
If python3
still defaults to /usr/local/bin/python3
, just call /usr/bin/python3
explicitly:
/usr/bin/python3 -m pip install --user <package>
Or update your PATH
so /usr/bin
comes first.
Recommended Rebuild Python with zlib
If you need a custom build, you must rebuild Python after installing the right headers.
- Install dependencies:
sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
libffi-dev liblzma-dev
- Rebuild Python from your source folder:
./configure --enable-optimizations --with-ensurepip=install
make -j"$(nproc)"
sudo make altinstall
- Verify zlib and pip:
/usr/local/bin/python3.X -c "import zlib; print('OK:', zlib.ZLIB_VERSION)"
/usr/local/bin/python3.X -m ensurepip --upgrade
/usr/local/bin/python3.X -m pip --version
(Replace 3.X
with your actual version.)
Alternative Use Pyenv
If you like clean and isolated Python installs, pyenv is a lifesaver:
# Install deps
sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
libffi-dev liblzma-dev git
# Install pyenv
curl https://pyenv.run | bash
# Add pyenv init to your shell, then:
pyenv install 3.12.5
pyenv global 3.12.5
python -m pip --version
python -c "import zlib; print('OK:', zlib.ZLIB_VERSION)"
Quick Hack Run get-pip.py with System Python
If your /usr/bin/python3
already has zlib:
/usr/bin/python3 get-pip.py
But honestly, the better long-term solution is either using python3-pip
from apt or rebuilding properly.
Practice Tools I Wrote
I also wrote some helpers to test and practice debugging this.
Bash Fixer Script
Save this as fix-pip-zlib.sh
:
#!/usr/bin/env bash
set -euo pipefail
echo "Python in PATH:"
which -a python3 || true
echo
PY="$(command -v python3 || true)"
if [[ -z "${PY}" ]]; then
echo "No python3 in PATH. Try: sudo apt install python3"
exit 1
fi
echo "Using: ${PY}"
if ! "$PY" - <<'PYCODE'
import sys
print("Version:", sys.version.split()[0])
print("Exec:", sys.executable)
try:
import zlib
print("zlib OK:", zlib.ZLIB_VERSION)
except Exception as e:
print("zlib MISSING:", e)
raise SystemExit(2)
PYCODE
then
echo
echo "==> zlib missing in the currently selected Python."
echo "If ${PY} is under /usr/local, you likely built Python without zlib headers."
echo "Try either:"
echo " 1) Use distro pip: sudo apt update && sudo apt install python3-pip"
echo " 2) Rebuild Python after: sudo apt install zlib1g-dev (plus common build deps)"
echo " Then configure with: ./configure --enable-optimizations --with-ensurepip=install"
exit 2
fi
echo
echo "Attempting 'python3 -m pip --version'…"
if ! "$PY" -m pip --version; then
echo "pip missing; attempting ensurepip…"
"$PY" -m ensurepip --upgrade || {
echo "ensurepip failed. If this is a custom Python, rebuild with --with-ensurepip=install."
exit 3
}
"$PY" -m pip --version
fi
echo
echo "All good"
Usage:
chmod +x fix-pip-zlib.sh
./fix-pip-zlib.sh
Python Diagnosis Helper
Save as diagnose_pip.py
:
import os, sys, shutil, platform
print("Python:", sys.version.split()[0])
print("Exec: ", sys.executable)
print("PATH: ", os.environ.get("PATH",""))
print("which pip:", shutil.which("pip"))
print("which python3:", shutil.which("python3"))
try:
import zlib
print("zlib OK:", zlib.ZLIB_VERSION)
except Exception as e:
print("zlib MISSING:", repr(e))
try:
import pip
print("pip OK:", pip.__version__)
except Exception as e:
print("pip import failed:", repr(e))
print("Platform:", platform.platform())
print("Tip: If Exec path starts with /usr/local and zlib is missing, rebuild Python after installing zlib1g-dev.")
Run it like this:
python3 diagnose_pip.py
Quick Checklist
which python3
→ if it’s/usr/local/bin/python3
, you’re on a custom build.- Install
zlib1g-dev
before compiling Python. - Rebuild with
./configure --with-ensurepip=install
. - Or, just use the distro
python3-pip
. - Always verify:
python3 -c "import zlib; print(zlib.ZLIB_VERSION)"
python3 -m pip --version
Final Thoughts
This problem really taught me the difference between runtime libraries (zlib1g
) and development headers (zlib1g-dev
). If you build Python without the headers installed, you’ll always end up missing zlib support no matter how many times you reinstall zlib afterward. For me, the cleanest fix was switching to pyenv, because it keeps my builds isolated and always ensures the right headers are present. But if you’re happy with the system Python, just stick to sudo apt install python3-pip
. Whenever you see zipimport.ZipImportError: can't decompress data; zlib not available
, it means Python was built wrong not that zlib isn’t installed.