Multiple Python versions on the same machine?

Question:

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?

Asked By: Andriy Drozdyuk

||

Answers:

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.

Answered By: Seth Johnson

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.

Answered By: Felix Kling

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.

Answered By: user297250

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.

Answered By: Rodrigue

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 a Unix system:

  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
  1. 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)
Answered By: Rotareti

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

Answered By: mohitesachin217

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.7.6 as your primary version on your mac:

python --version 

Output:

Python 3.7.6

Now Install python 3.10

Fist, we can check if it’s already there, run:

pyenv install --list

Scroll up (right above anacoda) to check if it’s there/installed, if not, run:

pyenv install 3.10

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)"
eval "$(pyenv init -)"

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

pyenv shell 3.10

Now run

python --version

Output:

Python 3.10.9

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

pyenv shell --unset

You can run it globally or locally as well

Answered By: Kohn1001

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.

Answered By: blue-cat

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.

Answered By: Mendhak

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

Answered By: Matteo Zanoni

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
Answered By: VPfB
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.