Tutorials  /  Python

Install Python 3.9 from Source on Ubuntu 20.04

Ccentron Redaktion · April 2025 ·4 min read ·Python, Tutorial

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.
VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

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:

Console
$ sudo nano /etc/apt/sources.list

Add the following entry to enable source packages:

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

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

Console
$ sudo apt update

Install the necessary build dependencies:

Console
$ sudo apt -y build-dep python3

Install optional third-party libraries:

Console
$ 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:

Console
$ 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:

Console
$ tar xzf v3.9.7.tar.gz

Switch into the extracted source directory:

Console
$ cd cpython-3.9.7

Run the configuration script with optimizations enabled:

Console
$ ./configure --enable-optimizations

The --enable-optimizations option activates performance enhancements.

Compile the source (this may require some time):

Console
$ make -s

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.

Console
$ sudo make altinstall

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:

Console
$ python3 -V
$ python3.8 -V

Typical output will look like:

Python 3.8.10

To run Python 3.9, use this command instead:

Console
$ python3.9 -V

You should see output like:

Python 3.9.7

Python 3.9 includes the pip package manager as well:

Console
$ python3.9 -m pip -V

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:

Console
$ 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.

Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Our team will help you with your specific setup - in German or English, by people who run the platform themselves.

War dieses Tutorial hilfreich?

Your answer is stored anonymously and helps us improve our tutorials.

Kommentare

No comments yet - be the first to ask a question about this tutorial.

Sign in to comment

Comments are open to centron customers. Sign in to your account to ask a question about this tutorial.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren