How to Install Python on Ubuntu 24.04

How to Install Python on Ubuntu 24.04

In this tutorial, we are going to explain how to install Python on Ubuntu 24.04 OS. Python is a high-level programming language that offers hundreds of libraries for building web applications and use in machine learning. It supports multiple programming paradigms such as structural, functional, and object-oriented programming. The Python programming language offers many features such as security updates, frameworks, integration with other programming languages, ease of use, etc. The latest Python version is Python 3.12 at the moment we are writing this tutorial.

The installation will be done with different methods and take a few minutes. Let’s get started!

Prerequisites

  • A server running Ubuntu 24.04 OS
  • User privileges: root or non-root user with sudo privileges

Update the System

Before we start with the installation, updating the system packages to their latest versions is recommended.

sudo apt update -y && sudo apt upgrade -y

Check previous Python versions

To check if there is any python3 version installed on your server execute the following command:

python3 -V

If there is no Python3 version on your system you should get the following output:

python3 -V

Install Python with APT

The APT or Advanced package tool is for managing Linux software packages. The easiest way to install the Python 3 version is with this method because the Python 3 is by default included in the Ubuntu 24.04 repository.

To install Python 3 execute the following command:

sudo apt install python3

To verify the installation is successful execute the following command:

python3 -V

You should get output similar to this:

root@host:~# python3 -V
Python 3.12.3

Install Python from Source

Installing or Building the software from the source is the process of creating binaries and other non-source files. With this process, we can install the exact Python3 version that we want. With the previous method, we installed Python3 version Python 3.12.3 because it was in the default Ubuntu 24.04 repository.

Before we proceed with installation we need to install the Python3 prerequisites:

sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev pkg-config wget

At the moment we were writing the tutorial the latest stable Python3 version for Linux was 3.12.4. Let’s download that version and build it from source:

wget https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz

Once, downloaded extract the file.

tar -xf Python-3.12.4.tgz

Enter the Python directory:

tar -xf Python-3.12.4.tgz

Enable the Python optimizations with the ./configure script for faster code execution:

./configure --enable-optimizations

You will get the following output:

checking for stdlib extension module xxsubtype... yes
checking for stdlib extension module _xxtestfuzz... yes
checking for stdlib extension module _ctypes_test... yes
checking for stdlib extension module xxlimited... yes
checking for stdlib extension module xxlimited_35... yes
configure: creating ./config.status
config.status: creating Makefile.pre
config.status: creating Misc/python.pc
config.status: creating Misc/python-embed.pc
config.status: creating Misc/python-config.sh
config.status: creating Modules/Setup.bootstrap
config.status: creating Modules/Setup.stdlib
config.status: creating Modules/ld_so_aix
config.status: creating pyconfig.h
configure: creating Modules/Setup.local
configure: creating Makefile

Once the optimizations step is completed you can proceed with installing the Python 3.12.4

sudo make install

You will need to allow around 30 minutes for the process to finish. After that, you will see the following message:

Looking in links: /tmp/tmp8h_tp2p5
Processing /tmp/tmp8h_tp2p5/pip-24.0-py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-24.0

To verify the installation is successful execute the following command:

python3.12 -V

You should get output similar to this:

root@host:/opt/Python-3.12.4# python3.12 -V
Python 3.12.4

Install Python via PPA

The PPA or Personal Package Archive is the last method that we can use to install Python3. It is a third-party repository for Ubuntu that offers a couple of simple steps to install Python3. Let’s see those steps.

First, install the supporting software for PPA.

sudo apt install software-properties-common

Then add the Deadsnakes PPA repository:

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS
sudo add-apt-repository ppa:deadsnakes/ppa

Once the repo is added, update the system:

sudo apt update -y

The PPA repository allows us to install the older version we want. For example, to install Python3.11 you can simply execute the following command:

sudo apt install python3.11

To verify the installation is successful execute the following command:

python3.11 -V

You should get output similar to this:

root@host:~# python3.11 -V
Python 3.11.9

Remove Python version

To remove the Python3 version completely of your server execute the following command:

apt purge python3* -y

This command will remove the Python 3 versions installed by the PPA and APT methods. To remove the Python3 version installed from the source simply remove the folder from your server and the symbolic links:

rm -rf Python-3.12.4/

rm /usr/local/bin/python3.12

That’s it. You successfully installed Python on Ubuntu 24.04. Of course, you do not have to do this on your own. You only need to sign up for the best NVMe Ubuntu hosting and submit a support ticket. Our admins will help you with any aspect of the Python installation on your server.

Leave a Comment