Skip to content

Installing Python on WSL2 (Ubuntu)

Install build tools and required Python libraries

Install build tools and required Python libraries
sudo apt update && sudo apt install -y build-essential git libexpat1-dev libssl-dev zlib1g-dev \
libncurses5-dev libbz2-dev liblzma-dev \
libsqlite3-dev libffi-dev tcl-dev linux-headers-generic libgdbm-dev \
libreadline-dev tk tk-dev

Install pyenv

Install pyenv
curl https://pyenv.run | bash

Update ~/.bashrc, ~/.profile, ~/.bash_profile (read instructions from previous output)

~/.bashrc
...

# Load pyenv automatically by adding
# the following to ~/.bashrc:

export PATH="/home/{USERNAME}/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
~/.profile
# Load pyenv automatically by appending
# the following to
# ~/.bash_profile if it exists, otherwise ~/.profile (for login shells)
# and ~/.bashrc (for interactive shells) :

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"

Install Python "common" libraries

Install needed version(s) ... can install multiple versions
# list available python versions
# pyenv install --list
pyenv install 3.14.2

Install / Upgrade base libraries for installed Python version(s)

Install / Upgrade base libraries into installed version(s) of Python
pyenv global 3.14.2
pip install pip --upgrade
pip install pipenv