skip to content

Python Installation β€” WSL Ubuntu

Install Python 3 inside WSL2/Ubuntu via apt, the deadsnakes PPA, or pyenv. Covers the python3-venv quirk and version aliasing.

3 min read 12 snippets yesterday quick read

Python Installation β€” WSL Ubuntu#

Prerequisites#

Ensure WSL2 is installed and you have an Ubuntu distro running. Update packages first:

sudo apt update && sudo apt upgrade -y

Method 1 β€” apt (quickest for system Python)#

Ubuntu ships Python 3, but often an older minor version. This is fine for scripts; for projects pin the version explicitly.

sudo apt install python3 python3-pip python3-venv -y

Output:

Reading package lists... Done
Building dependency tree... Done
The following NEW packages will be installed:
  python3 python3-pip python3-venv
0 upgraded, 3 newly installed, 0 to remove and 0 not upgraded.

[!WARNING] The python3-venv gap β€” python3 -m venv fails with ensurepip is not available if python3-venv is not installed separately. Always run sudo apt install python3-venv even if python3 is already present.

Method 2 β€” deadsnakes PPA (specific newer version)#

The deadsnakes PPA provides newer Python versions not yet in the Ubuntu main repos:

sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-dev -y

Output:

Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu jammy/main amd64 python3.12 amd64 3.12.3-1+jammy1 [621 kB]
...
Setting up python3.12 (3.12.3-1+jammy1) ...

Method 3 β€” pyenv (manage multiple versions)#

pyenv lets you install and switch between any Python version without affecting system Python:

# Install pyenv dependencies
sudo apt 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

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

# Add to ~/.bashrc or ~/.zshrc
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'command -v pyenv >/dev/null || export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc
source ~/.bashrc

# Install Python
pyenv install 3.12.3
pyenv global 3.12.3

Output:

Downloading Python-3.12.3.tar.xz...
Installing Python-3.12.3...
Installed Python-3.12.3 to /home/user/.pyenv/versions/3.12.3

Set python3.12 as the default python3#

If you installed via deadsnakes and want python3 to point to 3.12:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 1
sudo update-alternatives --config python3

Output:

There are 2 choices for the alternative python3:

  Selection    Path                      Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.10        2         auto mode
  1            /usr/bin/python3.10        2         manual mode
  2            /usr/bin/python3.12        1         manual mode

Press <enter> to keep the current choice, or type selection number: 2
update-alternatives: using /usr/bin/python3.12 to provide /usr/bin/python3

[!WARNING] Do not point the system python3 away from the distro default on Ubuntu β€” some system tools (e.g. apt, do-release-upgrade) depend on the original interpreter. Use update-alternatives carefully, or prefer pyenv/venv instead.

Verify#

python3 --version
python3 -m pip --version
python3 -c "import venv; print('venv OK')"

Output:

Python 3.12.3
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
venv OK

Next steps#

python3 -m venv .venv
source .venv/bin/activate

See Virtual Environments for the full guide.