How to Install and Use pyenv on Windows, Ubuntu, and macOS
Managing multiple Python versions across different operating systems can be a challenge. Whether you're developing projects that require different Python versions or simply want better control over your development environment, pyenv
makes it easy to install, switch, and manage multiple Python versions seamlessly. This guide will walk you through the setup of pyenv
on Windows, Ubuntu, and macOS, with detailed instructions for each.
1. Installing pyenv on Windows
Setting up pyenv
on Windows is straightforward thanks to the pyenv-win
package.
Step 1: Install pyenv-win
Open PowerShell or Command Prompt as an administrator.
Install
pyenv-win
with the following command:pip install pyenv-win --target C:\Users\<username>\.pyenv
Step 2: Update Environment Variables
Right-click on This PC → Properties → Advanced system settings → Environment Variables.
Under System variables, find and select Path, then click Edit.
Add the following paths:
C:\Users\<username>\.pyenv\pyenv-win\binC:\Users\<username>\.pyenv\pyenv-win\shims
Step 3: Install Python Versions
To install a specific Python version, use:
pyenv install 3.9.5
Replace 3.9.5
with any version you want.
Step 4: Set the Global or Local Python Version
To set a global version of Python:
pyenv global 3.9.5
To set a local Python version for a specific project or directory:
pyenv local 3.9.5
Step 5: Verify Python Version
Check which version of Python is currently active:
pyenv version
2. Installing pyenv on Ubuntu
Setting up pyenv
on Ubuntu involves a few dependencies and commands.
Step 1: Install Dependencies
Open the terminal and install dependencies:
sudo apt updatesudo apt install -y make 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
Step 2: Install pyenv
Clone the pyenv
repository to your home directory:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Step 3: Update Shell Configuration
Add pyenv
to your shell's startup file (.bashrc
or .zshrc
):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrcecho 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
source ~/.bashrc
Step 4: Install Python Versions
Install any version of Python using:
pyenv install 3.9.5
Step 5: Set Global or Local Python Version
To set a global Python version:
pyenv global 3.9.5
For a local version:
pyenv local 3.9.5
3. Installing pyenv on macOS
Step 1: Install Homebrew (If Not Installed)
First, install Homebrew (the macOS package manager), if you haven't already:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Step 2: Install pyenv
Once Homebrew is installed, you can easily install pyenv
:
brew install pyenv
Step 3: Update Shell Configuration
Add pyenv
to your shell configuration file (.bash_profile
, .zshrc
, or .bashrc
):
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profileecho 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init --path)"' >> ~/.bash_profile
source ~/.bash_profile
Step 4: Install Python Versions
To install Python 3.9.5 or any version, run:
pyenv install 3.9.5
Step 5: Set Global or Local Python Version
To set a global Python version:
pyenv global 3.9.5
For a local version:
pyenv local 3.9.5
Common pyenv Commands
Here are some useful pyenv
commands to manage Python versions:
List installed Python versions:
pyenv versionsList available Python versions:
pyenv install --listInstall a specific Python version:
pyenv install <version>Set global Python version:
pyenv global <version>Set local Python version:
pyenv local <version>Check active Python version:
pyenv version
Conclusion
Whether you're working on Windows, Ubuntu, or macOS, pyenv
is a powerful tool to easily manage multiple Python versions. Follow the steps outlined in this guide to set up pyenv
on your system and take full control over your Python development environment.
Thanks for sharing your feedback! It helps us grow.