How To Install Python 3.8 On Ubuntu 18.04 LTS

How To Install Python 3.8 On Ubuntu 18.04 LTS

Provides all the steps required to install the latest Python on Ubuntu 18.04 LTS using either PPA or source code. It also provide the program to print Hello World on the Console using Python.

March 22, 2020

Python is one of the major programming languages used to develop different types of applications including Web Applications, Desktop Applications, Numeric and Scientific Applications, etc. It soon entered into mainstream programming and majorly popular in data science engineers.

In this tutorial, we will discuss all the steps required to install Python 3 i.e Python 3.8 on the popular Linux distribution Ubuntu. It provides the steps for Ubuntu 18.04 LTS. The steps should be the same for other distributions of Linux.

You might also be interested in Python Cheatsheet to learn or brush up the basics of Python.

Install Python

We can install the Python using the package available at the Ubuntu repositories using the commands as shown below.

# Refresh the packages index
sudo apt update

# Prerequisites
sudo apt install python

# Verify Installation
python --version

# It shows version
Python 2.7.17

# Prerequisites
sudo apt install python3

# Verify Installation
python3 --version

# It shows version
Python 3.6.9

We can see that Python 2.7 and Python 3.6 get installed using the Ubuntu repositories which seems outdated as compared to the most recent version of Python i.e. Python 3.8. You can continue with the next sections to install the most recent version of Python either using the PPA or build from source. Optionally, you may wish to remove Python completely before moving ahead with the next sections using the commands as shown below. Make sure before removing existing Python 2.7 and Python 3.6 since several other packages and programs depend on it. If you are not sure, keep the existing version of Python installed on your system since we can install multiple versions of Python on the same system.

# Uninstall Python - Make sure before going for it
# It will also remove dependent packages including gimp, mysql etc
sudo apt purge python
sudo apt purge python3

Install Python using PPA

Install Python 3.8 using the commands as shown below. In several cases, it's not preferred to install Python using PPA. In such a case, you may follow the next section to install it from the source code.

# Refresh the packages index
sudo apt update

# Install Prerequisites
sudo apt install software-properties-common

# Add deadsnakes PPA to sources
sudo add-apt-repository ppa:deadsnakes/ppa

# Press Enter to continue

# Install Python 3.8
sudo apt install python3.8

The above command will install Python 3.8 at /usr/lib/python3.8. The default version of Python 3 remains installed at /usr/lib/python3. Now verify the installation using the commands as shown below.

# Check Python version
python3.8 --version

# Check the version
Python 3.8.2

You can follow the same steps to install Python 3.6, Python 3.7 using the same PPA. It won't override the Python 3.6 installed by us using the command sudo apt install python3 and installs it at a separate location i.e. /usr/lib/python3.6. In this way, we can have multiple versions of Python installed on the same system.

# Check Python version
python3.6 --version

# Check the version
Python 3.6.9

# Check Python version
python3.7 --version

# Check the version
Python 3.7.7

Install Python using Source

In this section, we will install Python 3.8 using the source code without using PPA as shown in the previous step.

# Refresh the Packages Index
sudo apt update

# Uninstall Python 3.8 - Installed using PPA in previous step
sudo apt purge python3.8

# Refresh the Packages Index
sudo apt update

# Install Prerequisites
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

Now download the latest release of Python 3.8 using wget as shown below.

# Download Python 3.8.2
sudo wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tar.xz

Extract the tarball after the download completes.

# Extract
tar -xf Python-3.8.2.tar.xz

Now compile and build the Python from source and install it using the commands as shown below. The make command will take some time to build the binaries from the source files. Also, use altinstall option to avoid overwriting the existing installation.

# Change directory
cd <path to download location>/Python-3.8.2

# Check dependencies
sudo ./configure --enable-optimizations

# Make - Compile and build Python - It will take some time, have a coffee or tea break
sudo make
# OR - specify processor units
sudo make -j 4

# Install Binaries
sudo make altinstall

# Switch active Python
sudo update-alternatives --config python3

# It won't show any option for python3

# Verify Installation
python3 --version

