๐Ÿ”ง How to Install pyenv on Linux, Ubuntu, WSL2 (2025 Guide)

Rishav MishraRishav Mishra
3 min read

If you ever wanted to have several different versions of Python on a single machine, pyenv is what you need. For coding older projects or trying out new features, pyenv makes installing and maintaining Python versions as easy as can be.

I will walk you through exactly how to install pyenv on Ubuntu (or indeed any Debian-based Linux) โ€“ with all steps and remedies covered.


๐Ÿ’ก What is pyenv?

pyenv is a Python version manager with low overhead. It lets you:

  • Manage several versions of Python.

  • Switch Python versions globally or per-project.

  • Employ virtual environments with pyenv-virtualenv.

No more depending on system Python or struggling with apt dependencies.


โœ… Step 1: Install Dependencies

pyenv compiles Python from source, and you will require some system packages initially:

sudo apt-get install -y 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-dev git

โœ… Step 2: Installing pyenv

Install with the official script:

curl -fsSL https://pyenv.run | bash

This will clone pyenv and its plugins to ~/.pyenv.


โœ… Step 3: Set Up Your Shell (Permanent)

To have pyenv available on every terminal you open, include these lines in your shell configuration file.

For bash users:

nano ~/.bashrc

๐Ÿ’ก Tip: In nano, press Ctrl + O to save, Enter to accept, and Ctrl + X to quit.

Then add this at the bottom of the document:

export PYENV_ROOT="$HOME/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
eval "$(pyenv virtualenv-init -)"

Apply the changes:

source ~/.bashrc

For zsh users, merely replace .bashrc with .zshrc in the above steps.


โœ… Step 4: Installing A Python Version

You are now able to install any Python version. For instance:

pyenv install 3.11.8

See all available versions:

pyenv install --list

โœ… Step 5: Set the Python Default Version

Set it globally:

pyenv global 3.11.8

Or only for a particular project:

pyenv local 3.11.8

Verify Python version:

python --version
which python

โœ… Optional: Create Virtual Environments

If you have installed pyenv-virtualenv (it is installed automatically by this script), you can:

pyenv virtualenv 3.11.8 myenv
pyenv activate myenv

To deactivate:

pyenv deactivate

๐Ÿงผ Bonus: Troubleshooting Tips

  • In case you don't have the pyenv command, check that you installed it correctly and added the shell configuration, then executed source ~/.bashrc.

  • In case a Python build fails, make sure that all dependencies are installed (build-essential, libssl-dev, etc.)

  • When you use WSL, avoid combining pyenv-win with Linux pyenv.


๐Ÿš€ Final Thoughts

You now have pyenv installed and can install and experiment with any Python version without having to touch your system Python. This is an indispensable tool for every serious Python programmer.

If this guide has been helpful for you, give it a ๐Ÿ‘ and follow for future development tips.

0
Subscribe to my newsletter

Read articles from Rishav Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rishav Mishra
Rishav Mishra