Manage multiple Python versions in Linux using pyenv

Last updated on 2022-07-03 Tagged under  #python   #arch   #linux 

Using pyenv makes it easy to install and switch between multiple versions of Python on a Linux system. It also enables a clean separation between the Python installed by the system (required for its maintenance and to satisfy package dependencies), and other Python versions and libraries installed by the user.

This is how:

Let's go!

1. Install pyenv

Use git to install pyenv to the user's home directory ...

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv

Pyenv uses small executable files called shims to re-direct Python commands to their ultimate file path in the designated Python version. These shims need to be added to the beginning of the user's $PATH.

To prepare my bash shell environment for pyenv, I add to ~/.bash_profile ....

if [ -d "$HOME/.pyenv" ] ; then
    export PYENV_ROOT="$HOME/.pyenv"
    command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"
    eval "$(pyenv init -)"
fi

Reload ...

$ . ~/.profile

My modified $PATH ...

$ echo $PATH
/home/dwa/.pyenv/shims:/home/dwa/.pyenv/bin:/home/dwa/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin

2. Install build dependencies

pyenv builds Python from source code. Before doing so, install software to satisfy build dependencies (the only instance where use of root is required).

On Arch ...

$ sudo pacman -S base-devel openssl zlib xz tk --needed

3. Install Python

At this point, the only Python environment I have installed is the version included with the system, as seen by pyenv ...

$ pyenv versions
* system (set by /home/dwa/.pyenv/version)
$ which python3
/usr/bin/python3
$ pyenv which python3
/usr/bin/python3

Python versions available to be built and installed (a long list) ...

$ pyenv install --list | less

I install the latest Python stable version (3.10.5 as of July 2022) ...

$ pyenv install -v 3.10.5

pyenv downloads and builds the Python source. It took roughly 3 minutes to build on my old laptop.

4. Switch Python

Now I have options ...

$ pyenv versions
* system (set by /home/dwa/.pyenv/version)
  3.10.5

I select the newly-installed 3.10.5 as my preferred Python version to use with the global command ...

$ pyenv global 3.10.5
$ pyenv versions
  system
* 3.10.5 (set by /home/dwa/.pyenv/version)
$ which python
/home/dwa/.pyenv/shims/python
$ pyenv which python
/home/dwa/.pyenv/versions/3.10.5/bin/python

Now whenever I run python, or pip, etc. an executable from 3.10.5 will be run instead of system Python.

Switch back to using system at any time by running pyenv global system.

List all available commands with pyenv commands and help with pyenv <command> --help.

5. Upgrade or Remove

Upgrade pyenv with a simple git pull ...

$ cd ~/.pyenv; git pull

Remove a user-installed Python with pyenv uninstall <version>. To remove pyenv itself, simply remove the settings in ~/.bash_profile to revert back to the old $PATH and delete the ~/.pyenv directory.

There are more options available to set different Python versions per directory, load multiple versions of Python simultaneously, and plugins for extra capabilities. However this is enough to get started!

Thanks for reading! Read other posts?

» Later: Track my weight using Python

« Earlier: Virtualization using KVM + QEMU + libvirt