sudo add-apt-repository ppa:deadsnakes/ppa, sudo apt update, and sudo apt install python3.X (replacing X with your desired version) to install the new binaries alongside the system default.
Why Default Repositories Are Insufficient
Ubuntu 18.04 LTS was released with Python 3.6 as its standard interpreter in the official main repository. While this version is stable and sufficient for many legacy applications, modern development often requires newer features found in Python 3.7, 3.8, or later. The Ubuntu package maintainers prioritize stability over cutting-edge software, meaning they do not frequently backport newer Python versions to older LTS releases like Bionic Beaver.
If you attempt to upgrade Python using only the standard apt commands without adding external sources, your system will likely report that the latest available version is already installed. This does not mean you have the newest Python; it simply means you are stuck at the version provided by Canonical for that specific release cycle. To access newer interpreters, you need a third-party repository known as a Personal Package Archive (PPA).
Adding the Deadsnakes PPA
The most reliable source for newer Python versions on Ubuntu is the deadsnakes PPA. This community-maintained repository provides packages for various Python versions that are not included in the official Ubuntu archives. Before adding this repository, ensure your system has the necessary tools to manage PPAs.
- Install Software Properties Common: If you do not already have the
software-properties-commonpackage installed, you must install it to use theadd-apt-repositorycommand.
sudo apt install software-properties-common
- Add the Repository: Run the following command to add the deadsnakes PPA to your system's software sources.
sudo add-apt-repository ppa:deadsnakes/ppa
When prompted, press Enter to confirm the addition of the repository. This step updates your local package index to include the new sources. Note that if you encounter issues similar to those described in Fixing PPA Purge Errors in Ubuntu, ensure your GPG keys are correctly configured to avoid authentication failures during the update phase.
Installing the Desired Python Version
Once the repository is added, you can search for and install the specific version of Python 3 you require. It is important to understand that installing a new version via APT will not replace the existing system Python. Instead, it installs the new version alongside the old one. This approach prevents breaking system utilities that depend on the original Python 3.6 binary.
- Update Package Index: Refresh your package list to fetch information about the new packages available from the deadsnakes PPA.
sudo apt update
- Install Python: Replace
Xwith the minor version number you want (e.g., 3.8, 3.9, 3.10).
sudo apt install python3.X
For example, to install Python 3.9, you would run sudo apt install python3.9. This command installs the interpreter, the standard library, and the venv module. You may also wish to install python3.X-dev if you plan to compile C extensions for this specific version.
Managing Multiple Python Versions
After installation, you will have multiple Python versions on your system. By default, the python3 command still points to the original system version (3.6). To use the newly installed version, you must invoke it explicitly by its version number, such as python3.9.
If you prefer to make the new version the default when typing python3, you can use the update-alternatives tool. This utility allows you to set symbolic links for different commands. However, modifying the system default is generally discouraged unless you are certain no other system scripts rely on the original version.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
In this example, the higher priority number (2) makes Python 3.9 the default. You can switch between them interactively using sudo update-alternatives --config python3. For most developers, creating virtual environments using the specific version binary is the safest and most flexible workflow.
Verifying the Installation
Confirm that the new version is correctly installed by checking its version string. This step ensures that the package manager successfully downloaded and configured the interpreter.
python3.X --version
You should see output matching the version you requested. If you encounter dependency errors, refer to guides on Solving Ubuntu Unmet Dependencies Error to resolve conflicts with existing libraries. Keeping your Python environment updated ensures compatibility with modern frameworks and security patches.
Frequently Asked Questions
Will updating Python break my system?
No, installing a new Python version via the deadsnakes PPA does not remove or overwrite the system default Python. It installs the new version in parallel, ensuring that critical OS utilities relying on Python 3.6 continue to function correctly.
How do I uninstall a Python version installed from a PPA?
You can remove the specific package using sudo apt remove python3.X. After removal, you may also want to clean up unused dependencies with sudo apt autoremove to free up disk space.
Can I use pip with the new Python version?
Yes, each installed Python version comes with its own pip installer. You should use pip3.X or python3.X -m pip to install packages for that specific interpreter, preventing package conflicts between different Python environments.
Is it safe to change the default python3 symlink?
While technically possible, changing the default symlink is risky. Many Ubuntu system tools depend on the specific behavior of the original Python 3.6. It is safer to use explicit version calls or virtual environments rather than altering the global default.
What if I cannot add the PPA due to network restrictions?
If you cannot access external PPAs, you might consider compiling Python from source. However, this is complex and error-prone. Alternatively, check if your corporate proxy allows HTTPS traffic to the deadsnakes repository, or look into containerized solutions like Docker which isolate Python versions from the host OS.