Last modified: May 25, 2025 By Alexander Williams

Install Multiple Python Versions with pyenv

Managing multiple Python versions is easy with pyenv. This tool lets you switch between versions seamlessly. Follow this guide to install and use pyenv.

What is pyenv?

pyenv is a Python version manager. It allows you to install and switch between different Python versions. This is useful for testing and development.

You can use pyenv on Linux, macOS, and Windows (via WSL). It works alongside tools like virtualenv.

Prerequisites

Before installing pyenv, ensure you have:

  • A Unix-based system (Linux/macOS) or WSL for Windows
  • Basic command-line knowledge
  • Git installed

Install pyenv

Linux/macOS Installation

Use the automatic installer for simplicity. Run this command:


curl https://pyenv.run | bash

Add these lines to your shell configuration file (.bashrc, .zshrc):


export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

Windows Installation

For Windows, install pyenv through WSL. Follow our guide on Python installation first.

Install Python Versions

List available Python versions with:


pyenv install --list

Install a specific version (e.g., Python 3.9.7):


pyenv install 3.9.7

This downloads and compiles Python. It may take a few minutes.

Manage Python Versions

Set a global default version:


pyenv global 3.9.7

Set a local version for a specific project:


pyenv local 3.8.12

This creates a .python-version file in your project directory.

Verify Installation

Check your current Python version:


python --version

Output should match your selected version:


Python 3.9.7

Switch Between Versions

List installed versions:


pyenv versions

Switch versions temporarily:


pyenv shell 3.8.12

Uninstall Python Versions

Remove unused versions to save space:


pyenv uninstall 3.7.12

Using pyenv with Virtual Environments

Combine pyenv with virtual environments for better isolation. Create a virtualenv with:


pyenv virtualenv 3.9.7 my-project-env

Common Issues

If you get build errors, install required dependencies. For Ubuntu/Debian:


sudo apt-get install -y 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

Conclusion

pyenv simplifies Python version management. You can easily install, switch, and remove versions. Combine it with pip for package management.

Now you can work on projects requiring different Python versions. No more conflicts or complex setups.