- How to Update Python
- How to Update Python in Linux, Mac, or Windows
- Updating Python in Linux
- Updating Python Using Apt-Get
- Updating Python in Mac
- Updating Python in Windows
- Conclusion
- Upgrade Python to latest version (3.10) on Ubuntu Linux
- Updating Python to the latest version
- Step 1: Check if Python3.10 is available for install
- Step 2: Install Python 3.10
- Step 3: Set Python 3.10 as default
- Fix pip and disutils errors
- Fix Python3-apt
- Install pip & distutils
- Fix pip-env errors when using venv
- Extra
- How to upgrade to Python 3.11 on Ubuntu 20.04 and 22.04 LTS
- Install Python 3.11
- Step 1: Add the repository and update
- Step 2: Install the Python 3.11 package using apt-get
- Step 3: Add Python 3.10 & Python 3.11 to update-alternatives
- Step 4: Update Python 3 for point to Python 3.11
- Step 5: Test the version of python
How to Update Python
Python receives a major update once every 12 months, with bug-fix updates and security patches being released every few months. The most recent version of Python – Python 3.9 – introduces features like Union Operators in dict, the Flexible function, and Type Hinting Generics. Updating Python can be confusing regardless of which OS you’re running. Do you remove the old version first? Or can you update the package directly? Here’s an easy tutorial on how to update Python.
How to Update Python in Linux, Mac, or Windows
It is possible to install two major versions of Python – say, Python 2.x and Python 3.x – on one computer and use both. If you want to move from Python 2.x to 3.x, you don’t have to uninstall the previous version unless you don’t want to use it anymore. Furthermore, Python 3 now comes with the “py.exe” launcher integrated by default. The launcher helps users in two ways:
- Users can run Python from any shell using the command “py.” Typing “python” is no longer required.
- Allows you to use two different versions of Python 3 at the same time. You can specify which version of Python you want to use to run code with simple switches in commands (like “py -3.6”).
In other words, you won’t need to change the PATH variable every time you want to use another version of Python. But the launcher only comes installed on the Windows version of Python 3. Updating Python in computers with Linux or macOS can be more complicated.
Note: If you’ve not installed Python on your computer yet, you cannot update to a newer version. If you’re having trouble installing Python on your computer, reading our Python installation guide will help.
Updating Python in Linux
Before you run any commands, you can save yourself a lot of effort by checking whether the Linux installation comes with the latest version of Python installed. To check what version of Python 3 your machine has, open the Terminal by pressing Ctrl+Alt +T, and type in the following command: python3 –version Or alternatively: python3 -V The output should look something like this: If you see that your machine doesn’t have Python 3.9, you must run the following commands: sudo apt update sudo apt install software-properties-common These commands will install the Python packages required to install the newest Python version. You can now install the latest version of Python in two ways: using Apt and using the source code. Using Apt to update Python is faster and easier. Here’s how you do it:
Updating Python Using Apt-Get
Using the Apt package manager is the easiest method of installing Python 3.9 on Linux. First, you must configure the deadsnakes PPA to your machine by running the following command: sudo add-apt-repository ppa:deadsnakes/ppa You can now update the apt cache and install the Python 3.9 package: sudo apt update sudo apt install python3.9 The Python update will install. However, by default, Python will point to the previously installed version of Python 3. After installation completes, you must update Python 3 to point to Python 3.9. Enter this command to configure Python 3: sudo update-alternatives —config python3 A list of options will appear, and you will be prompted to pick which version of Python you want Python 3 to point to. The prompt should look like this: All you have to do is type the right selection number, and you will have updated Python to the newest version on your Linux machine. To verify the version of Python being used, you can run: python3 —V
Updating Python in Mac
Updating Python on a machine with macOS is a lot easier than updating it on a Linux machine. A Mac can have more than one version of Python installed. Therefore, you can update Python by visiting https://www.python.org/downloads/mac-osx/ , downloading the installer, and running it. If you have Homebrew installed on your Mac, you can run the following command on the Terminal: brew install python After the process completes, your computer will have the latest version of Python 3. You can verify this by running the command: python3 —version If you want to upgrade pip and add a new library to it, you can enter the following command: pip3 install
Updating Python in Windows
Updating to a new Python version is easy on a computer running Windows. All you have to do is visit the Python downloads page and download the latest version. If you’re updating to a new patch of Python (3.x.a to 3.x.b), when you run the installer, it will prompt you to “Upgrade Now.” The installer will look like this: Clicking on the button will replace the existing version of Python with the new version. The older version will be removed from your computer. After you restart the computer, the new patch will be installed on your machine. In contrast, if you’re updating Python to a new minor version (3.x to 3.y), you will see the “Install Now” prompt instead of the “Upgrade Now” prompt on the installer. Clicking the button will install the new version of Python. However, the old version will not be removed. Your computer can have more than one Python version installed. The py launcher is installed automatically regardless of the version of Python you’re using. It will enable you to select the version of Python you want to use. Running a program with a specific version is as simple as using the “py” command and entering the version you want to use, like so: py -3.8 or py -3.9 Suppose there are many projects on your machine that use the previous version of Python, and you use them all in a virtual environment. In that case, you can update Python just in that environment using the following command: python -m venv —upgrade
Conclusion
Every version of Python, including the newest release , has some limitations. However, updating to the latest version of Python allows you to use all the new features. Furthermore, updated versions also come with bug fixes and improved security measures. With this guide handy, you should be able to get your hands on the newest Python version in a matter of minutes. Going through our Python usage guide next will help you quickly pick up all that you can do with Python.
Upgrade Python to latest version (3.10) on Ubuntu Linux
Linux systems come with Python install by default, but, they are usually not the latest. Python also cannot be updated by a typical apt upgrade command as well.
To check the version of Python installed on your system run
python keyword is used for Python 2.x versions which has been deprecated
- Update Python to the latest version
- Fix pip & other Python related issues
- While doing the above two, ensure your Ubuntu which is heavily dependent on Python does not break
Updating Python to the latest version
Ubuntu’s default repositories do not contain the latest version of Python, but an open source repository named deadsnakes does.
Python3.10 is not officially available on Ubuntu 20.04, ensure you backup your system before upgrading.
Step 1: Check if Python3.10 is available for install
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update
Check if Python 3.10 is available by running
This will produce the below result, if you see python3.10 it means you can install it
Step 2: Install Python 3.10
Now you can install Python 3.10 by running
sudo apt install python3.10
Now though Python 3.10 is installed, if you check the version of your python by running python3 —version you will still see an older version. This is because you have two versions of Python installed and you need to choose Python 3.10 as the default.
Step 3: Set Python 3.10 as default
Steps beyond here are tested on Ubuntu 20.04 in VM & WSL2, but are experimental , proceed at your own risk.
Changing the default alternatives for Python will break your Gnome terminal. To avoid this, you need to edit the gnome-terminal configuration file.
Open the terminal and run:
sudo nano /usr/bin/gnome-terminal
In first line, change #!/usr/bin/python3 to #!/usr/bin/python3.8 . Press Ctrl +X followed by enter to save and exit.
Then save and close the file.
Next, update the default Python by adding both versions to an alternatives by running the below
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 Now run
sudo update-alternatives --config python3
Choose the selection corresponding to Python3.10 (if not selected by default).
Now run python3 —version again and you should see the latest Python as the output.
Fix pip and disutils errors
Installing the new version of Python will break pip as the distutils for Python3.10 is not installed yet.
Fix Python3-apt
Running pip in terminal will not work, as the current pip is not compatible with Python3.10 and python3-apt will be broken, that will generate an error like
Traceback (most recent call last): File "/usr/lib/command-not-found", line 28, in <module> from CommandNotFound import CommandNotFound File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 19, in <module> from CommandNotFound.db.db import SqliteDatabase File "/usr/lib/python3/dist-packages/CommandNotFound/db/db.py", line 5, in <module> import apt_pkg ModuleNotFoundError: No module named 'apt_pkg'
To fix this first remove the current version of python3-apt by running
sudo apt remove --purge python3-apt
DO NOT RUN sudo apt autoremove as it will remove several packages that are required. This may break your system if you’re using GUI, if you’re on WSL2 you can proceed.
Finally, reinstall python3-apt by running
sudo apt install python3-apt
Install pip & distutils
Running pip will still throw an error pip: command not found . We need to install the latest version of pip compatible with Python 3.10.
Also, if try to manually install the latest version of pip, it will throw an error like
ImportError: cannot import name 'sysconfig' from 'distutils' (/usr/lib/python3.10/distutils/__init__.py)
Or you might also see an error stating No module named ‘distutils.util’ . This is because the distutils module is not installed yet, to install run the below command
sudo apt install python3.10-distutils
Now you can install pip by running
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py sudo python3.10 get-pip.py
If you get an error like bash: curl: command not found then you need to install curl first by running sudo apt install curl
Now you can run pip and you should see the output of pip —version
Fix pip-env errors when using venv
When you try to create a new virtual environment using python -m venv env , you may into the following error.
Error: Command -Imensurepip--upgrade--default-pipYou can fix this by reinstalling venv by running
sudo apt install python3.10-venv
All should be done now. It is complicated, but this is how you update Python to latest version.
Extra
If you have oh-my-zsh installed, you can avoid typing out python3 by running
Now you can run your files with py or python .
How to upgrade to Python 3.11 on Ubuntu 20.04 and 22.04 LTS
In this article, we will upgrade to Python 3.11 and configure it as the default version of Python in Ubuntu 20.04 and 22.04 LTS.
So let’s start with checking the currently installed version of Python on your system.
As seen in the image above, my currently installed Python version is 3.10 but yours may differ.
Install Python 3.11
Follow the simple steps to install and configure Python 3.11
Step 1: Add the repository and update
The latest Python 3.11 is not available in Ubuntu’s default repositories. So, we have to add an additional repository. On launchpad repository named deadsnakes is available for Python Packages.
Add the deadsnakes repository using the below commands.
sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get update
Update the package list using the below command.
Verify the updated Python packages list using this command.
As seen in the image above, Now we have Python 3.11 available for installation.
Step 2: Install the Python 3.11 package using apt-get
install Python 3.11 by using the below command :
sudo apt-get install python3.11
Step 3: Add Python 3.10 & Python 3.11 to update-alternatives
Add both old and new versions of Python to Update Alternatives.
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1 sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2
Step 4: Update Python 3 for point to Python 3.11
By default, Python 3 is pointed to Python 3.10. That means when we run python3 it will execute as python 3.10 but we want to execute this as python 3.11.
Type this command to configure python3:
sudo update-alternatives --config python3
You should get the above output. Now type 2 and hit enter for Python 3.11. Remember the selected number may differ so choose the selection number which is for Python 3.11.
Step 5: Test the version of python
Finally, test the current version of Python by typing this :
You should get Python 3.11 as an output.
In this article, we learn how to upgrade Python to the latest version which is 3.11 in Ubuntu 20.04 and 22.04 LTS.