Python is a high-level, interpreted programming language known for its simplicity, readability, and versatility. It is used for a wide range of applications, including web development, data analysis, artificial intelligence, scientific computing, automation, and more.
Python’s versatility and ease of use make it an ideal choice for both beginners and experienced developers. It is often recommended as a first programming language due to its simplicity and readability.
Python 3.12 is the latest stable release of the Python programming language. This tutorial illustrates two methods of how to install it on your Ubuntu 22.04 OS.
- Install Python 3.12 from the deadsnakes PPA
- Manually build Python 3.12 from the source code
Update Operating System
Update your Ubuntu 22.04 operating system to the latest version with the following command:
# apt update && apt upgrade -y
Method 1: Install Python 3.12 with APT
Installing Python 3.12 on Ubuntu 22.04 using APT is quite easy, a big thumbs up to the deadsnakes custom PPA!
This makes it easy to install Python on Ubuntu and be able to receive continued updates, bug fixes, and security updates.
Install the prerequisite for adding custom PPAs:
# apt install software-properties-common -y
Then proceed and add the deadsnakes PPA to the APT package manager sources list:
# add-apt-repository ppa:deadsnakes/ppa
Press Enter to continue.
Once the repository has been installed, run an APT update to ensure that the newly imported PPA is reflected.
# apt update
You can now install Python 3.12 with the following command:
# apt install python3.12
To verify the installation and Python 3.12 build version, perform the following:
# python3.12 --version 3.12.0
If you have installed Python 3.12 using the APT package manager, the PIP will not be installed by default. To install PIP, run the following command:
# curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12
You can check PIP for the Python 3.12 version using the following command:
# pip3.12 -V pip 23.2.1 from /usr/local/lib/python3.12/site-packages/pip (python 3.12)
Method 2: Install Python 3.12 from Source
The other alternative to get Python 3.12 installed on your Ubuntu 22.04 OS is by building it from the source code.
With this installation method, the main issue is that you cannot quickly update like the APT package manager and will need to recompile for any changes.
First, install the required prerequisite packages for the compilation of the Python 3.12 source code.
# apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
Now proceed and download the latest release version of Python from the Python official release page.
Alternatively, copy the download link for Python 3.12 gzipped tarball and use wget to pull it with the following command:
# wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz
Once done, extract the archive:
# tar -xf Python-3.12.0.tgz
Now navigate into the extracted directory and run the configure script to check the required dependencies. The –-enable optimization flag optimizes the binary by running multiple tests.
# cd Python-3.12.*/ # ./configure --enable-optimizations
Now initiate the Python 3.12 build process:
# make -j 4
Remember, the (-j) corresponds to the number of cores in your system to speed up the build time.
To find out how many cores you have on your system, execute the following code:
We have four cores, so in the (make) command, we used (-j 4).
Once the build process has been completed, run the following command to complete the Python installation on the Ubuntu 22.04 system.
The altinstall prevents the compiler to override default Python versions.
# make altinstall
Verify your installation:
# python3.12 --version Python 3.12.0
Install Python Modules|Extensions on Ubuntu 22.04
Modules and extensions can be installed on Ubuntu 22.04 using the Python Package manager (PIP).
Use the syntax below to install a Python module of choice.
# pip3.12 install module-name
In this tutorial, We will show you how to install a Python module numpy.
# pip3.12 install numpy
Collecting numpy Obtaining dependency information for numpy from https://files.pythonhosted.org/packages/e3/e2/4ecfbc4a2e3f9d227b008c92a5d1f0370190a639b24fec3b226841eaaf19/numpy-1.26.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata Downloading numpy-1.26.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (58 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 58.5/58.5 kB 687.4 kB/s eta 0:00:00 Downloading numpy-1.26.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 17.9/17.9 MB 1.2 MB/s eta 0:00:00 Installing collected packages: numpy Successfully installed numpy-1.26.0
You can verify your module installation using the following command:
# pip3.12 list Package Version ---------------------- -------------------- .............. numpy 1.26.0 ..............
Use Python 3.12 as default Python3
First, check the current default version using the below command from the terminal.
Use update-alternatives to create symbolic links to Python3:
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
And choose which one to use as Python3 via command:
sudo update-alternatives --config python3
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
Press <enter> to keep the current choice[*].
Now check the default version using the below command:
# python3 --version
That is it! You are now set to use Python 3.12 to build web applications, software development, create workflows e.t.c
Comments and Conclusion
In the tutorial, you have learned how to install Python 3.12 on Ubuntu 22.04 using APT or install it using source code.
For additional help or useful information, we recommend you to check the official Python documentation.
If you have any questions please leave a comment below.