Multiple Python versions on the same machine?

Python

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

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.

Solution 3 - Python

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

Old Answer: Install Python from source

If you need to install multiple versions of Python (next to the main one) on Ubuntu / Mint: (should work similar on other Unixs'.)

  1. Install Required Packages for source compilation

    $ sudo apt-get install build-essential checkinstall $ sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

  2. Download and extract desired Python version

Download Python Source for Linux as tarball and move it to /usr/src.

Extract the downloaded package in place. (replace the 'x's with your downloaded version)

$ sudo tar xzf Python-x.x.x.tgz

3) Compile and Install Python Source

$ cd Python-x.x.x
$ sudo ./configure
$ sudo make altinstall

Your new Python bin is now located in /usr/local/bin. You can test the new version:

$ pythonX.X -V
Python x.x.x
$ which pythonX.X
/usr/local/bin/pythonX.X

# Pip is now available for this version as well:
$ pipX.X -V
pip X.X.X from /usr/local/lib/pythonX.X/site-packages (python X.X)

Solution 4 - Python

I'm using Mac & the best way that worked for me is using pyenv!

The commands below are for Mac but pretty similar to Linux (see the links below)

#Install pyenv
brew update
brew install pyenv

Let's say you have python 3.6 as your primary version on your mac:

python --version 

Output:

Python <your current version>
Now Install python 3.7, first list all
pyenv install -l

Let's take 3.7.3:

pyenv install 3.7.3

Make sure to run this in the Terminal (add it to ~/.bashrc or ~/.zshrc):

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"

Now let's run it only on the opened terminal/shell:

pyenv shell 3.7.3
Now run
python --version

Output:

Python 3.7.3

And not less important unset it in the opened shell/iTerm:

pyenv shell --unset

You can run it globally or locally as well

Solution 5 - Python

How to install different Python versions is indeed OS dependent.

However, if you're on linux, you can use a tool like pythonbrew or pythonz to help you easily manage and switch between different versions.

Solution 6 - Python

Package Managers - user-level

For a package manager that can install and manage multiple versions of python, these are good choices:

  • pyenv - only able to install and manage versions of python
  • asdf - able to install and manage many different languages

The advantages to these package managers is that it may be easier to set them up and install multiple versions of python with them than it is to install python from source. They also provide commands for easily changing the available python version(s) using shims and setting the python version per-directory.

This disadvantage is that, by default, they are installed at the user-level (inside your home directory) and require a little bit of user-level configuration - you'll need to edit your ~/.profile and ~/.bashrc or similar files. This means that it is not easy to use them to install multiple python versions globally for all users. In order to do this, you can install from source alongside the OS's existing python version.


Installation from source - system-wide

You'll need root privileges for this method.

See the official python documentation for building from source for additional considerations and options.

/usr/local is the designated location for a system administrator to install shared (system-wide) software, so it's subdirectories are a good place to download the python source and install. See section 4.9 of the Linux Foundation's File Hierarchy Standard.

Install any build dependencies. On Debian-based systems, use:

apt update
apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libffi-dev libbz2-dev

Choose which python version you want to install. See the Python Source Releases page for a listing.

Download and unzip file in /usr/local/src, replacing X.X.X below with the python version (i.e. 3.8.2).

cd /usr/local/src
wget https://www.python.org/ftp/python/X.X.X/Python-X.X.X.tgz
tar vzxf Python-X.X.X.tgz

