1. What Is pyenv?
For Python developers, it is common to require different Python versions for different projects. This is where pyenv becomes useful. pyenv is a tool that allows you to manage multiple Python versions and easily switch between them per project.
Challenges in Python Version Management
As Python development progresses, you may encounter situations where different projects require different Python versions. For example, one project might need Python 3.9, while another requires Python 2.7. Usually, a system only has one Python version installed, making switching versions cumbersome. pyenv solves this issue efficiently.
Benefits of pyenv
Using pyenv provides several advantages:
- Easy Version Switching: With
pyenv, you can easily switch Python versions globally or on a per-project basis. - Manage Multiple Versions: You can manage multiple installed Python versions using a single command, allowing flexible handling of dependency differences between projects.
- Works on Many OS Platforms Except Windows: Available on various operating systems such as macOS and Linux.
How pyenv Works
pyenv creates separate directories for each Python version and installs them there. You can specify the Python version used system-wide or within a specific project using commands such as pyenv global and pyenv local.

2. How to Install pyenv
Overview
To use pyenv, you first need to install it on your system. This section explains how to install pyenv on macOS and Linux. Since pyenv is not supported on Windows, Windows users should consider alternative environments such as WSL or Anaconda.
Installing pyenv on macOS
On macOS, you can easily install pyenv using Homebrew, a package manager that simplifies software installation. Follow the steps below:
- Check if Homebrew Is Installed
Open the Terminal and run the following command:
brew --versionIf the version information appears, Homebrew is already installed. If not, visit the official website (Homebrew Official Site) to install it.
- Install pyenv
Once Homebrew is installed, run the following command:
brew install pyenvAfter the installation completes, proceed to the next step.
- Set Environment Variables
To usepyenv, add the required environment variables to your shell configuration file (.bash_profileor.zshrc).
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init --path)"' >> ~/.bash_profileIf you use .zshrc, add these lines there instead. Restart the terminal or run:
source ~/.bash_profile- Verify the Installation
Run the following command to confirm thatpyenvis installed correctly:
pyenv --versionInstalling pyenv on Linux
Linux installation is similar to macOS, but uses package managers like apt or yum. Below are the steps for Ubuntu:
- Install Required Packages
Run the following commands to install dependencies:
sudo apt update
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git- Install pyenv
Clone the pyenv repository and install it:
curl https://pyenv.run | bash- Set Environment Variables
Add the following to.bashrcor.zshrc:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"Restart the terminal or run source ~/.bashrc.
- Verify Installation
Run this command:
pyenv --versionTroubleshooting
If errors occur during installation, missing dependency libraries may be the cause. Linux systems may require different libraries, so refer to the official documentation and install any required packages.

3. Managing Python Versions
Once pyenv is installed, you can begin managing Python versions. This section explains how to install and switch between versions using pyenv.
Installing Python Versions
pyenv allows you to easily install different Python versions. For instance, if one project requires Python 3.8 and another needs Python 3.9, you can install both:
- Check Available Versions
Display a list of versions that can be installed:
pyenv install --listThis shows all Python versions supported by pyenv.
- Install a Specific Version
To install a version, run:
pyenv install 3.9.1This installs Python 3.9.1 on your system.
Switching Python Versions
With pyenv, switching between installed versions is simple, letting you use different Python environments per project.
- Switch System-Wide Version
Run:
pyenv global 3.9.1- Switch Version per Project
Usepyenv localfor per-project configuration:
pyenv local 3.8.0- Check Current Version
Run the following command:
pyenv versionUninstalling Versions
If a version is no longer needed, remove it:
pyenv uninstall 3.9.1
4. Using pyenv with virtualenv
While pyenv excels at version management, combining it with virtualenv is common for efficient dependency and environment isolation. This section explains how they work together.
What Is virtualenv?
virtualenv creates isolated environments for each project, allowing dependencies to be installed independently. For example, Project A can use requests version 2.0, while Project B uses version 3.0, without conflict.
Differences Between pyenv and virtualenv
pyenv focuses on Python version management, while virtualenv manages dependency isolation within environments.
- Manage Multiple Python Versions: Install and select optimal versions for each project.
- Manage Package Dependencies per Project: Prevent conflicts by separating environments.
Installing pyenv-virtualenv
By installing the pyenv-virtualenv plugin, you can manage virtual environments more easily.
- Installation
Run the following command:
brew install pyenv-virtualenvLinux users can run:
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv- Set Environment Variables
Add:
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile
source ~/.bash_profileCreating a Virtual Environment
pyenv virtualenv 3.8.0 my_project_envActivating a Virtual Environment
pyenv local my_project_envRemoving a Virtual Environment
pyenv uninstall my_project_env
5. Notes on Using pyenv
pyenv and virtualenv are powerful tools, but there are considerations and common pitfalls you should be aware of.
1. Limited Windows Support
pyenv is designed primarily for macOS and Linux. Windows users should rely on:
- Windows Subsystem for Linux (WSL)
- Anaconda
2. Missing Dependency Errors
If errors like:
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?appear, install missing libraries:
sudo apt-get install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev3. Conflict Between System Python and pyenv
Ensure that your shell config includes:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"4. Installation Failure for Specific Versions
Install missing dependencies, then run pyenv install again.
5. Incorrect Version Specification
Verify versions using:
pyenv install --list6. Virtual Environment Conflicts
Create separate environments per project to avoid dependency conflicts.
6. Summary and Next Steps
pyenv is a powerful tool for managing Python versions. Combined with virtualenv, it allows complete separation of environments and dependencies, enabling efficient development workflows.
Key Takeaways
- Easy Version Switching
- Seamless Virtual Environment Integration
- Troubleshooting Best Practices
Next Steps
- Enhance Efficiency with Tools Like pipenv and poetry
- Consider WSL or Anaconda for Windows
- Try pyenv in Real Projects


