It looks like you're new here. If you want to get involved, click one of these buttons!
Sign In RegisterIt looks like you're new here. If you want to get involved, click one of these buttons!
Python is one of the most widely used programming languages today. Its main advantages are relative simplicity, low entry threshold, versatility in use, and a variety of plugins.
In this guide, we will install the Python 3.12 version on Ubuntu (starting from version 20.04) and Debian 11 and 12.
Please note: It is recommended to create a backup of your system before making significant changes, such as adding third-party repositories or installing software that could impact system dependencies.
Start by updating your system:
apt update -y && apt upgrade -y
Next, you need to install Python.
The steps to install Python on Ubuntu and Debian are different.
The deadsnakes PPA offers the latest Python versions for Ubuntu.
Note: The deadsnakes PPA provides newer Python versions, but it might introduce conflicts with the system Python. Be cautious when using it on applications relying on the default Python version.
Install essential tools for managing software properties.
apt install software-properties-common -y
Add the repository providing the latest Python versions and update the package list:
add-apt-repository ppa:deadsnakes/ppa -y
apt update
Install Python version 3.12 from the added repository.
apt install python3.12 -y
Debian does not support the deadsnakes PPA, so Python must be built from source.
Install packages needed to build Python from source.
apt install -y wget build-essential zlib1g-dev libssl-dev libncurses5-dev libnss3-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev
Download the Python 3.12 source archive.
cd /usr/src
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
Unpack the downloaded source code.
tar xzf Python-3.12.0.tgz
cd Python-3.12.0
Configure, compile, and install Python with optimizations:
./configure --enable-optimizations
make -j$(nproc)
make altinstall
The execution of these commands may take some time to complete.
Using make altinstall instead of make install is crucial to avoid overwriting the system Python binary (python3).
On Ubuntu 24.04, Python 3.12 is the default version, but on other distributions, you may need to make adjustments to set Python 3.12 as the default version.
To set Python 3.12 as the default python3 version, you need to update the python3 symlink.
Run this command:
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
If you are getting an error, try this one:
update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.12 1
Then verify the default Python version:
python3 --version
You should see 12 version (the exact version depends on your distribution):
Python 3.12.0