Manage multiple Python versions in Linux using pyenv

Last edited on 2024-01-21 Tagged under  #python   #programming   #lmde   #debian   #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.

My setup:

  • Install pyenv for Debian Linux
  • Install the latest version of Python
  • Mark this newly-installed Python as my default Python

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

$ 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
/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.12.1 as of January 2024) ...

$ pyenv install -v 3.12.1

pyenv downloads and builds the Python source.

4. Switch Python

Now I have options ...

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

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

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

Now whenever I run python or pip an executable from 3.12.1 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 .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!

You can like, share, or comment on this post on Mastodon 💬

Thanks for reading! Read other posts?

» Next: Minimal Debian Bookworm

« Previous: Install FreeBSD 14.0 (Short and Sweet Version)