Reserve the NVIDIA GH200 Grace Hopper™ Superchip at Vultr today.
Author: Tung NguyenLast Updated: Mon, Oct 4, 2021
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.
$ sudo nano /etc/apt/sources.list
To enable the source packages, add their location, including URL, distribution name, and component name.
deb-src http://archive.ubuntu.com/ubuntu/ focal main
Save the file and exit.
Update the packages index.
$ sudo apt update
Install the required system libraries for Python.
$ sudo apt -y build-dep python3
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
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.
Extract the archive.
$ tar xzf v3.9.7.tar.gz
Change the working directory to the source directory.
$ cd cpython-3.9.7
Configure the source.
$ ./configure --enable-optimizations
--enable-optimizations flag actives all stable optimizations.
Build the source (this may take a few minutes).
$ make -s
-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.
$ sudo make altinstall
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.8, or their absolute paths
$ python3 -V $ python3.8 -V
Both give the same output like this.
To use Python 3.9, invoke
python3.9 or its absolute paths
$ python3.9 -V
The result is similar to this.
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