Choosing the right Python version for your Python application plays an important role in future-proofing the application. In this article, we'll look into two methods to install Python 3.12 on Linux systems. Ubuntu server 22.04 LTS comes with Python version 3.10.12 by default. To install Python version 3.12 on Ubuntu 22.04 system, use the following methods:
Using PPA repository
This is the easiest way to install the required Python version in Ubuntu 22.04 LTS. But first, you need to install the required system package for adding a new repository. Use the below command:
sudo apt install software-properties-common
After installing the above package in the system, you can now add PPA repository. We'll use deadsnakes PPA repository that contains the latest and old Python versions for Ubuntu. Keep in mind that PPA's are third-party repositories that are managed by individuals or groups outside of the official Ubuntu package maintainers.
To add deadsnakes repository, use below command:sudo add-apt-repository ppa:deadsnakes/ppa
$
sudo apt update
sudo apt install python3.12
Build from source package
If you don't want to use third-party PPA repositories, then you have the compile the required Python version yourself from the source. However, the process is pretty straight-forward if you have all the reuqired packages installed in your Ubuntu system.
Use below command to install system packages that are necessary to compile Python version from source:sudo apt -y install build-essential pkg-config wget libssl-dev zlib1g-dev libncurses5-dev libgdbm-dev libreadline-dev libffi-dev libsqlite3-dev libbz2-dev libnss3-dev libdb-dev liblzma-dev tk-dev uuid-dev
Installing system packages might take some time (depends upon your internet speed and disk type). Now download Python version 3.12 source file from Python official archive or use the below command to download directly in your current directory:
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tar.xz
$
tar xvf Python-3.12.0.tar.xz
After downloading the source package and extracting it, move to the Python-3.12.0 directory and use the below commands to compile Python from build:
cd Python-3.12.0
$
./configure --enable-optimizations
$
make
$
sudo make altinstall
If you want to install the Python version 3.12 as the default version of your system, then instead of sudo make altinstall, use sudo make install
command.
This process might take time (depending on your system hardware). After the process in completed, run python3.12 --version
command in your terminal to verify the installation.