Update Python on Ubuntu 22.04

Posted by Stefan Kecskes on Friday, December 1, 2023

Ok, so you have a fresh installation of Ubuntu 22.04 (Jammy Jellyfish), and you would like to update the default Python 3.10 that comes preinstalled in Ubuntu 22.04 to the latest version. It seems trivial, as you think that you can just uninstall the old version and install the new one. But it is not that simple. The problem is that Ubuntu 22.04 uses python 3.10 for some of its internal tools, and if you uninstall it, you will break your system. I know it, because I did that and I would like to save you some time and pain from restoring your system. So, what can you do? Well, you can install the new version of python alongside the old one, or even have multiple versions of python. This is a quick guide on how to do that.

Install new Python 3 versions

We will look into how to install Python 3.7, 3.8, 3.9, 3.11, 3.12 and 3.13. You can install any of these versions, or all of them. The process is the same for all of them. For all these versions we will use precompiled binaries from DeadSnakes PPA which is well maintained repository of Ubuntu distributions. We will use add-apt-repository to add the repository to our system, then we will update the package list, and finally we will install the python version we want or all of them.

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7 python3.8 python3.9 python3.11 python3.12 python3.13

Great, now we have all the python versions we want installed on our system. We can check that by running:

python3.7 --version
python3.8 --version
python3.9 --version
python3.11 --version
python3.12 --version
python3.13 --version

Install Python 3.6 on Ubuntu 22.04

If you work for some big corporations like me, you might have to work on old projects that need Python 3.6. Unfortunately, Python 3.6 is not available in the DeadSnakes PPA for Ubuntu 22.04, so we will have to install it manually, but before that we will install some dependencies that we will need to compile Python 3.6 from source:

sudo apt install make build-essential libreadline-dev \
wget curl llvm libssl-dev zlib1g-dev libbz2-dev  \
libsqlite3-dev libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev libgdbm-dev \
libnss3-dev libedit-dev libc6-dev

You might have some of these dependencies already installed on your system, but it is ok to install them again. Now we will download the precompiled binaries from the official Python website and extract the archive to a folder. I will use Python 3.6.15, but you can download any version you want:

wget https://www.python.org/ftp/python/3.6.15/Python-3.6.15.tgz
tar -xf Python-3.6.15.tgz
cd Python-3.6.15

Then we will compile Python 3.6 from source using CPython interpreter specifically for our version of Ubuntu with some optimized configuration and make build tool and install it on our system:

./configure --enable-optimizations --with-lto --with-pydebug
make -j 16  # adjust 16 for number of your CPU cores
sudo make altinstall

For your information, these 3 commands take a couple of minutes to finish. At this point we can verify that the python was successfully installed on our system by running:

python3.6 --version

Install Python 2.7 on Ubuntu 22.04

The Python Software Foundation decided to sunset Python 2.7 on January 1st, 2020. I would generally not recommend using Python 2.7, because it is not supported anymore with security updates, and it might have many critical security vulnerabilities. But if you have to use it for whatever reason, you can install it on Ubuntu 22.04 by running:

sudo apt install python2.7 -y
python2 --version  # should return the last version 2.7.18

Using various Python versions in projects

The main reason why we want to have multiple versions of python installed on our system is to be able to use them in our projects. The typical use case is that we have project that needs Python 3.7 because it is old project that runs on old servers and we don’t have capacity at the moment to update all dependencies and rewrite code. But in the same time we have a new project that needs Python 3.13 because it uses some new features that are not available in older versions of Python. So, how can we use different versions of Python in our projects? The answer is virtual environments.

What are virtual environments?

We will use venv to create virtual environments for each of our projects. Venv is a module that comes with Python 3.3 and later. It allows us to create isolated environments for our projects. Without virtual environments we used to have to install all dependencies for all projects globally in the same environment, which is not a good idea, because we had conflicts between dependencies of different projects, and we had to reinstall dependencies every time we switched between projects. With virtual environments we can create isolated environments for each of our projects and install dependencies only for that project.

Install venv modules for all python versions we have installed on our system:

sudo apt install python3.7-venv python3.8-venv python3.9-venv python3.11-venv python3.12-venv python3.13-venv

Create virtual environments for our projects

Let’s say that we have the following structure of our projects on our system:

/home/stefan/projects
├── oldProject  # needs Python 3.7
│   ├── requirements.txt  
│   └── src
│       └── main.py
└── newProject  # needs Python 3.13
    ├── requirements.txt  
    └── src
        └── main.py

First, we will create a virtual environment for our oldProject using Python 3.7 with all dependencies residing in the .venv folder inside our project by running the following commands:

cd /home/stefan/projects/oldProject
python3.7 -m venv .venv

If you check the new .venv folder, it will have python3.7 ready to be used, so we can activate the virtual environment and install the dependencies from requirements.txt for our project with pip by running:

source .venv/bin/activate
pip install -r requirements.txt

Amazing, now we are ready to run our project:

python src/main.py

The process to create the same for the newProject is the same, but we will use Python 3.13 instead of Python 3.7. So, we will run the following commands:

cd /home/stefan/projects/newProject
python3.13 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt    
python src/main.py

Conclusion

We have learned how to install multiple versions of Python on Ubuntu 22.04 and how to use them in our projects with virtual environments. I hope this guide was helpful to you. Please follow me bellow, or if you have any questions or suggestions, please send me a message. Thank you for reading and have a nice day!

Twitter GitHub LinkedIn Medium