How to Install Python 3.8 on Ubuntu 22.04/20.04

Python, version 3.8, comes with several new features and improvements. One of the most notable is the inclusion of a parallel filesystem cache for compiled bytecode. This can provide a significant performance boost when working on large projects. Debug also builds now share ABI as release builds, which should help reduce debug builds’ overall size. Additionally, f-strings now support a handy = specifier for debugging. This can be used to insert the value of an expression into the string, making it easier to track down errors. Finally, continue is now legal in finally: blocks that can be useful when dealing with cleanup procedures that may need to be interrupted.

In the following tutorial, you will learn how to install Python 3.8 using the command terminal and how to download and compile as an alternative method. The tutorial will also demonstrate how to change alternative Python versions for those who have multiple versions installed, such as Python 3.12, 3.11, 3.10, etc.

Recommended Steps Before Installation

Before proceeding with the tutorial, it is highly advised to run an update in your terminal to ensure all packages are up-to-date to avoid any conflicts during the installation. This is important when installing packages such as new kernels and graphic card drivers, especially.

sudo apt update

Optionally, you can list the updates for users who require review or are curious.

sudo apt --list upgradable

Proceed to upgrade any outdated packages using the following command.


sudo apt upgrade

#1st Method: Install Python 3.8 – LaunchPAD PPA (Recommended)

The first and easiest solution for Ubuntu users would be to import the “deadsnakes” team Launchpad PPA. This will always contain the latest updates for Python and all extra packages that may be required.

Importing Python 3.8 Repository

First, install the following packages that are required. These are most likely installed but run the command to be safe.

sudo apt install dirmngr ca-certificates software-properties-common apt-transport-https -y

For users who have not previously imported a GPG key from the Ubuntu keyserver, the command line terminal will often have issues importing GPG keys from LaunchPAD PPAs because the directories are not created. This is an easy fix. Use the following command that will, in turn, generate the directories.

sudo gpg --list-keys

Example output:

gpg: directory '/root/.gnupg' created
gpg: keybox '/root/.gnupg/pubring.kbx' created
gpg: /root/.gnupg/trustdb.gpg: trustdb created

This can be skipped, but if you encounter an issue, just run the command and re-try.

The next task is to import the GPG key needed.


sudo gpg --no-default-keyring --keyring /usr/share/keyrings/deadsnakes.gpg --keyserver keyserver.ubuntu.com --recv-keys F23C5A6CF475977595C89F51BA6932366A755776

Example output:

gpg: key BA6932366A755776: public key "Launchpad PPA for deadsnakes" imported
gpg: Total number processed: 1
gpg:               imported: 1

With the GPG key now imported, you can import the LaunchPAD PPA. Remember, match the command to the version of Linux Mint you are utilizing, or the installation will likely fail with errors.

Import PPA for Ubuntu 22.04 LTS Jammy Jellyfish

echo 'deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu jammy main' | sudo tee -a /etc/apt/sources.list.d/python.list

Import PPA for Ubuntu 20.04 LTS Focal Fossa

echo 'deb [signed-by=/usr/share/keyrings/deadsnakes.gpg] https://ppa.launchpadcontent.net/deadsnakes/ppa/ubuntu focal main' | sudo tee -a /etc/apt/sources.list.d/python.list

Before you continue, run an APT update to reflect the newly imported PPA.

sudo apt update

Run installation command for Python 3.8

With the 3.8 PPA now imported, you can install Python by executing the following command.

sudo apt install python3.8 -y

Verify the installation and build version using the following command.

python3.8 --version

Example output:


Python 3.8.15

Optionally, you can install the following extras.

Debug module Python 3.8 installation command

sudo apt install python3.8-dbg -y

Developer (dev) module Python 3.8 installation command

sudo apt install python3.8-dev -y

VENV (virtual environment) module Python 3.8 installation command

sudo apt install python3.8-venv -y

Distutils module Python 3.8 installation command

sudo apt install python3.8-distutils -y

lib2to3 utility module Python 3.8 installation command

sudo apt install python3.8-lib2to3 -y

DBM.GNU module Python 3.8 installation command

sudo apt install python3.8-gdbm -y

Tkinter module Python 3.8 installation command

sudo apt install python3.8-tk -y

Alternatively, to install all extras, run the full installation command.

