Dayalan Saravanan


Install Python 3.11.5 on Debian 12 GNU/Linux

Thursday, September 14, 2023

The instructions for installing the latest Python version 3.11.5 instead of (or alongside) an older version on Debian 12 GNU/Linux.

Use the python3 --version command to check the version installed. The default version of Python on Debian 12 is Python 3.11.2. If you want to install an updated version, follow the steps below.

  1. Install required development packages to build Python:

    $ doas apt-get update 
    $ doas apt-get install build-essential zliblg-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

  2. Download the latest stable release of Python:

    Visit the https://www.python.org/downloads/ and download the latest version. After the download is complete, you have a Python-3.11.5.tar.xz archive file containing the source code of Python.

    $ wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tar.xz

  3. Extract the tar.xz:

    $ tar -xfv Python-3.11.5.tar.xz

  4. Configure the script:

    Navigate to the configure script and execute it in your terminal.

    $ cd Python-3.11.5/ 
    $ ./configure --enable-optimizations

    Now run make. You can make the build using nproc, which returns the number of CPUs.

    $ make -j `nproc`

  5. Start the build process:

    If you already have a version of Python installed on your system and you want to install the new version alongside it, use the command.

    $ doas make altinstall

    This will install Python at /usr/local/bin/python3.11.

    If you want to replace your current version of Python with this new version, you should uninstall your current Python package using your package manager and then install.

    $ doas make install

    This will install Python at /usr/bin/python3.


  6. Verify the installation:

    If you haven't encountered any errors, the Python is now installed on your system. To verify it, write these in your terminal.

    $ python3.11 --version
        Python 3.11.5

Create a virtual environment:

Python provides a package known as venv, which helps you isolate a program directory or a package.

To create a virtual environment:

$ python3 -m venv env

To activate the virtual environment:

$ source env/bin/activate
(env) $

Notice that your terminal prompt $ is now preceeded by an environment name.

To deactivate the virtual environment:

(env) $ deactivate