Article

Table of Contents
Theme:
Was this article helpful?
Try Vultr Today with

$50 Free on Us!

Want to contribute?

You could earn up to $600 by adding new articles!

Install the Latest Version of Python from Source on Ubuntu 20.04

Author: Tung Nguyen

Last Updated: Mon, Oct 4, 2021
Python Ubuntu

Introduction

Ubuntu 20.04 comes with Python 3.8 pre-installed. However, you can install another version of Python 3 if you want. This guide explains how to install the latest version of Python from source on Ubuntu 20.04.

Prerequisites

1. Install Required Packages

Building Python from source requires various third-party and system libraries.

  1. Log in to the server as a non-root sudo user via SSH.
  2. Open the /etc/apt/sources.list file.

    $ sudo nano /etc/apt/sources.list
    
  3. To enable the source packages, add their location, including URL, distribution name, and component name.

    deb-src http://archive.ubuntu.com/ubuntu/ focal main
    
  4. Save the file and exit.

  5. Update the packages index.

    $ sudo apt update
    
  6. Install the required system libraries for Python.

    $ sudo apt -y build-dep python3
    
  7. Install the third-party libraries for all optional modules.

    $ sudo apt -y install gdb lcov libbz2-dev libffi-dev libgdbm-dev \
        libgdbm-compat-dev liblzma-dev libncurses5-dev libreadline6-dev \
        libsqlite3-dev libssl-dev lzma lzma-dev tk-dev uuid-dev zlib1g-dev
    

2. Build Python from Source

  1. Download the source archive to the home directory.

    $ cd ~
    $ wget https://github.com/python/cpython/archive/refs/tags/v3.9.7.tar.gz
    

    At the time of writing, the latest stable version of Python is 3.9.7. But, of course, you can always visit the Python releases page on GitHub to get the latest version.

  2. Extract the archive.

    $ tar xzf v3.9.7.tar.gz
    
  3. Change the working directory to the source directory.

    $ cd cpython-3.9.7
    
  4. Configure the source.

    $ ./configure --enable-optimizations
    

    The --enable-optimizations flag actives all stable optimizations.

  5. Build the source (this may take a few minutes).

    $ make -s
    

    The -s option tells make to print only warnings and errors.

When finished, you have a Python 3.9 binary suitable for a production installation.

3. Install Python

Installing Python 3.9 side by side with Python 3.8 is better than overwriting Python 3.8 because it allows existing programs that are not compatible with the new version to continue to work.

$ sudo make altinstall

Using make altinstall instead of make install avoids overwriting Python 3.8 with Python 3.9.

Python 3.8 is the default Python version. However, you still use it the same way as before, by invoking python3, python3.8, or their absolute paths /usr/bin/python3 and /usr/bin/python3.8.

$ python3 -V
$ python3.8 -V

Both give the same output like this.

Python 3.8.10

To use Python 3.9, invoke python3.9 or its absolute paths /usr/local/bin/python3.9 instead.

$ python3.9 -V

The result is similar to this.

Python 3.9.7

Python 3.9 also comes with pip, the package installer for Python.

$ python3.9 -m pip -V

The output should be like this.

pip 21.2.3 from /usr/local/lib/python3.9/site-packages/pip (python 3.9)

If you ensure that all existing programs are compatible with Python 3.9, you can make it the default version.

$ sudo ln -fs /usr/local/bin/python3.9 /usr/bin/python3

References

Want to contribute?

You could earn up to $600 by adding new articles