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-distutilsfor Python 3.8
The PPA installs the Python packages directly in your PATH (“/usr/bin”). Depends on which version you installed, simply run
python3.11 in terminal will start the IDE.
To verify Python versions, use commands
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:
pip3.11 install PACKAGE
NOTE: You need
python3.x-distutilsto make the previous pip commands work! Install the former via
sudo apt install python3-pipcommand.
Set your Python as default
The system default
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:
And finally make & install it:
sudo make altinstall -j4
-j4flag tells to start 4 threads! Remove it or change the number depends on how many CPU cores you have.
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
sudo update-alternatives --config python to choose it as default.
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:
sudo make uninstall
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.