sodcalculator

10 Tech How-To

Install Python 3.7 on Debian: Step-by-Step Guide

Guide 10-8911 · Updated · Step by step

This guide walks you through installing Python 3.7 on Debian, whether you need it for legacy projects, compatibility with specific libraries, or learning purposes. Debian’s default repositories may not include Python 3.7 (especially on newer releases), so you’ll learn how to add the necessary repository, install the package, and verify the installation.

What You’ll Need

  • A Debian system (Debian 10 Buster or later recommended; older versions may lack required dependencies).
  • Root or sudo access to install packages.
  • An active internet connection to download packages.
  • Basic familiarity with the command line.

Step-by-Step

  1. Update your system
    Open a terminal and run:
    sudo apt update
    sudo apt upgrade -y
    This ensures your package lists are current and all existing packages are up to date.
  2. Install prerequisite packages
    Some tools are needed to add repositories and install software:
    sudo apt install -y wget build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
    These libraries allow Python 3.7 to compile and run with full functionality (e.g., SSL, SQLite, tkinter).
  3. Add the deadsnakes PPA (for Debian)
    Debian doesn’t have an official Python 3.7 package in its main repos, but the deadsnakes PPA (Personal Package Archive) provides it. However, deadsnakes is primarily for Ubuntu. For Debian, you can use the deadsnakes repository via a script:
    sudo apt install -y software-properties-common
    sudo add-apt-repository ppa:deadsnakes/ppa
    If add-apt-repository is not available, install software-properties-common first. If the PPA fails (common on Debian), skip to Step 5.
  4. Update package lists again
    After adding the repository, refresh your package list:
    sudo apt update
  5. Install Python 3.7
    Now install Python 3.7 and its dev headers:
    sudo apt install -y python3.7 python3.7-dev python3.7-venv
    If the PPA method didn’t work, you can compile from source (see Step 6).
  6. Alternative: Compile from source
    If the repository method fails, download and compile Python 3.7.15 (the last 3.7 release):
    cd /tmp
    wget https://www.python.org/ftp/python/3.7.15/Python-3.7.15.tgz
    tar -xzf Python-3.7.15.tgz
    cd Python-3.7.15
    ./configure --enable-optimizations
    make -j$(nproc)
    sudo make altinstall
    The altinstall command prevents overwriting the system’s default python3 binary. This compilation may take 10-20 minutes.
  7. Verify the installation
    Check the version:
    python3.7 --version
    You should see Python 3.7.15 (or similar). If you used the PPA, you might also have python3.7 in your PATH.
  8. Test pip and venv
    Ensure pip is available:
    python3.7 -m pip --version
    If pip is missing, install it with:
    python3.7 -m ensurepip --upgrade
    Create a virtual environment to confirm venv works:
    python3.7 -m venv test_env
    source test_env/bin/activate
    You should see (test_env) in your prompt. Deactivate with deactivate.

Common Mistakes

  • Using python3 instead of python3.7: The system’s default Python might be 3.9 or newer. Always use the explicit python3.7 command to run your scripts.
  • Overwriting the system Python: When compiling, using make install instead of make altinstall can replace /usr/bin/python3, breaking system tools like apt.
  • Missing dependencies: Without the dev libraries (Step 2), compilation may fail or result in a Python without SSL or SQLite support, which causes errors later.
  • Forgetting to update after adding PPA: If you skip sudo apt update, apt won’t see the new packages.

Tips

  • Use virtual environments for each project to avoid dependency conflicts. Always create a venv with python3.7 -m venv myenv.
  • If you need multiple Python versions, consider using pyenv to manage them easily. It lets you switch between versions per directory.
  • For production, stick with the system Python version if possible, as 3.7 is end-of-life (no security updates after June 2023). Use 3.7 only for legacy compatibility.
  • When compiling, use --enable-optimizations for better performance, but be aware it increases build time.

Troubleshooting

  • “add-apt-repository: command not found”: Install software-properties-common with sudo apt install software-properties-common.
  • PPA not working on Debian: Debian isn’t officially supported by deadsnakes. If you get errors like “Unable to add PPA”, switch to the source compilation method (Step 6).
  • Compilation fails with “No module named ‘_ssl'”: Install the OpenSSL dev package: sudo apt install libssl-dev and reconfigure/rebuild.
  • pip not found: Run python3.7 -m ensurepip to install pip. If that fails, download get-pip.py and run it with python3.7 get-pip.py.
  • Python 3.7 not found after install: Check if the binary is in /usr/local/bin (for source install) or /usr/bin (for PPA). Add to PATH if necessary: export PATH=$PATH:/usr/local/bin.
  • Segmentation fault when running Python: This often indicates a missing or mismatched library. Re-run ldconfig or rebuild with the dependencies installed.

Frequently Asked Questions

Can I install Python 3.7 on Debian 11 (Bullseye)?

Yes, but the deadsnakes PPA may not work. Compiling from source is the most reliable method. The steps in this guide cover both approaches.

Will installing Python 3.7 break my system Python?

No, if you use altinstall or install from the PPA, it installs as python3.7 and does not replace the default python3. Only make install would overwrite it.

How do I set Python 3.7 as the default for python3?

You can create a symlink: sudo ln -s /usr/local/bin/python3.7 /usr/local/bin/python3, but this may break system tools. Better to use a virtual environment or a version manager like pyenv.

Is Python 3.7 still supported?

No, Python 3.7 reached end-of-life on June 27, 2023. It no longer receives security updates. Use it only for legacy projects that cannot migrate.

Can I use pip with Python 3.7?

Yes, pip works normally. Use python3.7 -m pip install package-name to install packages. Be aware that some modern packages may drop support for 3.7.

What if I need both Python 3.7 and 3.9?

They can coexist. Use python3.7 for 3.7 and python3 for the system version. Virtual environments help keep dependencies separate.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →