Is there official documentation on the Python website somewhere, on how to install and run multiple versions of Python on the same machine on Linux?
I can find gazillions of blog posts and answers, but I want to know if there is a "standard" official way of doing this?
Or is this all dependent on OS?
11 Answers
I think it is totally independent. Just install them, then you have the commands e.g. /usr/bin/python2.5 and /usr/bin/python2.6. Link /usr/bin/python to the one you want to use as default.
All the libraries are in separate folders (named after the version) anyway.
If you want to compile the versions manually, this is from the readme file of the Python source code:
Installing multiple versions
On Unix and Mac systems if you intend to install multiple versions of Python using the same installation prefix (--prefix argument to the configure script) you must take care that your primary python executable is not overwritten by the installation of a different version. All files and directories installed using "make altinstall" contain the major and minor version and can thus live side-by-side. "make install" also creates ${prefix}/bin/python3 which refers to ${prefix}/bin/pythonX.Y. If you intend to install multiple versions using the same prefix you must decide which version (if any) is your "primary" version. Install that version using "make install". Install all other versions using "make altinstall".
For example, if you want to install Python 2.5, 2.6 and 3.0 with 2.6 being the primary version, you would execute "make install" in your 2.6 build directory and "make altinstall" in the others.
On Windows they get installed to separate folders, "C:\python26" and "C:\python31", but the executables have the same "python.exe" name.
I created another "C:\python" folder that contains "python.bat" and "python3.bat" that serve as wrappers to "python26" and "python31" respectively, and added "C:\python" to the PATH environment variable.
This allows me to type python or python3 in my .bat Python wrappers to start the one I desire.
On Linux, you can use the #! trick to specify which version you want a script to use.
Update 2019: Using asdf
These days I suggest using asdf to install various versions of Python interpreters next to each other.
Note1: asdf works not only for Python but for all major languages.
Note2: asdf works fine in combination with popular package-managers such as pipenv and poetry.
If you have asdf installed you can easily download/install new Python interpreters:
# Install Python plugin for asdf:
asdf plugin-add python
# List all available Python interpreters:
asdf list-all python
# Install the Python interpreters that you need:
asdf install python 3.7.4
asdf install python 3.6.9
# etc...
# If you want to define the global version:
asdf global python 3.7.4
# If you want to define the local (project) version:
# (this creates a file .tool-versions in the current directory.)
asdf local python 3.7.4