Install the Python C development headers package matching your exact python3 minor version—such as sudo apt install python3-dev (or python3.12-dev) on Debian/Ubuntu, sudo dnf install python3-devel on Fedora/RHEL, or apk add python3-dev on Alpine—alongside a C compiler toolchain (build-essential / gcc), because pip is building a C/C++ extension from source (sdist) and cannot find Python’s C API header files (Python.h).
Basic Example
# 1. Debian / Ubuntu / WSL: Install headers for default python3 AND your exact minor version
sudo apt update
sudo apt install -y build-essential python3-dev
# If using a non-default Python version (e.g., Python 3.12 from deadsnakes PPA):
sudo apt install -y python3.12-dev
# 2. Fedora / RHEL / Rocky / AlmaLinux:
sudo dnf install -y gcc python3-devel
# 3. Alpine Linux (minimal Docker containers):
apk add --no-cache build-base python3-dev
# 4. Retry installing your package (and upgrade pip first to prefer prebuilt wheels)
python3 -m pip install --upgrade pip
python3 -m pip install <package-name>
Key Details & Edge Cases
- Match the
-dev/-develpackage to your activepython3 --version: On Ubuntu/Debian,sudo apt install python3-devonly installs headers for the system’s default Python (e.g., Python 3.12 on Ubuntu 24.04). If your virtual environment runs Python 3.11 or 3.13,Python.hwill still be missing until you installpython3.11-devorpython3.13-devspecifically. - Why
piptries to compile from source in the first place (and how to avoid compiling altogether):piponly invokes GCC and needsPython.hwhen no precompiled binary wheel (.whl) exists for your OS, CPU architecture (e.g.,aarch64/ ARM64), or brand-new Python version—or when yourpipbinary is too old to recognize modernmanylinuxwheels.- Run
python3 -m pip install --upgrade pip setuptools wheelfirst, or pass--only-binary=:all:to forcepipto download a prebuilt wheel instead of falling back to C compilation.
- Custom or source-compiled Python builds (
pyenv,uv, Conda):pyenv: Headers are compiled automatically duringpyenv install 3.x.y, provided system build dependencies (zlib1g-dev,libssl-dev, etc.) were present beforehand.- Conda / Mamba: Headers ship inside the active Conda environment (
$CONDA_PREFIX/include/python3.x/Python.h); ensure you are using the environment’spip(python -m pip install ...) rather than a systempip.
- Verifying where Python expects
Python.hto live: Check the exact include directory your active interpreter queries during C compilation:python3 -c "import sysconfig; print(sysconfig.get_path('include'))" # Output e.g.: /usr/include/python3.12 (verify Python.h exists inside this folder) - Missing library-specific C headers (
ffi.h,openssl/ssl.h,pg_config): OncePython.his resolved, compiling packages likecryptography,cffi, orpsycopg2may require companion C libraries (libffi-dev,libssl-dev,libpq-dev)—or can be bypassed with binary wheels (e.g.,pip install psycopg2-binary).
Platform & Package Manager Reference
| Operating System / Environment | Command to Install Python.h & C Compiler |
Header Location |
|---|---|---|
| Ubuntu / Debian / WSL | sudo apt install -y build-essential python3-dev (or python3.X-dev) |
/usr/include/python3.X/Python.h |
| Fedora / RHEL / CentOS / Rocky | sudo dnf install -y gcc python3-devel (or python3.X-devel) |
/usr/include/python3.X/Python.h |
| Alpine Linux (Docker) | apk add --no-cache build-base python3-dev |
/usr/include/python3.X/Python.h |
| openSUSE | sudo zypper install -y gcc python3-devel |
/usr/include/python3.X/Python.h |
| macOS | xcode-select --install (headers included with Python.org / Homebrew) |
/Library/Frameworks/... or //homebrew/... |
| Windows | Install Microsoft C++ Build Tools (headers included in python installer) |
C:\Users\<user>\AppData\Local\Programs\Python\Python3X\include\Python.h |
You must log in or register to comment.