sudo apt install python3.8-full

#2nd Method: Install Python 3.8 – Download Archive Manually

Download Python 3.8

First, visit the official download page and grab the latest version of the particular one you are after. The exact instructions should work on any version since you are compiling it. Once you have copied the link, use the wget command to download the Python 3.8 archive.

wget https://www.python.org/ftp/python/3.8.15/Python-3.8.15.tar.xz

THIS WILL CHANGE VERY SHORTLY, MAKE SURE TO GET A FRESH LINK; THE ABOVE IS AN EXAMPLE COMMAND ONLY.

Extract the Python archive, and remember to change the version number if you downloaded a newer one.

tar -xf Python-3.8.{version}.tar.xz

Now install the dependencies required to install Python 3.8.


sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev curl libbz2-dev pkg-config make -y

Navigate to the directory.

cd Python3.8.{version}/

Now, run the ./configure –enable-optimizations command.

./configure --enable-optimizations --enable-shared

The script performs several checks to ensure all of your system’s dependencies are present. The ./configure –enable-optimizations will optimize the Python binary by running multiple tests, making the build process slower.

Now that you have built and configured the environment, it is time to compile it with the command make.

make

A handy trick is to specify the -j <number of cpu> as this can significantly increase compiling speed if you have a powerful server.

For example, the LinuxCapable machine has 6 CPUs, and I can use all six or at least use 4 to 5 to increase speed.


make -j 6

Once you have finished building, install Python binaries as follows:

sudo make altinstall

Note, it is advised to use the make altinstall command NOT to overwrite the default Python 3 binary system.

Next, you need to configure the dynamic linker run-time bindings with the ldconfig command after the installation.

sudo ldconfig /opt/Python3.8.{version}

Example only:

sudo ldconfig /opt/Python3.8.15

Note, do not skip this, or you will face issues. You will also need to replace the path with your directory name and version.

Confirm that Python 3.8 and the build version are installed by running the following command.


sudo python3.8 --version

Example output:

Python 3.8.15

Install Python PIP with 3.8 on Ubuntu Linux

By default, installing python 3.8 using the following APT command should work for all those using the Python PPA repository.

sudo apt install python3-pip

Pip should have been installed already, but for those that have issues and need to re-install using the manual method, follow the steps downloading get-pip.py using the wget command.

wget https://bootstrap.pypa.io/get-pip.py

Next, install the downloaded file.

python3.8 get-pip.py

Once installed, it is a good idea to check for upgrades.

python3.8 -m pip install --upgrade pip

Example output:


joshua@ubuntu-linux:~$ python3.8 -m pip install --upgrade pip
Defaulting to user installation because normal site-packages is not writeable
Requirement already satisfied: pip in ./.local/lib/python3.8/site-packages (22.3.1)

Now verify the PIP 3.8 version installed with the following command.

pip3.8 --version

Example output:

pip 22.3.1 from /home/joshua/.local/lib/python3.8/site-packages/pip (python 3.8)

Switch Default Python Versions on Ubuntu Linux

You may have a particular one you want as the default for users needing multiple versions of Python on their system. The following steps will show you how to change python versions when you have numerous installed.

First, you must add the symbolic links for every Python version separately. Next to the symlink, you will add the group name python and the option number.

Example (you can customize this or copy):

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 3
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 4
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 5
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 6
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 7

Remember, it does not require you to have all versions listed up; you can copy the entire command, and the versions you have installed will have symlinks created.


Next, list the python versions with the following command.

sudo update-alternatives --config python

Example output:

change python version for 3.8 on ubuntu linux

As mentioned above, the tutorial machine has Python versions 3.12, 3.11, 3.10, 3.9, 3.8, and 3.7 installed, with 3.12 currently being the default selected version. This can be seen with the version with an Asterix * next to the selection number.

If you want to make, for example, Python 3.8 the default version, you will enter the selection number 6 in this example, and yours will vary, given the number of versions installed, as the selection numbers may change.

If successful, you will get the following output.

update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in manual mode

If you relist the alternative options, Python 3.8 is the default version with the Asterix * default sign.


Example:

change python version for 3.8 on ubuntu linux - example relist


Not what you were looking for? Try searching for additional tutorials.

1 thought on “How to Install Python 3.8 on Ubuntu 22.04/20.04”

Leave a Comment