# It shows version
Python 3.6.9

# Verify Installation
python3.8 --version

# It shows version
Python 3.8.2

The above commands will install the most recent version of Python 3.8, but won't enable it to be used using the command python3 from the console as shown above. The next section explains the steps to access Python 3.8 from the command line using python3.

Switch Installation (Optional)

If you have installed multiple minor versions of Python i.e. python3.6, python3.7, python3.8, etc, you can use the below mentioned commands to activate one installation at a time to use python3 instead of using python3.6, python3.7, or python3.8 on the command line. We can also switch to other versions by configuring the active command.

Notes: Configuring python3 to use the python installed using the PPA or source will disable the python3 installed using the command sudo apt install python3. You may use a different command instead of python3 to avoid any system discrepancy.

# Add python3 choice using python3.6
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1

# Add python3 choice using python3.8
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 2

# Verify Installation
python3 --version

# It shows version
Python 3.8.2

Since we have provided the choices to switch between multiple versions of python3, we can switch the active version using the command as shown below.

# Switch active Python
sudo update-alternatives --config python3

I have used my local installation of Python 3.8 as shown in Fig 1.

Switch Python

Fig 1

As soon as we enable the command python3 to refer Python 3.8 installed by us using the source code, the terminal stops working. So make sure to fix the terminal before closing it as shown below.

# Update Terminal Script
sudo nano /usr/bin/gnome-terminal

# Replace the first line to use python3.6
#! /usr/bin/python3.6

Apart from breaking the terminal, you will also get an error of missing apt_pkg while running the command sudo apt-get update as shown below.

ubuntu@ubuntu:~$ sudo apt-get update
Hit:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease                             
Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]                                           
Hit:3 http://us.archive.ubuntu.com/ubuntu bionic InRelease                                                 
Get:4 http://us.archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]        
Get:5 http://us.archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]
Fetched 252 kB in 4s (62.9 kB/s)   
Traceback (most recent call last):
  File "/usr/lib/cnf-update-db", line 8, in <module>
    from CommandNotFound.db.creator import DbCreator
  File "/usr/lib/python3/dist-packages/CommandNotFound/db/creator.py", line 11, in <module>
    import apt_pkg
ModuleNotFoundError: No module named 'apt_pkg'
Reading package lists... Done
E: Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/lib/command-not-found/ -a -e /usr/lib/cnf-update-db; then /usr/lib/cnf-update-db > /dev/null; fi'
E: Sub-process returned an error code

The apt_pkg can be fixed using the commands as shown below.

# Navigate to default Python 3 
cd /usr/lib/python3/dist-packages/

# Fix apt_pkg
sudo ln -s apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

You can also fallback and fix the python3 command as shown below.

# Remove python3 link
sudo rm /usr/bin/python3

# Fallback to python3.6
sudo ln -s /usr/bin/python3.6 /usr/bin/python3

# Update Terminal Script
sudo nano /usr/bin/gnome-terminal

# Replace the first line to use python3
#! /usr/bin/python3

You can use some other short name instead of python3 or simply use python3.8 to access the most recent version of Python(installed from source) from the terminal.

This is how we can install the latest Python on Ubuntu 18.04 LTS. We have also checked how to install and switch among the multiple versions of Python installed on the same system.

Hello World

In this section, we will write our first program in Python using the nano editor.

>sudo mkdir -p /data/programs/python
>cd /data/programs/python
>sudo nano helloworld.py

Now write the first program in Python as shown below, save the program by pressing Ctrl + O and hit Enter and exit the editor by pressing Ctrl + X.

# Print Hello World
print("Hello World !!")

Use the python3 installed by us to execute the program as shown below.

# Execute the program
python3 helloworld.py
# OR
python3.8 helloworld.py

# Program output
Hello World !!

These are the basic steps to write and execute Python programs.

Summary

This tutorial provided the steps to install Python 3.8 on Ubuntu 18.04 LTS and showed the process to manage multiple installations of Python on the same system.

Write a Comment
Click the captcha image to get new code.
Discussion Forum by DISQUS