Installing Python in Ubuntu 20.04

Why Python matters

In my recent post, I showcased how to install Python in Windows. Python is a cool programming language that is applied in machine learning. data science, desktop development and web development. One thing that makes it popular is the following:

  1. Reduced code complexity
  2. Open source - It is free.
  3. Cross-platform - Car run on multiple OSs.
  4. Modular - Has wide range of libraries/modules. To check out on how to install Python in Windows, refer to my post here

Downloading Python

There a are various methods through which one can install Python in Ubuntu 20.04 and also similar to Ubuntu 18.04. The methods for installing Python include the following:

  • Building from source.
  • Installing via repositories and personal package archives.

Check for Python installation

First step will be to check if Ubuntu has Python installed which can be done by opening the terminal and typing:

python3 --version

and output in the terminal will appear as shown below.
Image description

or if checking for Python2:

python2 --version

and just like the previous, output will appear as shown below.
Image description

Installing Python 3.8

Python version 3.8 is the most stable supported version at the time of this publication and to install it in the computer (if it does not exist), open the terminal type the following:

sudo apt install -y python3.8

which will install Python version 3.8 on after prompting for password. If it already exists, one may see such an output.
Image description
Verify the installation by checking the installed version.

python3.8 --version

Installing Python 3.9

Python version 3.9 can also be installed in a similar fashion. Open the terminal type the following:

sudo apt install -y python3.9

which installs the version3.9 and a similar approach can be used to verify the version installed.

python3.9 --version

Installing Python 3.10

Python version 3.10 has been released recently (in Oct. 4, 2021) and is the latest release candidate but not as stable as the previous. The version 3.10 comes with the following notable features:

  1. Direct type hinting of lists and dictionaries.
  2. Incorporation of new HTTP status codes.
  3. Improved support for Time Zone.
  4. Security updates.

For detailed features and enhancements, please refer to the Python 3.10 documentation
Check python 3.10 installation and if it is not installed, the following output should appear.
Image description

Option 1: Building from Source

This method guarantees the latest Python version although one may not be able to receive continued updates, bug fixes, and security updates through the Ubuntu package manager. First, let's install the required build dependencies by executing the following commands in terminal:

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

In the next step we can either manually download the latest release of Python from the Python Official Release page or use wget which we have installed in previous command. To download using wget, paste the following command in the terminal to download Python in the computer.

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

Once downloaded, proceed to extract the contents using tar.

tar -xf Python-3.10.*.tgz

Navigate into the extracted folder using the change directory cd command. The next commands will be run inside this directory.

cd Python-3.10.1

The next step will be to check and configure required dependencies and optimize the binaries by running multiple tests. Hopefully, no error should arise.

./configure --enable-optimizations

The configuration process appears in terminal (in part) as shown below.
Image description

configure script is in charge of making the software ready to build on your individual system.
It double-checks that all of the dependencies required for the rest of the build and install process are there, and it learns everything it needs to know about those dependencies.
--enable-optimizations sets the default make targets up to enable Profile Guided Optimization (PGO) and may be used to auto-enable Link Time Optimization (LTO).Reference
To build Python 3.10, run the following command:

make -j 4

where the number 4 represents the number of CPU cores in the system therefore, it is important that one knows the core number before running this command. To check the number of cores in the computer, run the following command.

nproc

and the output will be a number representing the cores.
After the build process is complete, the next step will be installing Python using make.
altinstall ensures that other already installed versions of Python are not overwritten as it would have been the case if install is used in place of it.

sudo make altinstall

The installation process log appears as shown below.
Image description
The final step is to verify the installed Python 3.10 via the command.

python3.10 --version

Below is an output for checking Python versions 3.8, 3.9 and 3.10 which are already installed.
Image description

Option 2: Installing using Apt Repository

This is an alternative method to option 1 and simpler. I decided to tackle the more detailed process first. To install Python 3.10 via apt requires adding (Personal Package Archive) PPA to the Ubuntu list of repositories as it not currently (by default) in Ubuntu repo.
We will make use of the deadsnakes PPA. Read more about Ubuntu PPAs.
To add PPA in Ubuntu, we have to have installed software-properties-common dependency using the following.

sudo apt install software-properties-common

Next, add the deadsnakes PPA and click enter key to continue

sudo add-apt-repository ppa:deadsnakes/ppa

and the process (in part) appears as shown below.
Image description
Refer to the deadsnakes repo.
The final step is installing Python 3.10 using the usual apt install

sudo apt install -y python3.10

and now the latest Python has been installed and ready to be used.
To verify installation

python3.10 --version

OR use this bonus command

whereis python3.10

You can refer to the repository link below for automated installation of Python 3.10.
Build Python 3.10 from Source

32