To install a specific previous version of Python on Debian, you can follow these steps:

  1. Update the package lists for upgrades and new package installations:

    sh sudo apt update

  2. Install the necessary prerequisites for building Python:

    sh sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev

  3. Download the source code for the Python version you want to install. You can find the previous versions on the Python website (https://www.python.org/downloads/source/) or choose from available versions at https://www.python.org/ftp/python/. For example, let’s assume you want to install Python 3.9.16:

    wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tgz

  4. Extract the downloaded source code archive:

    tar -xf Python-3.9.16.tgz

  5. Enter the extracted directory:

    cd Python-3.9.16

  6. Configure the build:

    ./configure --enable-optimizations

  7. Build and install Python:

    make -j$(nproc) sudo make altinstall

    The use of make altinstall instead of make install is important to prevent overwriting the default system Python installation.

  8. Verify that the installation was successful and check the installed Python version:

    python3.9 --version

    This command should display the version number of the Python installation you just installed.