February 28, 2021
How to install Python 3.9 on Ubuntu 20.04 LTS

How To Install Python 3.9.1 On Ubuntu 20.04 LTS

On this article we will learn how to install Python 3.9 on Ubuntu 20.04 operating system.

Introduction

Python is one of the world’s most popular choice for beginners and experienced developers. We can build all kinds of applications, from simple scripts to complex machine learning algorithms. On this tutorial we will discus how to install the last stable Python, version 3.9.1 on Ubuntu 20.04 LTS. The are two methods Python 3.9 installation, namely : using APT and Building Pyhton from the source.

Python 3.9.1 Installation on Ubuntu 20.04 LTS with APT

On the first option, we will install Python 3.9 by using package from the deadsnakes PPA (Personal Package Archives (PPA). The steps are as follow :

1. Update the system and install prerequisites packages

sudo apt update

ramans@diginet-app02:~$ sudo apt update
 [sudo] password for ramans: 
 Hit:1 http://us.archive.ubuntu.com/ubuntu focal InRelease     
 Get:2 http://security.ubuntu.com/ubuntu focal-security InRelease [109 kB]
 Get:3 http://us.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
 Get:4 http://us.archive.ubuntu.com/ubuntu focal-backports InRelease [101 kB]
 Get:5 http://security.ubuntu.com/ubuntu focal-security/main i386 Packages [171 kB]
 Get:6 http://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [724 kB]
 Get:7 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [417 kB]        

sudo apt install software-properties-common

ramans@otodiginet:~$ sudo apt install software-properties-common
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following additional packages will be installed:
   python3-software-properties software-properties-gtk
 The following packages will be upgraded:
   python3-software-properties software-properties-common software-properties-gtk
 3 upgraded, 0 newly installed, 0 to remove and 376 not upgraded.
 Need to get 99.9 kB of archives.
 After this operation, 0 B of additional disk space will be used.
 Do you want to continue? [Y/n] Y

2. Adding Deadsnakes PPA to system system list

Python 3.9.1 has been bundled on the Deadsnakes PPA, so we have to add this PPA to user system by submitting command line : sudo add-apt-repository ppa:deadsnakes/ppa.

ramans@diginet-app02:~$ sudo add-apt-repository ppa:deadsnakes/ppa
 This PPA contains more recent Python versions packaged for Ubuntu.
 Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.
 Update Note
 Please use this repository instead of ppa:fkrull/deadsnakes.
 Reporting Issues
 Issues can be reported in the master issue tracker at:
 https://github.com/deadsnakes/issues/issues
 Supported Ubuntu and Python Versions
 Ubuntu 16.04 (xenial) Python 2.3 - Python 2.6, Python 3.1 - Python3.4, Python 3.6 - Python3.9
 Ubuntu 18.04 (bionic) Python2.3 - Python 2.6, Python 3.1 - Python 3.5, Python3.7 - Python3.9
 Ubuntu 20.04 (focal) Python3.5 - Python3.7, Python3.9
 Note: Python2.7 (all), Python 3.5 (xenial), Python 3.6 (bionic), Python 3.8 (focal) are not provided by deadsnakes as upstream ubuntu provides those packages.
 Note: for focal, older python versions require libssl1.0.x so they are not currently built 
 The packages may also work on other versions of Ubuntu or Debian, but that is not tested or supported.
Addig Deadsnakes PPA to our system.
Addig Deadsnakes PPA

3. Install Python 3.9.1 On Ubuntu 20.04 LTS

After adding all prerequisite libraries, then we will install the Python 3.9 by submitting command line : sudo apt install python3.9.

ramans@diginet-app02:~$ sudo apt install python3.9
 Reading package lists… Done
 Building dependency tree       
 Reading state information… Done
 The following packages were automatically installed and are no longer required:
   libeclipse-jdt-core-java libfprint-2-tod1 libllvm9 libtcnative-1 linux-headers-5.4.0-26 linux-headers-5.4.0-26-generic
   linux-image-5.4.0-26-generic linux-modules-5.4.0-26-generic linux-modules-extra-5.4.0-26-generic
 Use 'sudo apt autoremove' to remove them.
 The following additional packages will be installed:
   libpython3.9-minimal libpython3.9-stdlib python3.9-minimal
 Suggested packages:
   python3.9-venv python3.9-doc binutils binfmt-support
 The following NEW packages will be installed:
   libpython3.9-minimal libpython3.9-stdlib python3.9 python3.9-minimal
 0 upgraded, 4 newly installed, 0 to remove and 9 not upgraded.
 Need to get 4,819 kB of archives.
 After this operation, 19.2 MB of additional disk space will be used.
 Do you want to continue? [Y/n] Y

4. Verify Python Version

After Python 3.9 installation was completed done, then we will verify the installation result by querying its version. This task is done by submitting command line : python3.9 --version.

ramans@diginet-app02:~$ python3.9 --version
 Python 3.9.1
Python 3.9.1
Python 3.9.1

Python 3.9 Installation From The Source

Another way to install python 3.9 is to compile Python 3.9 from the available source files. The installation process begins with download the latest release’s source code from the Python download page. The installation process is described in the steps below :

1. Install the dependencies files to build Python

As usual before installing a new application on the Ubuntu Linux operating system, we must first update the system.

sudo apt update

sudo apt update
Update system

And then add the required files for Python 3.9 installation purposes.

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

Install Python dependencies files
Install dependencies files

2. Download the latest Python stable source code

On this sub section we will download the latest Python stable source code from the Python download page, by using wget command line. The source file can be downloaded in Python official website.

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

Download Python 3.9.1 source file
Download Python 3.9.1 source file

3. Extract the Python 3.9.1 gzipped archive

At this point, we will extract the gzipped archive of Python 3.9.1 and move into the python directory.

tar -xf Python-3.9.1.tgz
cd Python-3.9.1

Extracting Python 3.9.1 archive
Extracting Python 3.9.1 archive

4. Run the configure script

We will check to make sure all of the dependencies to support Python 3.9.1 installation are already exist on the system.

./configure --enable-optimizations

Checking Python 3.9.1 dependencies
Checking Python 3.9.1 dependencies

5. Build the Python 3.9.1

After all done, we will build the Python 3.9.1 by submitting command line :

make -j 12

building python 3.9.1 (make -j 12)
make -j 12

6. Install the Python 3.9.1

The installation process itself is by submitting command line :

sudo make altinstall

Python 3.9.1 installation (sudo makealinstall)
Python 3.9.1 installation

7. Verify the Python 3.9.1 installation

This is the last step on Python 3.9.1 installation process, we will verify if Python 3.9.1 has been installed on the system.

python --version

Python 3.9.1 version
Python 3.9.1 version

Conclussion

We have learn how to install Python 3.9.1 (the last stable version) on Ubuntu 20.04 LTS. There are two Python 3.9.1 installation option : using apt and building from source. What’s the next? learn much more about Python programming language and make some cool applications. Have a nice day and stay safe.

Share this article via :

Leave a Reply

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