Karpton Logo

How to Install pyenv

Written by Toni Karppi on

Diffrent projects may have diffrent requirements of the version of Python that they support. The tool pyenv works as a version manager for Python to make it easy to switch between versions of Python. In this tutorial I’ll be showing you how to install and use pyenv. Note: you should under no circumstances uninstall the system level Python. There are most likely applications running on your system that depend on it being present, and things will probably break without it.

Installing pyenv

Start off by cloning the project to your system

git clone https://github.com/pyenv/pyenv.git ~/.pyenv

The next step depends if you’re running bash or zsh.

Using Bash

Add the following lines to the end of ~/.bash_profile

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

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
fi

Then source the file to call the exports

source ~/.bash_profile

Using ZSH

I’ll be assuming that you’re using ZSH with oh-my-zsh. In this case all we need to do is to add the pyenv plugin to the plugins array in ~/.zshrc.

plugins=(
  pyenv
)

Then source to activate the changes.

source ~/.zshrc

Using pyenv

At this point, zsh should be accessible through your terminal. To check this, enter:

pyenv --version

To check which commands are available, enter:

pyenv --help

To list out all available versions of Python, enter:

pyenv install -l

Let’s install the latest stable version as of this writing, 3.7.3. This may take some time to complete.

pyenv install 3.7.3

You can check that the new version was installed.

pyenv versions

The system interpreter is still set as the default (indicated by the *). We want to set 3.7.3 as the default interpreter.

pyenv global 3.7.3

Verify that we actually are using the new installation.

python --version

If you want to use another version of Python, just repeat the above steps with the version you want to use.

What’s next

Now you should have a pretty good idea of how pyenv works. Another option that you may want to look into is the pyenv local command, which allows you to use a specific version of Python for a particular project, while keeping the global Python interpreter outside of the project. Thanks for reading!