# Installing Pyenv

These instructions are what I usually follow in my system. You can check Pyenv's github page to see the official instructions.

# Install the prerequisites

To compile Python interpreters, you will need some basic compiling tools, as well as libraries (such as openssl) required by some of Python's own libraries.

See here for the official instructions. What I do is the following.

For Ubuntu/Mint, run

sudo apt-get update
sudo apt-get install 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-dev4

For Arch/Manjaro, run

pacman -S --needed base-devel openssl zlib xz

# Install Pyenv

I use the Pyenv installer to install Pyenv automatically. This also installs some useful plugins, such as pyenv-virtualenv to manage virtual environments. To do so, run

curl https://pyenv.run | bash

If you are not comfortable with running code from a random website, you can either

# Add Pyenv to the terminal's path

Now, you have to tell the terminal where it can find the Pyenv installation. You can do this in two ways.

# Automatically

See the section Configure your shell's environment for Pyenv on Pyenv's manual installation instructions and run the commands there.

For Debian/Ubuntu/Mint, you can run the following commands on the terminal.

# the sed invocation inserts the lines at the start of the file
# after any initial comment lines
sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc

# Manually

You will need to paste the following lines to your terminal's initialization scripts (in Ubuntu/Mint, these will be ~/.bashrc and ~/.profile)

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

eval "$(pyenv init --path)"

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
export PYENV_VIRTUALENV_DISABLE_PROMPT=1

In Debian/Ubuntu/Mint:

  • The first two lines should go to the beginning of ~/.profile
  • The third line should go to the end of ~/.profile
  • The last three lines should go to the end of ~/.bashrc

In Arch/Manjaro:

  • All the lines can go to the end of ~/.bashrc.

# Restart the shell

Now, close the shell and open it again, or run a command like, so that the terminal recognizes the pyenv commands.

exec $SHELL

# Install your first Python version

You are now ready how to install a new Python version. We will also make it the global Python version that will be used by default when you call a terminal.

First, choose the version you want to install. There are many available, including pypy and conda. You can see all the available version with the command

pyenv install --list

We will install Python 3.10.2.

pyenv install 3.10.2

Create a new virtual environment called "py" (it can be named anything else!)

pyenv virtualenv 3.10.2 py

Make it the global python environment

pyenv global py

Activate it and you can now install stuff! This will be the default virtual environment every time you open a shell.

pyenv activate py
pip install --upgrade pip
pip install ipython

And that's it! You can install as many Python versions and virtualenvs as you'd like.