How to install Pyenv in Ubuntu

If you have been working with Python for a while, you would know that each and every projects have their own Python version requirements. Pyenv is a tool created to manage multiple different Python version in the same machine. With Pyenv, each project can be linked to a different Python version, inside its own virtual environment (using virtualenv). One thing I like the most about Pyenv is its automatic virtualenv activation/deactivation upon entering project directory. Just one more nifty thing to make my workflow leaner.

Pyenv isn’t written on Python, but rather pure shell scripts. That means there’s no apt package for easy install. In this article, we’ll show you how to install Pyenv in Ubuntu. And no, it doesn’t run on Windows except inside the WSL.

Pyenv manages Python virtual environments with pipenv and virtualenv

Install Pyenv using installer

Pyenv developers offers an official easy installer which works out of the box for most Linux distributions, including Ubuntu. In order to install Pyenv using its  pyenv-installer, run the following command.

curl | bashCode language: JavaScript (javascript)

If you see something like "curl: command not found", you may have to install curl first, as most Debian-based system doesn’t include it by default. Run the command below to install curl:

sudo apt-get install curlCode language: JavaScript (javascript)

Once the installer finishes its job, restart your shell so that the new changes can take effect by running the following command :

exec $SHELLCode language: PHP (php)

Manually install Pyenv

In case you prefer to set up Pyenv yourself, or the installer doesn’t work properly on your distribution, it’s also extremely easy to install Pyenv manually.

First, you have to install all the necessary libraries and dependencies for Pyenv to work properly. Run the following command to do that:

sudo apt 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 python-openssl 

Once the installation is done, clone Pyenv from ít GitHub repository using the command below:

git clone ~/.pyenvCode language: PHP (php)

The final step before you can use Pyenv is adding it to your $PATH so that the pyenv command is recognized globally. Sequentially run the commands below to do that:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init --path)"' >> ~/.bashrcCode language: PHP (php)

Please do note that the commands are made for bash shell. In case you’re using another shell, you would have to change ~/.bashrc accordingly. For example, for ZSH users, the commands should be :

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


eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"Code language: JavaScript (javascript)

Finally, restart your shell so that the changes can take effect immediately.

exec "$SHELL"Code language: JavaScript (javascript)

Uninstall Pyenv

In contrast to other terminal utilities, uninstalling Pyenv is as easy as installing it. In order to completely uninstall Pyenv, you have to manually remove all of its files as well as its shell startup configuration.

First, delete Pyenv files by running the command below:

rm -rf $(pyenv root)Code language: JavaScript (javascript)

After that, inspect ~/.bash_profile and ~/.bashrc and delete any code block that mentions pyenv. You may see a few lines that looks like these :

# In ~/.bash_profile
if command -v pyenv 1>/dev/null 2>&1; then
    eval"$(pyenv init-)"
fiCode language: PHP (php)
# In ~/.bashrc
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"Code language: PHP (php)

Remove those lines and restart your shell so that the changes can take effect immediately.

exec "$SHELL"Code language: JavaScript (javascript)

We hope this article helped you learn how to install and uninstall Pyenv in Ubuntu. You may also want to see our guide on installing multiple packages in one command with pip and using pipenv in VSCode.

Leave a Comment