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.12, 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, 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.12):
sudo apt install python3.12 python3.12-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:
pip3.11 install PACKAGE
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
In the commands replace python3.8 and python3.11 to your desired python versions. 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:
sudo make altinstall -j4
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):
python3.7 --version
pip3.7 --version
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.12
, use command:
sudo apt remove --autoremove python3.12
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
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.