Manage multiple Python versions in Linux using pyenv

Last updated on 2022-10-18 Tagged under  #python   #debian   #mint   #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.

Let's go!

1. Install pyenv

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

$ git clone ~/.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 ~/.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 -)"

Reload ...

$ . ~/.profile

My modified $PATH ...

$ echo $PATH

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).

$ sudo apt install make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

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
$ pyenv which 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.8 as of October 2022) ...

$ pyenv install -v 3.10.8

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)

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

$ pyenv global 3.10.8
$ pyenv versions
* 3.10.8 (set by /home/dwa/.pyenv/version)
$ which python
$ pyenv which python

Now whenever I run python, or pip, etc. an executable from 3.10.8 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: Install Linux Mint 21 with custom LVM on LUKS

« Earlier: Track my weight using Python