While Python 3.9 may not be the latest version, it holds historical significance and remains crucial for specific project requirements. This guide will show you how to install Python 3.9 on Ubuntu 22.04 Jammy Jellyfish or the older stable release of Ubuntu 20.04 Focal Fossa. Whether you’re a developer working with a framework that still relies on Python 3.9 or need it for specific software compatibility, this version has enduring relevance.
Key Historical Features in Python 3.9:
- New Parser: A Parsing Expression Grammar (PEG) based parser was introduced, setting the stage for future language enhancements.
- String Functions: Methods like
removeprefix()
andremovesuffix()
simplified string manipulation. - Dictionary Merging: The
|
and|=
operators made dictionary updates more straightforward. - Time Zone Support: The
zoneinfo
module reduced the need for third-party time zone libraries. - Type Hinting: Enhanced type hinting and custom metadata support improved code readability and maintainability.
Noteworthy Deprecations:
- Deprecated Functions: Functions such as
collections.abc.MappingView
were removed. - fractions.gcd(): This function was deprecated in favor of
math.gcd()
.
For those with specific needs tied to Python 3.9, installing this version on Ubuntu can be a strategic choice. The following guide will provide step-by-step instructions for the installation process.
Install Python 3.9 on Ubuntu 22.04 or 20.04 via APT
Step 1: Update Ubuntu Before Python 3.9 Installation
Before starting the installation process, update your Ubuntu system to ensure it is current and to avoid any potential conflicts during the tutorial. Keeping your system up-to-date is crucial for maintaining good system performance and security:
sudo apt update && sudo apt upgrade
Step 2: Import Python PPA on Ubuntu
For Ubuntu users, the simplest solution to install Python 3.9 is by importing the “deadsnakes” team Launchpad PPA (Personal Package Archive). The deadsnakes PPA contains the latest Python releases and required additional packages, ensuring you have access to the most up-to-date version of Python.
First, install the prerequisite package software-properties-common
for adding custom PPAs to your system:
sudo apt install software-properties-common -y
Next, add the deadsnakes PPA to your APT package source list using the following command:
sudo add-apt-repository ppa:deadsnakes/ppa -y
Once the repository is successfully imported, update your package manager to reflect the newly added PPA:
sudo apt update
Now, you can install Python 3.9 by executing the following command:
sudo apt install python3.9
To verify the installation and confirm the Python 3.9 build version, run the following command:
python3.9 --version
Step 3: Install Optional Python 3.9 Extras on Ubuntu via APT Command
Optionally, you can install the following extras to enhance your Python 3.9 experience:
- Install development headers for building C extensions:
sudo apt install python3.9-dev -y
- Install the standard library (venv) module:
sudo apt install python3.9-venv -y
- Install the standard library (distutils) module:
sudo apt install python3.9-distutils -y
- Install the (2to3.9) utility as well as the standard library (lib2to3) module:
sudo apt install python3.9-lib2to3 -y
- Install the standard library (dbm.gnu) module:
sudo apt install python3.9-gdbm -y
- Install the standard library (tkinter) module:
sudo apt install python3.9-tk -y
Install Python 3.9 via source on Ubuntu 22.04 or 20.04
Step 1: Download Python 3.9 Source Code on Ubuntu
To begin the manual installation of Python 3.9, visit the official Python download page and find the desired version. Remember that the instructions here should work for any version since you will compile the source code.
Once you have the download link, use the wget
command to download the Python 3.9 archive. Replace the URL with the appropriate one for the version you are downloading:
wget https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tar.xz
The download link might change over time, so always check for the most recent one.
Step 2: Extract the Python Archive
Next, extract the Python archive. Be sure to change the version number if you downloaded a different version:
tar -xf Python-3.9.{version}.tar.xz
Optionally, move the extracted Python 3.9 directory to a more suitable location, such as the /opt/
directory:
sudo mv Python3.9.{version} /opt/
Step 3: Install Python 3.9 Initial Packages on Ubuntu
Before you can compile Python 3.9, you need to install the required dependencies:
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 4: Configure the Python Build Environment on Ubuntu
Navigate to the Python 3.9 directory:
cd /opt/Python3.9.{version}/
Now, run the ./configure
command with the --enable-optimizations
and --enable-shared
options:
./configure --enable-optimizations --enable-shared
The
./configure
script checks that all dependencies are present on your system. The--enable-optimizations
flag optimizes the Python binary by running multiple tests, which might slow the build process.
Step 5: Compile Python 3.9 on Ubuntu
Compile Python 3.9 by running the make
command:
make
To speed up the compilation process, you can specify the -j <number_of_cpus>
option. This can significantly improve build time if your server has multiple CPU cores:
make -j 6
Step 6: Install Python 3.9 Binaries on Ubuntu
Once the compilation is complete, install the Python binaries with the following command:
sudo make altinstall
It is recommended to use
make altinstall
to avoid overwriting the default system Python 3 binary.
Step 7: Configure Dynamic Linker Run-time Bindings
After the installation, configure the dynamic linker run-time bindings using the ldconfig
command:
sudo ldconfig /opt/Python3.9.{version}
Note: Do not skip this step, or you may encounter issues. Also, remember to replace the path with your directory name and version.
Step 8: Verify Python 3.9 Installation on Ubuntu
To confirm that Python 3.9 is installed and check the build version, run the following command:
python3.9 --version
Create a Test Virtual Environment on Ubuntu 22.04 or 20.04
Python’s venv
module allows you to create virtual environments, isolated Python environments containing their own Python interpreter, libraries, and scripts. These virtual environments are separate from others and, by default, independent of any libraries installed on your operating system. This ensures that you avoid conflicts and disruptions in your production environments.
To confirm that Python 3.9 is properly installed and functioning, let’s create a simple Python project and set up a virtual environment.
Step 1: Create a Project Directory and Navigate to It on Ubuntu
First, create the project directory and navigate to it with the following commands:
mkdir ~/test_app && cd ~/test_app
Step 2: Create a Virtual Environment
Inside the project root directory, run the following command to create a virtual environment named test_app_venv
:
python3.9 -m venv test_app_venv
Note: If you installed Python 3.9 using the PPA method and haven’t installed the python3.9-venv
package yet, you’ll need to do so with the following command:
sudo apt install python3.9-venv -y
Step 3: Activate the Virtual Environment
Next, activate the virtual environment with the following command:
source test_app_venv/bin/activate
You’ll see the environment name prefixed to your shell prompt upon activating the virtual environment. This indicates that you’re inside the virtual environment.
Step 4: Deactivate the Virtual Environment
When you’re done working within the virtual environment, you can exit it by running the deactivate
command:
deactivate
Install PIP with Python 3.9 on Ubuntu 22.04 or 20.04
Python Package Installer (PIP) is a tool that helps you install and manage Python packages from the Python Package Index (PyPI). To get PIP working with Python 3.9, follow the steps outlined below.
Step 1: Install Python PIP Using APT on Ubuntu
If you installed Python 3.9 using the Python PPA repository, you can install PIP with the following APT command:
sudo apt install python3-pip
Step 2: Install PIP Manually (If Not Already Installed) on Ubuntu
PIP should have been installed automatically for those who installed Python 3.9 manually. If, for some reason, it wasn’t, you can install PIP by downloading get-pip.py
using the wget
command:
wget https://bootstrap.pypa.io/get-pip.py
Next, run the downloaded file to install PIP:
python3.9 get-pip.py
Step 3: Upgrade PIP
After installing PIP, it’s a good idea to check for and install any available upgrades:
python3.9 -m pip install --upgrade pip
Step 4: Verify PIP Installation
To ensure that PIP is installed correctly and associated with Python 3.9, check the installed version with the following command:
pip3.9 --version
Switching Default Python Versions on Ubuntu
If you have multiple Python versions installed on your system, you may want to switch between them and set a preferred default Python version. Follow the steps below to change the default Python version effectively.
Step 1: Create Symbolic Links for Python Versions
Create symbolic links for each installed Python version to manage different Python versions. When creating a symbolic link, use the group name “python” followed by the version number.
For example, if you have the following Python versions installed, create symbolic links for each version:
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
You don’t need to have all the versions listed. Copy the command for the Python versions you have installed, and the symbolic links will be created accordingly.
Step 2: View Available Python Versions
To see the available Python versions and their corresponding symbolic links, use the following command:
sudo update-alternatives --config python
Step 3: Switch Default Python Version
After running the --config
command, you will see a list of available Python versions and their priority numbers. The currently selected version will be marked with an asterisk (*). To switch to an alternative version, enter the number corresponding to the desired version and press Enter.
Closing Thoughts
In conclusion, installing Python 3.9 on Ubuntu Linux is a straightforward process that involves several essential steps. We covered the methods for installing Python 3.9 using the PPA repository or manual installation. We also discussed creating a virtual environment to test the installation, installing PIP with Python 3.9, and switching default Python versions for users with multiple Python versions installed. By following these steps, you will be well-equipped to work with Python 3.9 on Ubuntu Linux, whether a beginner or an experienced user.