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.
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.
1. Update the system
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.
2. Steps For Ubuntu
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.
2.1. Install Python Dependencies
Install essential tools for managing software properties.
apt install software-properties-common -y
2.2. Add the deadsnakes PPA
Add the repository providing the latest Python versions and update the package list:
add-apt-repository ppa:deadsnakes/ppa -y
apt update
2.3. Install Python 3.12:
Install Python version 3.12 from the added repository.
apt install python3.12 -y
3. Steps For Debian
Debian does not support the deadsnakes PPA, so Python must be built from source.
3.1. Install Required Build Dependencies:
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
3.2. Download Python 3.12 Source Code:
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
3.3. Extract the Source Code:
Unpack the downloaded source code.
tar xzf Python-3.12.0.tgz
cd Python-3.12.0
3.4. Configure and Compile Python
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).
4. Set Python 3.12 As The Default Version (For both Ubuntu and Debian)
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