How to Install Python 3.12 on Ubuntu 24.04

Choose a different version or distribution

Introduction

Before we begin talking about how to install Python 3.12 on Ubuntu 24.04, let's briefly understand – What is Python 3.12?

Python 3.12 is the latest major version of the popular programming language. This version brings many improvements, including enhanced performance, expanded type hinting capabilities, new language features like pattern matching, and updates to the standard library.

Python 3.12 focuses on optimizing the core interpreter, improving compatibility with existing code, and providing developers with more powerful tools and functionality. These enhancements make Python 3.12 an attractive choice for a wide range of programming tasks, from web development to data analysis and beyond.

In this tutorial, you will install Python 3.12 on Ubuntu 24.04 using the PPA repository and office setup file. We will also address a few FAQs on how to install Python 3.12 on Ubuntu 24.04.

Advantages of Python 3.12

  1. Enhanced Performance: Optimizations to the core interpreter result in faster execution times.
  2. Improved Type Hinting: Expanded type annotation capabilities, including better support for typing **kwargs.
  3. New Language Features: Includes pattern matching and other syntax improvements for cleaner, more expressive code.
  4. Expanded Standard Library: Updates and additions provide more built-in functionality for developers.
  5. Better Compatibility: Focus on maintaining compatibility ensures a smooth upgrade path for existing Python codebases.

Prerequisites

  • A server operating on Ubuntu 24.04 OS
  • User privileges that can be either root or a non-root user with sudo access.

Update the System

Prior to commencing the installation, it's advisable to update the system packages to ensure they are at their latest versions.

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

Check previous Python versions

To verify whether Python3 is installed on your server, run the following command:

python3 -V

If Python3 is not installed on your system, you should receive the following output:

python3 -V

Install Python with APT

APT, the Advanced Package Tool, manages Linux software packages. The most straightforward method to install Python 3 is through APT, as Python 3 is included in the default Ubuntu 24.04 repository.

To install Python 3, run the following command:

sudo apt install python3

To confirm the installation was successful, run the following command:

python3 -V

You should get output similar to this:

Python3 Version after installation

Install Python from Source

Installing or building software from its source involves creating binaries and other non-source files. This method allows us to install a specific version of Python 3 that we desire. In the previous method, we installed Python 3 version 3.12.3 because it was available in the default Ubuntu 24.04 repository.

Before proceeding with the installation, we need to install the Python 3 prerequisites:

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

At the time of writing this tutorial, the latest stable version of Python 3 for Linux was 3.12.4. Let's download and build that version from the source:

wget https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz
downloading python3 source archive

Once, downloaded, extract the file.

tar -xf Python-3.12.4.tgz

Enter the Python directory:

cd Python-3.12.4

Activate Python optimizations using the ./configure script to enhance code execution speed:

./configure --enable-optimizations
Configuring for installation

You will get the following output:

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

After completing the optimization step, proceed to install Python 3.12.4.

sudo make install
python3 installation process using make command

Please allocate approximately 30 minutes for the process to complete. Afterward, you will encounter the following message:

Output

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 confirm that the installation was successful, run the following command:

python3.12 -V
python3.12 version

You should get output similar to this:

Output

Python 3.12.4

Install Python via PPA

The PPA (Personal Package Archive) is another method available for installing Python3. It's a third-party repository for Ubuntu that provides straightforward steps to install Python3. Let's go through these steps.

First, install the necessary software to support the PPA.

sudo apt install software-properties-common

Then add the Deadsnakes PPA repository:

sudo add-apt-repository ppa:deadsnakes/ppa
adding deadsnakes ppa for python

Once the repo is added, update the system:

sudo apt update -y

The PPA repository enables us to install specific older versions as needed. For instance, to install Python 3.11, execute the following command:

sudo apt install python3.11
python3.11 installation

To confirm that the installation was successful, run the following command:

python3.11 -V

You should get output similar to this:

Output

Python 3.11.9

Remove Python version

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

apt purge python3* -y

This command will uninstall Python 3 versions installed via both the PPA and APT methods. To remove a Python 3 version installed from source, delete the corresponding folder and any symbolic links from your server.

rm -rf Python-3.12.4/ rm /usr/local/bin/python3.12

That's all there is to it! Python has been successfully installed on Ubuntu 24.04.

FAQs to Install Python 3.12 on Ubuntu 24.04

How do I verify that Python 3.12 is properly installed on Ubuntu 24.04?

After installation, you can verify the installation by running python3.12 --version in the terminal. It should display the version as "Python 3.12.x"

Can I install Python 3.12 using the apt-get command on Ubuntu 24.04?

No, you cannot install Python 3.12 using the apt-get command directly, as it is not available in the default Ubuntu repositories.

What are the recommended system requirements for installing Python 3.12 on Ubuntu 24.04?

The recommended system requirements include:

  • Ubuntu 24.04 LTS
  • At least 2GB of RAM
  • At least 2GB of available disk space

Can I install Python 3.12 alongside an existing Python version on Ubuntu 24.04?

Yes, you can install Python 3.12 alongside an existing Python version on Ubuntu 24.04. The new installation will not replace the existing version.

What is the difference between python3 and python3.12 commands? 

The python3 command typically points to the default Python 3 version installed on the system, while python3.12 specifically refers to version 3.12.

Can I use virtualenv with Python 3.12 on Ubuntu 24.04? 

Yes, you can use virtualenv with Python 3.12. Create a virtual environment with python3.12 -m venv myenv and activate it with source myenv/bin/activate.

Are there any known issues or limitations with Python 3.12 on Ubuntu 24.04?

There are no major known issues with Python 3.12 on Ubuntu 24.04. However, as with any software update, it's always a good idea to thoroughly test your applications and dependencies before upgrading to ensure compatibility.

Conclusion

We hope this detailed tutorial helped you understand how to install Python 3.12 on Ubuntu 24.04.

If you have any suggestions or queries, kindly leave them in the comments section.