Different projects often need different Python versions. This guide covers two ways to manage Python versions on WSL Ubuntu: pyenv (recommended) for per-project version control, and version-specific virtual environments for interpreters installed through apt.
The examples in this guide are run on WSL2 Ubuntu in Windows, but they work the same on any Ubuntu system.
Prerequisites
- WSL 2 with Ubuntu installed: see How to Install Ubuntu in WSL 2 on Windows if you need to set this up
The pyenv method needs build dependencies for compiling Python from source. Install them first:
sudo apt update && sudo apt install -y make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses-dev \
xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev git
Method 1: pyenv (Recommended)
pyenv lets you install any Python version and switch between them per project: without touching your system Python. It works like nvm does for Node.js.
Step 1: Install pyenv
Clone the pyenv repository and add it to your shell:
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
Add pyenv to your ~/.bashrc:
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo '[[ -d "$PYENV_ROOT/bin" ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
Add these lines once. For login shells, also add the same initialization to your existing ~/.profile or ~/.bash_profile as described in the pyenv README. Load the changes:
source ~/.bashrc
Verify pyenv is working:
pyenv --version
Step 2: Install a Python Version
See what’s available:
pyenv install --list | grep "^\s*3\."
Use a current pyenv checkout. Resolve the latest known stable patch in the 3.14 series, then install that exact version. Keep PYTHON_VERSION available for the later examples. Compilation takes a few minutes:
PYTHON_VERSION="$(pyenv latest -k 3.14)"
pyenv install "$PYTHON_VERSION"
Install as many versions as you need:
pyenv install 3.11
pyenv install 3.13
Step 3: Set Your Default Python Version
Set a global default that applies to all new terminal sessions:
pyenv global "$PYTHON_VERSION"
Verify:
python --version
Step 4: Set Per-Project Python Versions
This is where pyenv really helps. Set a specific Python version for a project directory:
cd ~/projects/my-app
pyenv local "$(pyenv latest 3.11)"
This creates a .python-version file in the directory. Whenever you cd into this folder, pyenv automatically switches to that version. Commit this file to your repo so your team uses the same version.
List all installed versions:
pyenv versions
Create Virtual Environments with pyenv
pyenv manages Python versions. For isolated project dependencies, add the pyenv-virtualenv plugin.
Install pyenv-virtualenv
git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
source ~/.bashrc
Create and Use a Virtual Environment
Create a virtualenv tied to a specific Python version:
pyenv virtualenv "$PYTHON_VERSION" my-project-env
Activate it manually:
pyenv activate my-project-env
Or set it as the local version for a project directory so it activates automatically:
cd ~/projects/my-app
pyenv local my-project-env
Now every time you cd into ~/projects/my-app, the virtualenv activates. When you leave the directory, it deactivates.
Method 2: apt Interpreters with Project Virtual Environments
Keep /usr/bin/python3 unchanged. Ubuntu tools depend on its distro-managed interpreter; do not register it with update-alternatives. On Ubuntu 24.04, Python 3.12 is already the system version. For another version, the deadsnakes community PPA provides Python 3.11 for Noble. This is a third-party package source, so review its support policy before adding it.
sudo apt install -y software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install -y python3.11 python3.11-venv
python3.11 -m venv .venv
source .venv/bin/activate
python --version
python -m pip install requests
deactivate
Create the venv inside your project directory. Call python3.11 explicitly outside it. Current Ubuntu releases protect the system environment under PEP 668; use a venv rather than sudo pip or –break-system-packages.
Alternative: Manage Python with uv
Astral’s uv downloads managed Python builds without compiling them locally. After following its installation guide, you can create a project environment with:
uv python install 3.14
uv venv --python 3.14 .venv
source .venv/bin/activate
python --version
Useful pyenv Commands
| Command | What it does |
|---|---|
pyenv install --list |
Lists all available Python versions |
PYTHON_VERSION="$(pyenv latest -k 3.14)"
pyenv install "$PYTHON_VERSION" |
Installs a specific version |
pyenv global "$PYTHON_VERSION" |
Sets the default version |
pyenv local "$(pyenv latest 3.11)" |
Sets version for current directory |
pyenv versions |
Lists all installed versions |
pyenv uninstall VERSION |
Removes a version |
pyenv which python |
Shows path to the active Python binary |
Conclusion
Use pyenv if you work on multiple projects with different Python requirements: it handles versions, per-directory switching, and virtual environments. Use a version-specific apt interpreter with a venv when you prefer packaged Python. Keep the Ubuntu system interpreter unchanged.
For better code quality in your Python projects, check out How to Use Flake8 and Black for Python Code Quality or set up pre-commit hooks on Ubuntu WSL 2.
For a project layout that works across scripts and cloud functions, see structuring Python projects for APIs and CLI tools.