Before building and installing, set the CFLAGS environment variable with C compiler flags necessary (see GNU's make documentation). This is usually not necessary for general use, but if, for example, you were going to create a uWSGI plugin with this python version, you might want to set the flags, -fPIC, with the following:

export CFLAGS='-fPIC'

Change the working directory to the unzipped python source directory, and configure the build. You'll probably want to use the --enable-optimizations option on the ./configure command for profile guided optimization. Use --prefix=/usr/local to install to the proper subdirectories (/usr/local/bin, /usr/local/lib, etc.).

cd Python-X.X.X
./configure --enable-optimizations --prefix=/usr/local

Build the project with make and install with make altinstall to avoid overriding any files when installing multiple versions. See the warning on this page of the python build documentation.

make -j 4
make altinstall

Then you should be able to run your new python and pip versions with pythonX.X and pipX.X (i.e python3.8 and pip3.8). Note that if the minor version of your new installation is the same as the OS's version (for example if you were installing python3.8.4 and the OS used python3.8.2), then you would need to specify the entire path (/usr/local/bin/pythonX.X) or set an alias to use this version.

Solution 7 - Python

It's most strongly dependent on the package distribution system you use. For example, with MacPorts, you can install multiple Python packages and use the pyselect utility to switch the default between them with ease. At all times, you're able to call the different Python interpreters by providing the full path, and you're able to link against all the Python libraries and headers by providing the full paths for those.

So basically, whatever way you install the versions, as long as you keep your installations separate, you'll able to run them separately.

Solution 8 - Python

I did this with anaconda navigator. I installed anaconda navigator and created two different development environments with different python versions

and switch between different python versions by switching or activating and deactivating environments.

first install anaconda navigator and then create environments.

see help here on how to manage environments

https://docs.anaconda.com/anaconda/navigator/tutorials/manage-environments/

Here is the video to do it with conda

https://youtu.be/EGaw6VXV3GI

Solution 9 - Python

Wanted to throw in another way to do this, which is to install additional versions of Python alongside the system default. It's lightweight if all you need is just one other version of Python for some specific project. It's using the deadsnakes PPA, so this is specific to Ubuntu Linux.

Instructions. Add the PPA

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update && sudo apt upgrade

Now suppose you want Python 3.5:

sudo apt install python3.5 python3.5-dev python3.5-venv

You can then use the python3.5 executable in your path, if you want.

Now if you need to create a venv for a specific project,

cd your-project
python3.5 -m venv .venv
source .venv/bin/activate

Side note - I did try the popular 'pyenv', but found that it was doing too much in the bashrc/profile, and was slowing down shell prompt. Also, installations of new Python versions were very slow as it was compiling each version. pyenv is probably better if you need to switch between Python versions a lot for many projects.

Solution 10 - Python

I would suggest using pyenv. It is a python version manager that can help you manage multiple versions of python on the same machine.

Solution 11 - Python

Fedora Linux - simply install from packages

Probably worth noting that Fedora distro includes older versions of Python that are not EOL'ed. I find it quite convenient.

For example in the recent Fedora 35 (autumn 2021), the default Python is 3.10, but you can install 3.6, 3.7, 3.8, and 3.9 as well.

The packages are described as:

> Python 3.X package for developers. > > This package exists to allow developers to test their code against an older > version of Python. This is not a full Python stack and if you wish to run > your applications with Python 3., see other distributions > that support it, such as an older Fedora release.

To install for instance the 3.7 as an addition to the installed Python (shell command line, as root or with sudo):

dnf install python3.7

There is no pip, one must start with (shell command line, regular user):

python3.7 -m ensurepip --user --altinstall
# --altinstall will create pip3.7 but will not overwrite pip3

and then we can normally continue:

pip3.7 install --user pytest ... # or whatever you need

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndriy DrozdyukView Question on Stackoverflow
Solution 1 - PythonFelix KlingView Answer on Stackoverflow
Solution 2 - Pythonuser297250View Answer on Stackoverflow
Solution 3 - PythonRotaretiView Answer on Stackoverflow
Solution 4 - PythonKohn1001View Answer on Stackoverflow
Solution 5 - PythonRodrigueView Answer on Stackoverflow
Solution 6 - Pythonblue-catView Answer on Stackoverflow
Solution 7 - PythonSeth JohnsonView Answer on Stackoverflow
Solution 8 - Pythonmohitesachin217View Answer on Stackoverflow
Solution 9 - PythonMendhakView Answer on Stackoverflow
Solution 10 - PythonMatteo ZanoniView Answer on Stackoverflow
Solution 11 - PythonVPfBView Answer on Stackoverflow