Complete Guide to Installing pip for Python 3.9 on Ubuntu 20.04

Dec 06, 2025 · Programming · 12 views · 7.8

Keywords: Python 3.9 | pip installation | Ubuntu 20.04 | distutils | virtual environment

Abstract: This article provides a comprehensive guide to installing the pip package manager for Python 3.9 on Ubuntu 20.04 systems. Addressing the coexistence of the default Python 3.8 and the target version 3.9, it analyzes common installation failures, particularly the missing distutils.util module issue, and presents solutions based on the official get-pip.py script. The article also explores the advantages and limitations of using virtual environments as an alternative approach, offering practical guidance for dependency management in multi-version Python environments.

Introduction

Ubuntu 20.04 systems come with Python 3.8 as the default version, but many modern Python projects require the newer 3.9 version. Due to system dependency constraints, users typically cannot uninstall Python 3.8, necessitating the management of multiple Python versions. Based on practical installation experience, this article systematically explains the complete process of installing the pip package manager for Python 3.9 and deeply analyzes the root causes of common issues.

Preparation for Python 3.9 Installation

Before installing pip, it is essential to ensure that Python 3.9 is correctly installed on the system. Since Ubuntu's official repositories often lack newer Python versions, installation via third-party PPAs is required:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.9

After installation, verify success with the python3.9 --version command. The system will now have both Python 3.8 and 3.9, where the python3 command defaults to version 3.8, and python3.9 explicitly uses version 3.9.

Analysis of Core pip Installation Issues

Users attempting to install pip for Python 3.9 often encounter two main problems:

  1. Pip installed via the system package manager defaults to Python 3.8: The command sudo apt-get install python3-pip installs pip for the system's default Python 3.8, which cannot be directly used with Python 3.9.
  2. Direct execution of the get-pip.py script results in ModuleNotFoundError: No module named 'distutils.util'. This issue stems from the absence of the distutils module in Python 3.9's standard library, a fundamental dependency for pip installation.

The error message indicates that when executing python3.9 get-pip.py, the script attempts to import the distutils.util module but fails. In Debian-based systems (including Ubuntu), the distutils module is typically provided as a separate package that must be installed independently.

pip Installation Based on the Official Script

The most reliable method to resolve these issues is to follow the pip official documentation, using the bootstrap script for installation. The specific steps are:

  1. Download the official get-pip.py script: curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
  2. Install the distutils module for Python 3.9: sudo apt install python3.9-distutils
  3. Execute the installation command: python3.9 get-pip.py

If permission issues arise, add the --user flag to install pip in the user directory: python3.9 get-pip.py --user. After installation, verify that pip is successfully installed and bound to Python 3.9 using python3.9 -m pip --version.

Virtual Environment as an Alternative Approach

Beyond direct pip installation, using virtual environments is another effective method for managing Python dependencies, particularly suitable for isolating project dependencies:

sudo apt-get install python3.9-venv
python3.9 -m venv myenv
source myenv/bin/activate
pip --version

Virtual environments automatically create isolated Python environments that include pip for the corresponding version. While this method enforces the use of virtual environments, it ensures purity and reproducibility in dependency management, avoiding system-wide package conflicts.

Version Compatibility Considerations

It is important to note that the official get-pip.py script has specific Python version requirements. The current version of the script only supports Python 3.7 and above. For older versions (e.g., 3.6), use version-specific installation scripts: https://bootstrap.pypa.io/pip/3.6/get-pip.py. This version-specific URL format provides backward-compatible solutions for developers.

Conclusion

Installing pip for Python 3.9 on Ubuntu 20.04 requires overcoming challenges posed by the coexistence of the system's default version and the target version. By installing the python3.9-distutils module to resolve basic dependencies and using the official get-pip.py script, pip installation can be reliably completed. The virtual environment approach, while adding complexity, offers better dependency isolation. Developers should choose the appropriate method based on specific project needs to ensure stability and maintainability in Python package management.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.