How to Install Python 3.8 on Ubuntu 22.04 or 20.04

Python 3.8 was a notable release in the Python programming world. While it’s older compared to the current stable version, Python 3.11, and the upcoming Python 3.12, which is in release candidate status, Python 3.8 introduced many enhancements that had a significant impact on the Python community. This guide will show you how to install Python 3.8 on Ubuntu 22.04 Jammy Jellyfish and the earlier version, Ubuntu 20.04 Focal Fossa.

Key Features and Enhancements of Python 3.8:

  • Assignment Expressions: Famously dubbed the “walrus operator” (:=), it allows for in-expression variable assignments, leading to more concise code.
  • Positional-only Parameters: With the new ‘/’ syntax, specific parameters are designated as positional-only, preventing the use of keyword arguments for them.
  • F-strings Debugging: Building on the F-strings from Python 3.6, the ‘!=’ format specifier in Python 3.8 aids in simultaneously displaying an expression alongside its value.
  • Performance Boost: Python 3.8 enjoys refined function calls, optimized memory usage, and a more adept garbage collection mechanism.
  • New Syntax Features: Introducing the ‘continue’ statement within the ‘finally’ clauses offers enhanced program flow control.
  • Advanced Typing: The typing module received a facelift with additions like ‘TypedDict’, ‘Literal’ types, and the ‘@final’ decorator.
  • Revamped Standard Library: Python 3.8 saw the birth of new modules like ‘importlib.metadata’ and refinements to stalwarts like ‘statistics’ and ‘multiprocessing’.
  • Deprecated Features: Certain features, including ‘platform.dist()’ and ‘asyncio.Task.current_task()’, are marked for future removal.
  • Additional Tweaks: A myriad of minor changes and bug resolutions refine the developer experience in Python 3.8.

With this foundational knowledge in place, the subsequent section of our guide will elucidate the steps on how to install Python 3.8 on Ubuntu 22.04 Jammy Jellyfish or Ubuntu 20.04 LTS Focal Fossa, utilizing the Python Launchpad PPA for the most up-to-date version.

Install Python 3.8 on Ubuntu 22.04 or 20.04 via APT PPA

Step 1: Update Ubuntu Before Python 3.8 Installation

Before installing Python 3.8, it’s essential to update your Ubuntu system to ensure that all packages are up-to-date and avoid any potential conflicts during installation. To do this, open your terminal and run the following command:

sudo apt update

If you have any outdated packages, upgrade them with this command:

sudo apt upgrade

Step 2: Import Python 3.8 PPA on Ubuntu

For Ubuntu users, the easiest way to access the latest updates for Python and additional required packages is by importing the “deadsnakes” team Launchpad PPA. This will enable you to install and update Python 3.8 directly from your terminal. To import the PPA, run the following command:

sudo add-apt-repository ppa:deadsnakes/ppa -y

Step 3: Update Package List After Python 3.8 Import

After importing the PPA, you must update your package list to ensure your system recognizes the newly added repository. To do this, run the following command in your terminal:

sudo apt update

Step 4: Install Python 3.8 on Ubuntu via APT Command

With the PPA successfully imported and your package list updated, you’re ready to install Python 3.8. To do so, run the following command:

sudo apt install python3.8

Step 5: Verify Python 3.8 Installation on Ubuntu 22.04 or 20.04

After the installation, verifying that Python 3.8 has been installed correctly and checking its build version is essential. To do this, run the following command in your terminal:

python3.8 --version

Step 6: Install Additional Python Packages on Ubuntu (Optional)

If desired, you can also install the following additional extra:

Debug module Python 3.8 Ubuntu installation command

sudo apt install python3.8-dbg

Developer (dev) module Python 3.8 Ubuntu installation command

sudo apt install python3.8-dev

VENV (virtual environment) module Python 3.8 Ubuntu installation command

sudo apt install python3.8-venv

Distutils module Python 3.8 Ubuntu installation command

sudo apt install python3.8-distutils

lib2to3 utility module Python 3.8 Ubuntu installation command

sudo apt install python3.8-lib2to3

DBM.GNU module Python 3.8 Ubuntu installation command

sudo apt install python3.8-gdbm

Tkinter module Python 3.8 Ubuntu installation command

sudo apt install python3.8-tk

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

sudo apt install python3.8-full

Install Python 3.8 on Ubuntu 22.04 or 20.04 via source

Step 1: Download Python 3.8 on Ubuntu

To begin, visit the official Python download page and obtain the download link for the latest version of the Python release you want. Remember that these instructions should work for any version, as you’ll be compiling it yourself. Once you’ve copied the download 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

Note: The download link may change, so be sure to obtain a fresh link. The command above is just an example. After downloading the Python archive, extract it and remember to update the version number if you downloaded a newer release:

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

Step 2: Install Required Packages for Python 3.8

Next, you’ll need to install the dependencies required to compile and 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

Step 3: Configure the Python Build Environment

Navigate to the directory where you extracted the Python source code:

