Skip to content

Instantly share code, notes, and snippets.

@franTarkenton
Last active February 14, 2024 17:56
Show Gist options
  • Save franTarkenton/6fe2ad9a0ed9861d041b76d2ce10ad5c to your computer and use it in GitHub Desktop.
Save franTarkenton/6fe2ad9a0ed9861d041b76d2ce10ad5c to your computer and use it in GitHub Desktop.
Quick guide to managing multiple versions of python in WSL / ubuntu

Options

Option 1: Pyenv (preferred)

Already installed, just want to select an env:

pyenv shell 3.12
# or globally
pyenv global 3.12

stolen from: https://realpython.com/intro-to-pyenv/

by default pyenv installs to ~/.pyenv/versions

make sure linux brew paths are not in the PATH env var.

export PATH=$(echo "$PATH" | sed -e 's/:\/home\/linuxbrew\/\.linuxbrew\/sbin//g' -e 's/:\/home\/linuxbrew\/\.linuxbrew\/bin//g')

or alternatively (not tested)

brew install gdbm

or

brew unlink pkg-config

and while you are at it might as well remove windows paths that show up by default in WSL

export PATH=$(echo "$PATH" | sed -e 's/:[^:]*\/mnt\/c[^:]*//g')

Install dependencies

$ sudo apt-get 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 python3-openssl

Install pyenv

curl https://pyenv.run | bash

list versions

pyenv install --list | grep " 3\.*"

Install specific dependency

pyenv install -v 3.12

List installed

pyenv versions

Use a specific version - in current shell

pyenv shell 3.12

Set global default

pyenv global 3.12

Option 2: Install Deadsnake

stolen from: https://hackersandslackers.com/multiple-python-versions-ubuntu-20-04/

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt upgrade

list available pythons

apt list | grep python3.x

install

sudo apt-get install <python-version>

swap versions of python

configure alternatives

update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2

list alternative configs

update-alternatives --list python3

swap versions

update-alternatives --config python3

setup other dependencies (pip / venv / distutils)

sudo apt remove --purge python3-apt
sudo apt autoclean
sudo apt install python3-apt

sudo apt install python3.9-distutils
apt install python3-pip
sudo apt install python3.9-venv

install dev dependencies

sudo apt install python3.11-dev

alt approach, install via brew

  • either setup dev env to use that version of python
  • configure PATH env var to use version of python in brew

Pyenv

Haven't used this approach, but this is a link to some docs: https://github.com/pyenv/pyenv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment