Manage multiple Python versions in Linux using pyenv
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.
1. Setup
- Install
pyenv
on Linux Mint Debian Edition (LMDE) - Install the latest version of Python
- Mark this newly-installed Python as my default Python
2. 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
3. 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
4. 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
Install the latest Python stable version (3.12.3
as of May 2024):
pyenv install -v 3.12.3
pyenv
downloads and builds the Python source.
5. Switch Python
Now I have some options:
$ pyenv versions
* system (set by /home/dwa/.pyenv/version)
3.12.3
I select the newly-installed 3.12.3
as my preferred Python version to use with the global
command:
$ pyenv global 3.12.3
$ pyenv versions
system
* 3.12.3 (set by /home/dwa/.pyenv/version)
$ which python
/home/dwa/.pyenv/shims/python
$ pyenv which python
/home/dwa/.pyenv/versions/3.12.3/bin/python
Now whenever I run python
or pip
an executable from 3.12.3
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
.
6. 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 💬
» Next: Rename photos automatically using Python and Cron
« Previous: After the install: My configuration script for Debian Bookworm