
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.
Building Python from source requires various third-party and system libraries.
Log in to the server as a non-root sudo user via SSH.
Open the /etc/apt/sources.list file.
To enable the source packages, add their location, including URL, distribution name, and component name.
Save the file and exit.
Update the packages index.
Install the required system libraries for Python.
Install the third-party libraries for all optional modules.
Download the source archive to the home directory.
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.
Extract the archive.
Change the working directory to the source directory.
Configure the source.
The --enable-optimizations flag actives all stable optimizations.
Build the source (this may take a few minutes).
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.
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.
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.
Both give the same output like this.
To use Python 3.9, invoke python3.9 or its absolute paths /usr/local/bin/python3.9 instead.
The result is similar to this.
Python 3.9 also comes with pip, the package installer for Python.
The output should be like this.
If you ensure that all existing programs are compatible with Python 3.9, you can make it the default version.
0 Comments
Be the first to comment and share your perspective with the community.