How To Install Python 3.7 On Ubuntu 18.04 LTS

By bhagwatchouhan
How To Install Python 3.7 On Ubuntu 18.04 LTS

Python has emerged as 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.7 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.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.7. 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.7 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.7 sudo apt install python3.7

The above command will install Python 3.7 at /usr/lib/python3.7. 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.7 --version
# Check the version Python 3.7.7

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

 

Install Python using Source

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

# Refresh the Packages Index
sudo apt update
# Uninstall Python 3.7 - Installed using PPA in previous step sudo apt purge python3.7
# 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.7
sudo wget https://www.python.org/ftp/python/3.7.7/Python-3.7.7.tar.xz

Extract the tarball after the download completes.

# Extract
tar -xf Python-3.7.7.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.7
# 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 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.7 --version
# It shows version Python 3.7.7

The above commands will install the most recent version of Python 3.7, 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.7 from the command line using python3.

 

Switch Installation (Optional)

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.

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 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
# Verify Installation python3 --version
# It shows version Python 3.7.7

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

As soon as we enable the command python3 to refer Python 3.7 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.7 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.

Share this blog:

Profile picture for user bhagwatchouhan
bhagwatchouhan