HowTo :: Install PyEnv (Simple Python Version Management)
Introduction
Managing multiple Python versions on a single machine can be a nightmare. System tools often rely on a specific version of Python, and upgrading it can break your OS. PyEnv solves this by letting you easily install and switch between multiple isolated versions of Python.
In this guide, you will learn how to:
- Install PyEnv and its build dependencies
- Configure your shell to use PyEnv
- Install different Python versions
- Manage virtual environments easily
1. Establishing the Build Environment
PyEnv compiles Python from source. To do this, you need a set of build dependencies installed on your system.
Ubuntu / Debian
| |
Fedora / RHEL
| |
2. Installing PyEnv
The easiest way to install PyEnv (along with useful plugins like pyenv-virtualenv) is using the automatic installer:
| |
3. Configuring Your Shell
This is the most critical step. PyEnv needs to be loaded in your shell environment to work.
For Bash Users (~/.bashrc)
Add the following lines to the end of your ~/.bashrc file:
| |
Then reload your shell:
| |
For Zsh Users (~/.zshrc)
Add the following lines to the end of your ~/.zshrc file:
| |
Then reload your shell:
| |
4. Basic Usage
Installing a Python Version
List available versions:
| |
Install a specific version (e.g., 3.10.4):
| |
Switching Versions
Global: Sets the default Python version for your user.
| |
Local: Sets a Python version ONLY for the current directory (creates a .python-version file).
| |
5. Using Virtual Environments
The installer includes pyenv-virtualenv, which makes virtualenvs first-class citizens.
Create a virtualenv:
| |
Activate it automatically: Navigate to your project folder and set the local version to name of your virtualenv.
| |
Now, whenever you enter my_project_folder, the virtual environment will strictly activate automatically!
6. Keeping PyEnv Updated
To update PyEnv to the latest version later on:
| |
Troubleshooting
- “Command not found”: Did you restart your shell after editing
.bashrc? - Build failures: You are likely missing a dependency from Step 1. Check the error log to see which library header is missing.