pyenv Cheat Sheet (Mac & Linux)
Table of contents
- What is pyenv?
- Installation
- Install Python Versions
- pyenv Usage
- pyenv-virtualenv (manage virtual environments)
- Upgrading Python in an Existing Project
- Troubleshooting
- Common Paths
- Cleanup
- Tips
What is pyenv?
pyenv is a tool that lets you easily switch between multiple versions of Python. It works by manipulating the PATH to point to the desired Python version.
Installation
Prerequisites
MacOS:
brew update
brew install openssl readline sqlite3 xz zlib
Linux (Debian/Ubuntu):
sudo apt update
sudo apt install -y make build-essential libssl-dev \
libreadline-dev zlib1g-dev libsqlite3-dev curl git
Install pyenv
Using Homebrew (MacOS/Linux)
brew install pyenv
Manual (Universal)
curl https://pyenv.run | bash
Shell Configuration
Add to your shell config (~/.bashrc, ~/.zshrc, etc.):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Then restart your shell:
exec "$SHELL"
Install Python Versions
List available versions
pyenv install --list
pyenv install --list | grep -E ' 3\.([1-9][0-9]+)'
Install a version
pyenv install 3.11.9
Uninstall a version
pyenv uninstall 3.11.9
pyenv Usage
Show current Python version
pyenv version
ls $PYENV_ROOT/versions/
List all installed versions
pyenv versions
Set global Python version
pyenv global 3.11.9
Set local Python version (for project dir)
pyenv local 3.10.12
Creates a .python-version file in current directory.
Set a virtual env inside the project (using Python’s venv)
python -m venv .env
Create .env/ folder inside the project
Set shell-specific version
pyenv shell 3.8.18
pyenv-virtualenv (manage virtual environments)
Install:
brew install pyenv-virtualenv
Add to shell config:
eval "$(pyenv virtualenv-init -)"
Usage:
- Create virtualenv:
pyenv virtualenv 3.10.6 myenv
- Activate virtualenv:
pyenv activate myenv
- Deactivate:
pyenv deactivate
- Auto-activate per-project:
echo "myenv" > .python-version
- Set the local environment using pyenv local
pyenv local my-awesome-env
- List pyenv virtualenvs
pyenv virtualenvs
- Delete the Virtual Environment
pyenv uninstall <env-name>
Upgrading Python in an Existing Project
If a project needs a new Python version:
pyenv install 3.13.0
pyenv virtualenv 3.13.0 newenv
pyenv local newenv
pip install -r requirements.txt
Troubleshooting
pyenv not found?
Check if installed and shell configured properly.
command -v pyenv
Should return something like /home/youruser/.pyenv/bin/pyenv
Build failed?
Install missing dependencies. Check for errors related to:
zlibopensslreadline
Common Paths
- Pyenv root:
~/.pyenv - Installed Python:
~/.pyenv/versions/ - Virtualenvs:
~/.pyenv/versions/<env-name>
Cleanup
Remove pyenv
rm -rf ~/.pyenv
Also remove lines from your shell config file.
Tips
- Use
pyenv localin each project. - Combine with
pipxorpoetryfor isolated environments. - For global Python packages per version: use
~/.pyenv/versions/3.x.y/lib/python3.x/site-packages/