How to install python3 & Python 3.12 on Ubuntu 22.04

Endrit Qerreti

Endrit Qerreti

In this tutorial, you will learn how to install python3 on Ubuntu 22.04.

Ubuntu 22.04 by default comes with python 3 installed. However, in this tutorial, we'll be installing python3 via apt package manager and via source.

The difference of installing python3 via terminal and source, is that python3 can be installed via the terminal because python3 is available on Ubuntu repositories, while when installing python3 via source you need to download the source code of python and install it manually on your machine by building it.

Install Python3 via Terminal on Ubuntu

sudo apt install python3

Like other packages, python3 will need sudo permission in order to install it. Once you run the command above, you will be prompted to authenticate before Installing Python, simply authenticate by entering sudo password and you should be able to install Python without any issues.

Install Python 3.12 via source on Ubuntu

To install Python 3.12 via source code, first you need to download the source code.

1) Download Python source

Run the command below to download Python-3.12.0.tar.xz, below we are using wget to download Python's source code, so make sure you have wget installed on your system.

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

2) Install Necessary dependencies

In order to install Python and be able to use python without issue, you must install its dependencies. To do so, simply run the command below

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

3) Extract Python source

Once you have downloaded Python source, and installed Python dependencies. Now you need to extract the content of the Python source that you downloaded.

To extract content of Python-3.12.0.tar.xz, you can use the extraction command

tar -xvf Python-3.12.0.tar.xz

4) Change directory to Python source

Now you need to enter to Python source directory, this is the directory that got extracted from Python-3.12.0.tar.xz file.

cd Python-3.12.0

5) Configure Python 3.12.0

Now that you are in the directory of Python-3.12.0, you need to run the configure command to make sure that there's no issues and that you are able to install Python on your machine.

./configure --enable-optimizations

6) Build Python source

Before installing Python, you must build the source first. To build Python source, run the command below

make -j 12

And the building process will begin

7) Install Python

Now you should be able to install Python on your machine. To do so, run the command below

sudo make altinstall

How to run Python3 & Python3.12

Once you have installed Python3 on your machine. To check if Python3 was installed correctly, you can simply run the python3 command

Python 3.10.12

python3

Python 3.12.0

python3.12

Uninstalling Python 3

To uninstall Python3 from your system, simply run the command below

sudo apt purge python3 

Uninstalling Python 3.12

To uninstall Python3.12, you need to manually remove Python3.12. To do so, run the command below

sudo rm -rf /usr/local/bin/python3.12 /usr/local/lib/python3.12

Conclusion

By now you should know how to Install Python 3.12 via the source, and via the terminal by using the default package manager on Ubuntu 22.04.

You should now be able to use Python 3.10 and Python 3.12.0 on your Ubuntu 22.04 machine.