cd Python3.8.{version}/

Now, run the ./configure command with the --enable-optimizations and --enable-shared options:

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

The script will perform several checks to ensure that all necessary dependencies are present on your system. Running ./configure with --enable-optimizations will optimize the Python binary by running multiple tests. This can make the build process slower but will result in a faster and more efficient Python installation.

Step 4: Compile Python 3.8 on Ubuntu

After configuring the build environment, it’s time to compile Python using the make command:

make

To significantly increase the compiling speed, you can use the -j <number of CPUs> option, which specifies the number of CPUs you want to utilize. For example, if your server has 6 CPUs, you can use all six or at least 4 to 5 to increase the speed. The command would be make -j 6.

make -j 6

Step 5: Install Python Binaries on Ubuntu

After finishing the building process, install the Python binaries by running the following command: sudo make altinstall. Using the make altinstall command is recommended to avoid overwriting the default Python 3 binary system.

sudo make altinstall

Finally, configure the dynamic linker run-time bindings by running the ldconfig command after installation:

sudo ldconfig

Example only:

sudo ldconfig /opt/Python3.8.16

Step 6: Verify Python 3.8 Installation on Ubuntu

Run the following command to confirm that Python 3.8 and the corresponding build version have been successfully installed:

sudo python3.8 --version

Install PIP on Ubuntu 22.04 or 20.04 with Python 3.8

Option 1: Install PIP via Python 3.8 on Ubuntu

For most users using the Python PPA repository, installing PIP for Python 3.8 can be accomplished simply by running the following APT command:

sudo apt install python3-pip

Option 2: Download the get-pip.py script on Ubuntu

To manually install PIP for Python 3.8, you will first need to download the get-pip.py script using the wget command:

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

This script is provided by the Python Packaging Authority (PyPA) and is used to install or upgrade PIP on your system.

Install PIP for Python 3.8 on Ubuntu 22.04 or 20.04

Once you have downloaded the get-pip.py file, you can proceed with the installation by running the following command:

python3.8 get-pip.py

This will install PIP specifically for Python 3.8 on your system.

Upgrade PIP to the latest version on Ubuntu

After successfully installing PIP, you should check for upgrades to ensure you have the latest version. You can do this by running the following command:

python3.8 -m pip install --upgrade pip

You should see output similar to the following:

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)

This output indicates that PIP has been upgraded to the latest version.

Verify the installed version of PIP

You can verify the version of PIP for Python 3.8 that is installed on your system by running the following command:

pip3.8 --version

This will display the current PIP version associated with your Python 3.8 installation.

Switch Default Python Versions on Ubuntu 22.04 or 20.04

If you need multiple versions of Python installed on your system and want to set a particular one as the default, follow these steps to change Python versions.

Step 1: Add symbolic links for each Python version on Ubuntu

First, you must add symbolic links for each Python version separately. Next to the symlink,”add th” group name “python” and Here’srsion number.

Here’s an example (you can customize this or copy it):

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
Screenshot showing the installation of alternative Python versions on Ubuntu 22.04/20.04.
Demonstrative screenshot of Python 3.8 installation on Ubuntu 22.04 or 20.04.

Remember that you don’t need to list all the Python versions you have installed – copying the entire command will create symbolic links for the versions on your system.

Step 2: List available Python versions on Ubuntu

To list the available Python versions, use the following command:

sudo update-alternatives --config python
Screenshot listing default and alternative Python versions on Ubuntu 22.04/20.04.
Screenshot showcasing the default and alternative Python versions available on Ubuntu 22.04 or 20.04 LTS.

This will display a list of installed Python versions with their respective selection numbers. An asterisk (*) beside the selection number will indicate the currently set default version.

Step 3: Set a different Python version as the default on Ubuntu

To set a different version as the default (e.g., Python 3.8), enter the corresponding selection number (which will vary depending on the number of versions you have installed). In this example, the selection number for Python 3.8 is 6.

After running the command to set Python 3.8 as the default version, the output should confirm the change:

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

Step 4: Verify the default Python version on Ubuntu

After setting Python 3.8 as the default version, you can list the available alternative options again to ensure that Python 3.8 is now the default version (indicated by an asterisk *):

sudo update-alternatives --config python
Screenshot demonstrating how to switch from Python 3.12 to 3.8 on Ubuntu 22.04/20.04.
A visual guide on transitioning from Python 3.12 to Python 3.8 on Ubuntu 22.04 or 20.04 LTS.

This will confirm that the default Python version has been successfully switched to the desired version.

Conclusion

In conclusion, installing Python 3.8 on Ubuntu Linux is a straightforward process that can be accomplished by following the provided steps. Users can easily switch between different Python environments by understanding how to manage multiple Python versions and setting the desired one as the default. This flexibility is essential for developers who work on multiple projects with varying Python version requirements. Always keep your Python installation and packages up to date to ensure a secure and stable development environment.

Leave a Comment


Your Mastodon Instance
Share to...