Skip to content

Install pyenv on Ubuntu and Debian

Last Updated on 28/12/2023

install pyenv on ubuntu logo


This tutorial will show how to install pyenv on Ubuntu and other Debian distributions, so you can quickly switch between Python versions.

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

Install pyenv

curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash

Validate installation

The command below should print the installed version:

pyenv -v

which in my case printed:

pyenv 2.1.0

If it doesn’t, then add the following lines at the end of your:

  • .bashrc if you use bash
  • .zshrc if you use zsh
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
 eval "$(pyenv init --path)"
fi
eval "$(pyenv virtualenv-init -)"

Then reload your shell and it should work.

Installing Python versions

Run the command below to list the available Python versions to be installed:

pyenv install --list

Which should yield something like:

Available versions:
  2.1.3
  2.2.3
  ...
  3.10.0
  3.10-dev
  3.11.0a1
  3.11-dev

Then, pick a version of your choice and install it running the following command:

pyenv install 3.10.0

Finally, set the installed version as the default local (to this terminal session) or global (entire system):

pyenv local 3.10.0

References

10 thoughts on “Install pyenv on Ubuntu and Debian”

  1. Absolutely love this page. It’s my go-to reference to install pyenv. Just wish that the “copy” button’s didn’t also copy the leading “~ $”…

    1. Thanks for the feedback! I removed the leading “~ $” and split a command that had the output in the same box, so it should be better to copy them now 🙂

      I will try to figure out a non-intrusive way to format the command boxes with the “~ $” prefix later.

  2. Installed it as instructed but the files were all littered with Windows line returns. Had to use dos2unix on all of the files before it would work…

  3. Nowadays (I’m running pyenv version 2.2.5) the .bash/.zshrc config must look different:
    “`
    export PYENV_ROOT=”$HOME/.pyenv”
    export PATH=”$PYENV_ROOT/bin:$PATH”
    eval “$(pyenv init –path)”
    eval “$(pyenv init -)”
    “`
    Works for me.

    1. Normally you need to install git manually on Alpine and Solus distros, but Ubuntu should already have it on a fresh install. Which distro are you using?

Leave a Reply

Your email address will not be published. Required fields are marked *