🚀 How to Install pyenv on Ubuntu (The Easy Way)

🚀 How to Install pyenv on Ubuntu (The Easy Way)

🚀 How to Install pyenv on Ubuntu (The Easy Way)

If you’ve ever juggled multiple Python versions on your machine and felt the pain of dependency conflicts, pyenv is your new best friend. It's a simple yet powerful tool that allows you to install and manage multiple Python versions on the same system without headaches.

In this guide, I’ll walk you through how to install pyenv on Ubuntu step by step. Whether you're a beginner or a seasoned developer, this will help you get started quickly and cleanly.


🧰 What is pyenv and Why Should You Use It?

pyenv lets you easily:

  • Install multiple versions of Python side-by-side
  • Switch Python versions globally or per-project
  • Create isolated environments (with pyenv-virtualenv)

This is especially useful if you're working on different projects that require different Python versions.


🛠️ Step 1: Install Required Dependencies

Before installing pyenv, make sure your system has the necessary build tools and libraries:

sudo apt update
sudo apt 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

These packages are essential for compiling and running different versions of Python from source.


📥 Step 2: Install pyenv via Curl

Use the official installation script to install pyenv and its plugins:

curl https://pyenv.run | bash

This will install:

  • pyenv
  • pyenv-virtualenv (for managing virtual environments)
  • pyenv-update (for keeping it up-to-date)
  • pyenv-doctor (for troubleshooting)

🧠 Step 3: Add pyenv to Your Shell

Now, we need to let your shell know about pyenv. Add the following lines to your shell configuration file.

If you use bash:

nano ~/.bashrc

Add this at the end of the file:

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Then reload your shell:

source ~/.bashrc

If you use zsh:

Do the same in ~/.zshrc, and run:

source ~/.zshrc

✅ Step 4: Verify the Installation

You’re almost done! To make sure everything is working, run:

pyenv --version

You should see the installed version of pyenv. If that works, you’re ready to start installing Python versions.


🐍 Step 5: Install a Python Version

Let’s install Python 3.12.2 as an example:

pyenv install 3.12.2

Once installed, set it as the global (default) Python version:

pyenv global 3.12.2

You can confirm it with:

python --version

📦 Bonus: Create a Virtual Environment

Want an isolated environment for a project?

pyenv virtualenv 3.12.2 myenv
pyenv activate myenv

Now you can install packages without affecting your system Python.


💡 Final Thoughts

Managing Python versions doesn’t have to be messy. pyenv gives you clean control over your development environment, especially when working across projects with different dependencies.

If you're working on Ubuntu, this setup will save you time and headaches in the long run. Once you’ve got pyenv running, pair it with tools like poetry or pip-tools to take your Python workflow to the next level.

Got questions? Drop them in the comments — I’m happy to help.