Manage multiple Python versions in Linux using pyenv
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.
pyenvfor Debian and Void Linux
- Install the latest version of Python
- Mark this newly-installed Python as my default Python
1. Install pyenv
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
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
$ . ~/.profile
$ 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
$ sudo base-devel libffi-devel bzip2-devel openssl openssl-devel readline readline-devel sqlite-devel xz liblzma-devel zlib zlib-devel
3. Install Python
At this point, the only Python environment I have installed is the version included with the
system, as seen by
$ 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.0 as of November 2023) ...
$ pyenv install -v 3.12.0
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.0
I select the newly-installed
3.12.0 as my preferred Python version to use with the
global command ...
$ pyenv global 3.12.0 $ pyenv versions system * 3.12.0 (set by /home/dwa/.pyenv/version) $ which python /home/dwa/.pyenv/shims/python $ pyenv which python /home/dwa/.pyenv/versions/3.12.0/bin/python
Now whenever I run
pip an executable from
3.12.0 will be run instead of
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
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
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!
» Next: How to create a LAN subnet using OpenWrt
« Previous: #37. The Galileo Project