Skip to content

Instantly share code, notes, and snippets.

@ebb-earl-co
Last active March 16, 2023 17:34
Show Gist options
  • Save ebb-earl-co/d23779ac5a03ef3b3ede3e8923bdd646 to your computer and use it in GitHub Desktop.
Save ebb-earl-co/d23779ac5a03ef3b3ede3e8923bdd646 to your computer and use it in GitHub Desktop.
Multiple Versions of Python using Pyenv on Ubuntu 22.04
# First, the system dependencies needed from APT;
# Pyenv downloads the source tarball from the Python
# project, and compiles it, so the necessary libraries
# for `python3` must be installed on Ubuntu. For example,
# libsqlite3-dev is a dependence because Python3 includes
# sqlite3 as a default module. Also for example, asking pyenv
# for python 3.9 outputs the following log message:
# Downloading Python-3.9.16.tar.xz... -> https://www.python.org/ftp/python/3.9.16/Python-3.9.16.tar.xz
sudo apt-get update -qq && \
sudo apt-get 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
# Piping curl to Bash isn't a super idea, but we're going to roll with it here
curl https://pyenv.run | bash
# Add the following lines to the BOTTOM of ~/.bashrc
# pyenv
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
# Now, close whatever terminal you're using and open a new terminal (restarting the shell)
# and check if pyenv is installed correctly with
pyenv versions
# Let's grab Python 3.9 because Ubuntu 22.04 has Python 3.10 as its system version
pyenv install 3.9
# If, at some point, you installed a Python version with pyenv and not all of these
# steps were done correctly OR you simply want to remove a version that you had installed,
# do the following (using 3.9 as an example). NOTE you have to pay attention to the EXACT
# version installed; e.g. `3.9.16` is the one downloaded from pyenv when only `pyenv install 3.9`
# is specified
pyenv uninstall 3.9.16
# Now, CD into the directory where your project requiring a special version of Python is; e.g.
# cd ~/projects/py39-project and execute the following to have the Ubuntu command `python3`
# refer to python3.9 IN THAT DIRECTORY ONLY
pyenv local 3.9.16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment