Jun 5, 2024 6 min read

How to Install Python 3.12 on Ubuntu 22.04

Install Python 3.12 on Ubuntu 22.04 with our step-by-step tutorial. Python is a known high-level language appreciated for its readability.

Install Python 3.12 on Ubuntu 22.04
Install Python 3.12 on Ubuntu 22.04
Table of Contents

Choose a different version or distribution

Introduction

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

Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. It is widely used for web development, data analysis, artificial intelligence, and more.

Python's syntax is easy to learn, making it a popular choice for beginners and experienced developers alike. With its vast range of libraries and frameworks, Python enables rapid development and deployment of applications.

In this tutorial, you will install Python 3.12 on Ubuntu 22.04. We will also address a few FAQs on how to install Python 3.12 on Ubuntu 22.04.

Advantages of Python 3.12

  1. Enhanced f-strings: Allows more flexibility with multi-line expressions and comments.
  2. Improved error messages: Provides helpful suggestions and guidance for common errors.
  3. Faster performance: Optimizes code for improved execution speed.
  4. New syntax for type variables: Simplifies type annotations with a new syntax.
  5. Support for the Linux perf profiler: Enables detailed performance monitoring with minimal overhead.

Update Operating System

Run the following command to update your Ubuntu 22.04 operating system to the most recent version:

apt update && apt upgrade -y

Method 1: Install Python 3.12 with APT

APT makes installing Python 3.12 on Ubuntu 22.04 very simple; this is a great credit to the deadsnakes custom PPA.  This facilitates the installation of Python on Ubuntu and enables it to get regular updates, security updates, and bug fixes. Install this prerequisite so that custom PPAs can be added:

apt install software-properties-common -y

Next, add the deadsnakes PPA to the list of sources in the APT package manager:

add-apt-repository ppa:deadsnakes/ppa
Deadsnakes PPA on Ubuntu

To proceed, hit Enter.

Run an APT update after installing the repository to make sure the recently imported PPA is reflected.

apt update

You can now use the following command to install Python 3.12:

apt install python3.12

Take the following actions to confirm the installation and Python 3.12 build version:

python3.12 --version

Output-

The PIP won't be installed by default if Python 3.12 was installed via the APT package manager. Execute the following command to install PIP:

curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12 

With the following command, you can verify PIP for Python 3.12:

pip3.12 -V 

Output-

Method 2: Install Python 3.12 from Source

Installing Python 3.12 on your Ubuntu 22.04 operating system can also be accomplished by building it from the source code.

The primary problem with this installation method is that any changes require a recompile because you cannot update as quickly as with the APT package manager.

Installing the prerequisite packages is the first step in compiling the source code for Python 3.12.

apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

Now go ahead and get the most recent Python release version from the official Python release page.

As an alternative, copy the Python 3.12 gzipped tarball download link and use wget to pull it using the given command:

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

After that, extract the archive:

tar -xf Python-3.12.0.tgz

In order to verify the necessary dependencies, navigate to the extracted directory and launch the configure script. Multiple test runs are performed to optimize the binary by using the –-enable optimization flag.

cd Python-3.12.*/
./configure --enable-optimizations

Now start the build process for Python 3.12:

make -j 4

To reduce the build time, keep in mind that the -j represents the number of cores in your system.

Run the following code to determine the number of cores on your system:

nproc
Output

4

Since we have four cores, we used -j 4 in the make command. After the build is finished, execute the next command to finish installing Python on the Ubuntu 22.04 system. The compiler cannot override the default Python versions thanks to altinstall.

make altinstall

Verify the installation:

python3.12 --version
Output

Python 3.12.0

Install Python Modules|Extensions on Ubuntu 22.04

On Ubuntu 22.04, modules and extensions can be installed via the Python Package Manager (PIP). To install your preferred Python module, use the syntax shown below.

pip3.12 install module-name

We will walk you through installing the numpy Python module in this tutorial.

pip3.12 install numpy

Output-

You can use the following command to confirm that your module is installed:

pip3.12 list

Output-

Use Python 3.12 as default Python3

Use the terminal to run the command below to first verify the most recent default version.

python3 --version
Output

Python 3.10.12

To make symbolic links to Python3, use update-alternatives:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.12 2

Using the command, select which one to use as Python 3:

sudo update-alternatives --config python3
Output

Selection    Path                        Priority   Status
------------------------------------------------------------
* 0           /usr/local/bin/python3.12     2        auto mode
  1           /usr/bin/python3.10           1        manual mode
  2           /usr/local/bin/python3.12     2        manual mode

To maintain the current selection, hit [*].

Now use the command below to verify the default version:

python3 --version

Output-

And that's it!  You can now use Python 3.12 to develop software, build web applications, establish workflows, and more.

FAQs to Install Python 3.12 on Ubuntu 22.04

Is Python 3.12 available in the default Ubuntu 22.04 repository?

No, Python 3.12 is not available in the default Ubuntu 22.04 repository. You need to add a PPA (Personal Package Archive) to install it.

How do I add the PPA for Python 3.12 on Ubuntu 22.04?

You can add the PPA for Python 3.12 using the following command: sudo add-apt-repository ppa:deadsnakes/ppa.

What are the dependencies required to install Python 3.12 on Ubuntu 22.04?

The dependencies required to install Python 3.12 on Ubuntu 22.04 are build-essential, libssl-dev, and libffi-dev.

Can I install multiple versions of Python on Ubuntu 22.04?

Yes, you can install multiple versions of Python on Ubuntu 22.04. However, you need to manage them manually.

How do I set the default Python version on Ubuntu 22.04?

You can set the default Python version on Ubuntu 22.04 by running the command sudo update-alternatives --set python /usr/bin/python3.12.

Are there any known issues with installing Python 3.12 on Ubuntu 22.04?

Yes, there are known issues with installing Python 3.12 on Ubuntu 22.04. For example, some packages may not be compatible with Python 3.12.

How do I uninstall Python 3.12 on Ubuntu 22.04?

You can uninstall Python 3.12 on Ubuntu 22.04 by running the command sudo apt-get purge python3.12.

Conclusion

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

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.

Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to DevOps Tutorials - VegaStack.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.