English

Install pyenv on Ubuntu and Debian


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

bgasparotto

View Comments

  • 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 "~ $"...

    • 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.

  • 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...

  • 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.

    • 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?

Recent Posts

Python function decorator

This guide will show you how to create a Python function decorator with a few…

3 years ago

Got permission denied while trying to connect to the Docker daemon socket

This guide will show you how to fix the error Got permission denied while trying…

3 years ago

Python virtual environment on Intellij IDEA

This guide will show you how to create a Python virtual environment on Intellij IDEA…

3 years ago

Find and kill processes on Linux and Mac by port number

This tutorial will quickly show you how to to find and kill processes on Linux,…

3 years ago

Python: Relocation R_X86_64_PC32 against symbol can not be used when making a shared object Error

This guide shows a possible solution for Python error Relocation R_X86_64_PC32 against symbol can not…

3 years ago

Kubernetes useful commands

I condensed below a cheat sheet of Kubernetes useful commands. I will keep updating this…

3 years ago