Edited 2 weeks ago by ExtremeHow Editorial Team
PythonUbuntuProgrammingLinuxSoftwareInstallationUpgradeOperating SystemsDevelopmentSystem
This content is available in 7 different language
Python is one of the most popular programming languages used in various applications across the world. It is known for its simplicity and versatility, making it a favorite among both beginners and experienced developers. On Ubuntu, which is a widely used Linux distribution, Python can be found installed by default in many cases. However, keeping Python updated is essential to access new features, improvements, and security updates. This guide will help you upgrade Python on Ubuntu step-by-step, ensuring a seamless transition to the latest version.
Before proceeding with the upgrade process, it is important to know the current version of Python installed on your Ubuntu system. You can quickly check it by opening your terminal and typing the following command:
python --version
Or
python3 --version
The first command checks the version of the default Python installation, which may be Python 2 or Python 3 depending on your system configuration. The second command explicitly checks Python 3, which is the version that will usually be upgraded since Python 2 is often deprecated.
There are two main versions of Python: Python 2 and Python 3. Python 2 has reached its end of life and is no longer actively maintained. Therefore, it is highly recommended to upgrade to Python 3, which is supported and receives updates.
Python 3 also has several sub-versions such as 3.8, 3.9, 3.10, etc. It is important to know which exact version you want to upgrade to as you proceed with this guide.
Before upgrading Python, make sure your Ubuntu package list is up to date. This process involves updating the list of available packages and installing the latest versions of these packages.
In your terminal, execute the following command:
sudo apt update sudo apt upgrade
The first command brings up a list of available updates, and the second command applies these updates to your system. Address the prompts or confirmations the terminal may require during this process.
Ubuntu doesn't always provide the latest Python version through its official repositories. Therefore, adding an alternative repository like the DeadSnake PPA allows you to access more recent Python releases.
To add the DeadSnake PPA, use the following command:
sudo add-apt-repository ppa:deadsnakes/ppa
Press Enter when prompted to confirm adding this repository. Refresh your package list by running:
sudo apt update
Once the DeadSnake PPA has been added, you can proceed to install the desired Python version. For example, to install Python 3.9, execute the following command:
sudo apt install python3.9
Replace 3.9
with the version number you want to install. Follow any prompts in the terminal to complete the installation process. New Python versions installed this way do not replace existing installations by default, allowing multiple versions to coexist on the same system.
You may want to change the default Python version after installing the new version. This adjustment affects which Python version is used when you run the python3
command without specifying a version.
To set Python 3.9 as the default, use update-alternatives
command:
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
This command updates the alternative system to prefer Python 3.9 when executing python3
commands. Verify the default Python version with the following:
python3 --version
After the installation and configuration steps, it is important to verify the upgrade. Make sure the new Python version is current by executing:
python3.9 --version
This command will return the version number of the newly installed Python without any error. If you encounter any problems, recheck the steps, make sure all the commands were executed correctly.
Installed Python versions use their site-packages for libraries. If you have existing packages, it is sensible to replicate them into your new Python installation. You can achieve this using a tool like pip
, the Python package installer.
First, make sure pip
is installed for your new Python version:
sudo apt install python3.9-distutils sudo apt install python3-pip
Next, list the packages from an existing version and install them for your new version:
pip freeze > installed_packages.txt python3.9 -m pip install -r installed_packages.txt
These commands take the list of current packages and then install those packages with Python 3.9.
When upgrading Python on Ubuntu, you may encounter some problems. Here are common problems and solutions:
apt -f install
.sudo
. Make sure sufficient permissions have been granted.Upgrading Python helps to access new language features, performance improvements, and security patches. It also allows compatibility with the latest libraries often developed for recent Python versions. Staying up to date smooths development processes and reduces bugs.
Upgrading Python on Ubuntu involves checking your existing Python version, updating system packages, adding required repositories, installing new Python versions, and finally configuring your system to use the updated version by default. Following these steps ensures a hassle-free upgrade and access to the latest Python capabilities, leading to a better software development experience.
If you find anything wrong with the article content, you can