How to Install Python 3.10 on Ubuntu 20.04

How To Install Python 3.10 on Ubuntu 20.04

Python is an open-source high-level programming language with a large community. It is often used for machine learning, deep learning, computer vision, and data analysis.

The latest stable release version of Python is 3.10.1 released on December 6, 2021, and it contains many new features and optimizations.

This tutorial illustrates two methods of how to install Python 3.10 on the Ubuntu 20.04 system.

  • Install Python 3.10 from the deadsnakes PPA
  • Manually build Python 3.10 from the source code

Update Operating System

Update your Ubuntu 20.04 operating system to make sure all existing packages are up to date:

$ sudo apt update && sudo apt upgrade -y

Method 1: Install Python 3.10 with APT

Installing Python 3.10 on Ubuntu 20.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:

$ sudo apt install software-properties-common -y

Then proceed and add the deadsnakes PPA to the APT package manager sources list:

$ sudo add-apt-repository ppa:deadsnakes/ppa
Python 3.10.1
Install Python 3.10 on Ubuntu 20.04

Press Enter to continue.

Once the repository has been installed, you can now install Python 3.10 with the following command:

$ sudo apt install python3.10

To verify the installation and Python 3.10 build version, perform the following:

$ python3.10 --version
3.10.1

Method 2: Install Python 3.10 from Source

The other alternative to get Python 3.10 installed on your Ubuntu 20.04 system 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.10 source code.

$ sudo 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.10 gzipped tarball and use Wget to pull it with the following command:

$ sudo wget https://www.python.org/ftp/python/3.10.1/Python-3.10.1.tgz

Once done, extract the archive:

$ sudo tar -xf Python-3.10.*.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.10.*/
./configure --enable-optimizations

Now initiate the Python 3.10 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:

$ nproc

Output:

4

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 20.04 system.

The altinstall prevents the compiler to override default Python versions.

$ sudo make altinstall

Verify your installation:

$ python3.10 --version
Python 3.10.1

Install Python Modules|Extensions on Ubuntu 20.04.

Modules and extensions can be installed on Ubuntu 20.04 using the Python Package manager (PIP).

You can check PIP for the Python 3.10 version using the following command:

$ pip3.10 -V

pip 21.2.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)

Then use the syntax below to install a Python module of choice.

$ sudo pip3.10 install module-name

In this tutorial, We will show you how to install a Python module babel.

$ sudo pip3.10 install babel

Output:

Collecting babel
Downloading Babel-2.9.1-py2.py3-none-any.whl (8.8 MB)
|████████████████████████████████| 8.8 MB 5.2 kB/s
Collecting pytz>=2015.7
Downloading pytz-2021.3-py2.py3-none-any.whl (503 kB)
|████████████████████████████████| 503 kB 6.0 MB/s
Installing collected packages: pytz, babel
Successfully installed babel-2.9.1 pytz-2021.3

You can verify your module installation using the following command:

$ pip3.10 list
Package                Version             
---------------------- --------------------
..............
Babel 2.9.1
..............

Use Python 3.10 as default Python3

First, check the current default version using the below command from the terminal.

python3 --version

Output:

Python 3.8.10

Use update-alternatives to create symbolic links to python3

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/local/bin/python3.10 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.10     2        auto mode
  1           /usr/bin/python3.8            1        manual mode
  2           /usr/local/bin/python3.10     2        manual mode

Press <enter> to keep the current choice[*].

Now check the default version using the below command:

python3 --version

Output:

Python 3.10.1

That is it!  You are now set to use Python 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.10 on Ubuntu 20.04 using APT or install it using source code.

Leave a Reply

Your email address will not be published. Required fields are marked *