Provides all the steps required to install the latest Python on Ubuntu 18.04 LTS using either PPA or source code.

How To Install Python 3.7 On Ubuntu 18.04 LTS
How To Install Python 3.7 On Ubuntu 18.04 LTS
September 11, 2019

Python has emerged as one of the major programming language 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 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.

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.15+

We can see that Python 2.7 gets installed using the Ubuntu repositories which seems outdated as compared to the most recent version oh Python i.e. Python 3.7. You can continue with the next sections to install the most recent version of Python either using the PPA or build from source. You may wish to remove Python completely before moving ahead with the next sections using the commands as shown below.

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

Install Python using PPA

Install Python 3.7 using the commands as shown below. In several cases, it's not preferred to install Python using PPA. In such 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.7
sudo apt install python3.7

Now verify the installation using the commands as shown below.

# Check Python version
python3.7 --version

# Check the version
Python 3.7.4

You can follow the same steps to install Python 3.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.8

Install Python using Source

In this section, we will install Python 3.7 using the source code.

# 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.7 using wget as shown below.

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

Extract the tarball after the download completes.

# Extract
tar -xf Python-3.7.4.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.7.4

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

# Make - Compile and build Python
sudo make
# OR - specify processor units
sudo make -j 4

# Install Binaries
sudo make altinstall

Switch Installation

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

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

# Add python3 choice for python3.7
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 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.7 as shown in Fig 1.

Python on Ubuntu

Fig 1

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

Write a Comment

Click on the captcha image to get new code.
Discussion Forum by DISQUS