How to Install an Old Version of Python on Debian or Ubuntu
Install Necessary Libs
Update your package manager and install required packages to build python.
sudo apt-get update$
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev \ libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \ libncurses5-dev libncursesw5-dev xz-utils tk-dev libgdbm-dev \ libc6-dev
Download Python Source Tarball
Say you want to install python 3.7.3, download the source tarball (note that the link is case sensitive).
Feel free to browse https://www.python.org/ftp/python/ for other versions.
Build and Install
After downloaded, unzip it and enter the source directory.
tar -zx -f Python-3.7.3.tgz$
Then start building.
sudo make install
Remove the Symbolic Link
Let's say you already have a python 3.9.2 coming with OS. Before you installed python 3.7.3,
python3 --version gives you Python 3.9.2, while after you installed 3.7.3 you will find python3 is referring to python 3.7.3 instead.
Why is that?
In most GNU/Linux systems, there are directory /usr/bin and /usr/local/bin, the former for binaries supplied by OS or package manager, the latter by user. In most systems the latter precedes the former in PATH, which allows users to install alternate versions of binaries to override OS provided ones.
ls -l /usr/bin/python*lrwxrwxrwx 1 root root 9 Apr 5 2021 /usr/bin/python3 -> python3.9 -rwxr-xr-x 1 root root 5479736 Mar 1 2021 /usr/bin/python3.9 $
ls -l /usr/local/bin/python*lrwxrwxrwx 1 root root 9 Jun 3 12:06 /usr/local/bin/python3 -> python3.7 -rwxr-xr-x 2 root root 15329120 Jun 3 12:06 /usr/local/bin/python3.7
Maybe there are some apps in your system relying on python 3.9.2 and you don't want to disrupt them, in which case, you can remove the symbolic link in /usr/local/bin, which is created during installation. Note that you can always explicitly specify which version you want to use by using python3.7 or python3.9 instead of python3.
$ sudo rm /usr/local/bin/python3
However, if you use command
python3 now, you will get an error:
-bash: /usr/local/bin/python3: No such file or directory
That's because BASH doesn't realize the python3 located in /usr/local/bin has been deleted. You need to relogin BASH or let it rescan PATH.
python3 --versionPython 3.9.2