Installing the Latest Python Version from Source on Ubuntu 20.04

Ubuntu 20.04 comes with Python 3.8 already installed by default. Nonetheless, if you wish to use a more recent Python 3 version, it is possible to compile it manually. This guide outlines the steps to build and install the most recent Python release from source on an Ubuntu 20.04 system.

Prerequisites

  • An Ubuntu 20.04 server instance.
  • Configure a sudo-enabled user and update your system.

1. Install Required Dependencies

To compile Python from source, you need to install a number of system and external libraries.

Start by accessing the server through SSH using a sudo-capable user (non-root).

Edit the sources list file:

$ sudo nano /etc/apt/sources.list

Add the following entry to enable source packages:

deb-src http://archive.ubuntu.com/ubuntu/ focal main

Save and close the file, then update the package list:

Install the necessary build dependencies:

$ sudo apt -y build-dep python3

Install optional third-party libraries:

$ 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. Compile Python from Source

Begin by downloading the Python source archive to your home directory:

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

At the time this guide was written, Python 3.9.7 was the current stable version. You can check the official Python GitHub page for newer releases.

Extract the archive:

Switch into the extracted source directory:

Run the configuration script with optimizations enabled:

$ ./configure --enable-optimizations

The --enable-optimizations option activates performance enhancements.

Compile the source (this may require some time):

The -s flag limits the output to warnings and errors only. After the compilation finishes, you’ll have a usable Python 3.9 binary suitable for production environments.

3. Install Python

Instead of overwriting the existing Python 3.8 installation, it is recommended to install Python 3.9 alongside it. This ensures backward compatibility for older programs.

The use of make altinstall instead of make install ensures that Python 3.8 remains the system default.

You can still invoke Python 3.8 using these commands:

$ python3 -V
$ python3.8 -V

Typical output will look like:

Python 3.8.10

To run Python 3.9, use this command instead:

You should see output like:

Python 3.9.7

Python 3.9 includes the pip package manager as well:

The output will be similar to:

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

If all your applications support Python 3.9, you can switch the system default version like so:

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

Conclusion

Python 3.8 is pre-installed with Ubuntu 20.04, but this guide has demonstrated how to build and install a newer Python version manually. Following this process allows you to leverage the latest features while maintaining compatibility with existing tools and scripts.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in:

Moderne Hosting Services mit Cloud Server, Managed Server und skalierbarem Cloud Hosting für professionelle IT-Infrastrukturen

Secure HTTPS Setup on Arch Linux with Apache or Nginx

Security, Tutorial

Linux file permissions with this comprehensive guide. Understand how to utilize chmod and chown commands to assign appropriate access rights, and gain insights into special permission bits like SUID, SGID, and the sticky bit to enhance your system’s security framework.