Python is one of the widely used and versatile computer programming languages, and system users must have the right version installed as it relates to functionality, security, and efficiency. This guide will help explain how to check and update the Python version on Ubuntu 24.04 by employing any of the available methods. Whether you are an enthusiast, a developer, or a sysadmin, this guide covers all the steps needed in depth to support you.
Table of Contents
How to Check and Update the Python Version on Ubuntu 24.04
- Method 1: Check the Installed Python Version
- Method 2: Update Python Using APT
- Method 3: Install a Specific Python Version Using DeadSnakes PPA
- Method 4: Compile Python from Source
How to Check and Update the Python Version on Ubuntu 24.04
To check and update Python on Ubuntu 24.04, use python3 –version to check the current version, and update via sudo apt update && sudo apt install python3. There are various reasons for checking and updating the Python version.
Here are given below:
- Compatibility: Specific established applications and libraries have their requirements that depend on certain versions of Python to function properly.
- Security: Protecting systems comes in many ways. Newer versions of Python are known to have critical security patches and other methods of protecting the system.
- Performance: With updated versions, there is most often an automatically provided improvement in performance, enhancement in the everyday coding experience due to the reaping of the bugs, new features, and improvements in the division of the undertaken tasks.
Method 1: Check the Installed Python Version
Before upgrading Python, understanding what is currently installed in the versioning system is important.
Step 1: Open the Terminal
To open the terminal, which is where all the commands will be executed, press CTRL along with ALT and T.
Step 2: Check Python 3 Version
This command fetches the versions of Python 3 installed, and what will be displayed is your installed version e.g. Python 3.12.3:
python3 –version

This command displays the installed Python 3 version (e.g., Python 3.12.3).
Step 3: Check Python 2 Version (if installed)
To check whether Python 2 is installed and to check its version (if it is installed) run:
python2 --version

Note: It is recommended to use Python 3 since Python 2 is no longer supported.
Step 4: Check Default Python Version
In case you have both version 2 and version 3 installed, you can check the default version using:
python --version

This will show you which version is the default if the command python is executed.
Method 2: Update Python Using APT
For Ubuntu users, the recommended and straightforward way of updating Python is by using the APT package manager. Let’s explore it:
Step 1: Update the Package List
In this step, users need to refresh the repository:
sudo apt update

The statement above makes sure that your system has the latest information regarding the available packages.
Step 2: Upgrade Installed Packages
Python will also be included in the upgraded packages, if a newer version is available in the repository, then it will be installed:
sudo apt upgrade

This command will install updates for packages that have already been installed.
Step 3: Download The Latest Version Of Python
In case there is an available newer version of Python, use this command:
sudo apt install python3

This command will use the latest version available from the repository.
Step 4: Verify the Update
Confirm the update by checking the current version of Python using:
python3 --version

This ensures the update has been done successfully.
Method 3: Install a Specific Python Version Using DeadSnakes PPA
You can utilize the DeadSnakes PPA if the default Ubuntu repository does not have the Python version that you need. There are several different versions of Python available there.
Step 1: Add the DeadSnakes PPA
Execute these commands to add the PPA:
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

The DeadSnakes PPA contains a wide array of Python versions that are not available in the default repository.
Step 2: Installing the Required Python Version
For instance, if you wish to install Python 3.11:
sudo apt install python3.11

Try replacing 3.11 with whichever version you require.
Step 3: Set the Default Python Version
In the case that you wish to use the new default version, then you have to pass the update-alternatives command to it:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 1
sudo update-alternatives --config python3

This enables you to use multiple versions of Python which are already installed in your system.
Method 4: Compile Python from Source
If you wish to have the latest version of Python or would like a specific build, then you can cross-compile it from the source.
Step 1: Install Build Dependencies
Get the needed machine tools and libraries to compile the Python version you intend to use:
sudo apt update
sudo apt install build-essential checkinstall
sudo apt install libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

The listed packages will be required to build Python from the source.
Step 2: Downloading the Source code of Python
You can get the Python version you want from the website. In our case, we need to download Python 3.12’s files:
wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Step 3: Compiling and Extracting Python
Now, let us extract the downloaded folder and at the same time compile Python:
tar -xvf Python-3.12.0.tgz
cd Python-3.12.0
./configure --enable-optimizations
make
sudo make install

Using the –enable-optimizations flag will enhance the performance of Python.
Step 4: Confirming the Installation
Ensure that the correct version of Python is installed:
python3 --version

This proves that the added version is indeed successful.
Conclusion
Checking and updating the Python version on Ubuntu 24.04 is essential for ensuring compatibility, security, and performance. Whether you use APT, the DeadSnakes PPA, or compile from source, this guide provides detailed steps to help you keep your Python environment up-to-date. With these methods, you can check your Python version and effortlessly change it to fulfill your system’s development requirements.
Frequently Asked Questions
python3 --version
or python --version.
sudo apt install python3
to install the latest Python 3 version available in the repositories. update-alternatives
command: sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.x 1
. python3 --version
to verify the version. Ensure it matches the one you installed.
Leave feedback about this