- How to Install Latest Python Version in Ubuntu
- Install Python 3.11 on Ubuntu
- Set Default Python Version in Ubuntu
- 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 Install Python 3.11/3.9/3.8 & Other Versions in Ubuntu 22.04
- Option 1: Install recent Python versions via PPA
- Add PPA & Install certain Python edition
- Verify
- Use Pip for specific Python version
- Set your Python as default
- Option 2: Manually compile Python in Ubuntu 22.04
- Install Build dependency
- Download Python tarball:
- Build & Install Python:
- Verify
- Uninstall Python:
- Summary
How to Install Latest Python Version in Ubuntu
Python is the fastest-growing major general-purpose programming language. There are a number of reasons attributed to this, such as its readability and flexibility, ease to learn and use, reliability, and efficiency as well.
There are two major Python versions being used – 2 and 3 (the present and future of Python); the former will see no new major releases, and the latter is under active development and has already seen a lot of stable releases over the last few years. The latest stable release of Python 3 is version 3.11.
On newer Ubuntu releases, Python 3.10 or Python 3.8 is pre-installed, which is not the case for older Ubuntu versions.
In this article, we will explain how to install the latest Python 3.11 version on all Ubuntu releases via the apt package manager using deadsnakes PPA.
To install the latest version of Python from sources in all major Linux distributions, check out this guide: How to Install Latest Python from Source in Linux
Install Python 3.11 on Ubuntu
To install the latest Python 3.11 version, you can use “deadsnakes” team PPA which contains more recent Python versions packaged for Ubuntu.
$ sudo add-apt-repository ppa:deadsnakes/ppa $ sudo apt update $ sudo apt install python3.11
If you want to install a specific Python version or multiple versions of Python in your Ubuntu system, you can simply run the following command with the Python version number as shown.
$ sudo apt install python3.10 $ sudo apt install python3.9 $ sudo apt install python3.8 $ sudo apt install python3.7 $ sudo apt install python3.6
To view a list of all Python binaries installed on your system, run the following ls command.
lrwxrwxrwx 1 root root 10 Apr 22 2022 /usr/bin/python3 -> python3.10 -rwxr-xr-x 1 root root 5901416 Apr 2 2022 /usr/bin/python3.10 -rwxr-xr-x 1 root root 6705016 Oct 24 15:56 /usr/bin/python3.11 -rwxr-xr-x 1 root root 960 Dec 23 2020 /usr/bin/python3-futurize -rwxr-xr-x 1 root root 964 Dec 23 2020 /usr/bin/python3-pasteurize
From the output in the screenshot above, the default Python version on the test system is 3.10, you can also check the Python version using the following command.
$ python -V Python 3.10.4
To use Python 11, invoke the following command.
Python 3.11.0 (main, Oct 24 2022, 19:56:13) [GCC 11.2.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> print ("TecMint #1 Linux Blog"); TecMint #1 Linux Blog >>> quit()
To exit the Python interpreter, type the following command and press Enter.
Set Default Python Version in Ubuntu
If you’ve installed multiple versions of Python in your Ubuntu system, and you want to set only one version as default, then you need to perform some additional steps as shown.
$ python3 --version $ 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 $ sudo update-alternatives --config python3 $ python3 --version
That’s all! In this short article, we have explained how to install Python 3.11 in Ubuntu via the apt package manager. If you have questions, use the comment form below to reach us.
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 Install Python 3.11/3.9/3.8 & Other Versions in Ubuntu 22.04
This simple tutorial shows how to install different version of Python programming language and set default in Ubuntu 22.04.
Ubuntu 22.04 defaults to Python 3.10. For software developers need to test app with different Python editions, or those have apps that requires specific Python edition. Here’s how to install Python 3.11, 3.9. 3.8, 3.7, 3.6 and other versions step by step.
Option 1: Install recent Python versions via PPA
There’s a trustworthy PPA has been maintaining the Python packages for Ubuntu LTS for quite a few years. With it, you may install Python 3.12(Dev), Python 3.11, Python 3.9, Python 3.8, and Python 3.7 as easy as few commands.
Add PPA & Install certain Python edition
1. Firstly, press Ctrl+Alt+T on keyboard to open terminal. When it opens, run the command below to add the PPA:
sudo add-apt-repository ppa:deadsnakes/ppa
Type user password when it asks (no visual feedback) for sudo authentication. Then hit enter to continue.
2. After adding the PPA, simply run the command below to install certain python version as you want (e.g., v3.11):
sudo apt install python3.11 python3.11-distutils
Replace version number in command, e.g.,python3.8
andpython3.8-distutils
for Python 3.8
Verify
The PPA installs the Python packages directly in your PATH (“/usr/bin”). Depends on which version you installed, simply run python3.8 or python3.11 in terminal will start the IDE.
To verify Python versions, use commands python3.x —version :
Use Pip for specific Python version
It does not install pip separately for each version. But, you may simply use the command below to install a package via Pip (replace 3.11 to yours Python version):
python3.11 -m pip install PACKAGE
Or, add a shortcut via command:
alias pip3.11="python3.11 -m pip"
Then you may use command below to use pip install:
NOTE: You needpython3-pip
andpython3.x-distutils
to make the previous pip commands work! Install the former viasudo apt install python3-pip
command.
Set your Python as default
The system default Python3 is Python3.10 . It’s not recommended to change it because some core apps depend on it.
However, you may set default python which is not in use by default. Firstly, run the commands below to add your Python as alternatives (run the command below accordingly):
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.11 1
Run the previous commands if you even has other versions installed. And, finally use the command below choose one as default:
sudo update-alternatives --config python
Option 2: Manually compile Python in Ubuntu 22.04
The Python version is not in PPA, or you just don’t trust third-party PPAs? It’s easy to build the package by yourself.
Install Build dependency
Firstly, you need some dependency libraries for building the language. Press Ctrl+Alt+T on keyboard to open terminal and run the command below to install them:
sudo apt install build-essential libreadline-dev libncurses5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
Download Python tarball:
Next, select download the Python tarball that you need from its website:
After that, extract the package, right-click on the folder and select “Open in Terminal“:
Build & Install Python:
Once the terminal opens in the correct working directory, run command to configure source:
./configure --enable-optimizations
And finally make & install it:
The -j4
flag tells to start 4 threads! Remove it or change the number depends on how many CPU cores you have.
Verify
The previous commands will build and install Python into “/usr/local/bin” and install pip alongwith it.
To verify them use command (change version 3.7 to yours):
And, you may set it as default by adding as alternatives via command:
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python3.7 1
Then use sudo update-alternatives —config python to choose it as default.
Uninstall Python:
To remove Python installed via PPA, for example python3.11 , use command:
sudo apt remove --autoremove python3.11
And remove the Ubuntu PPA via command:
sudo add-apt-repository --remove ppa:deadsnakes/ppa
To remove the package compiled from source, you have to first navigate to source folder in terminal and then run command:
Summary
Install different Python versions in Ubuntu 22.04 is quite easy. There’s an Ubuntu PPA for Python 3.12, 3.11, 3.9, 3.8 and 3.7. Though all other version can be easily compiled from source.