Andi Ashari

Tech Voyager & Digital Visionary

Easy Steps to Set Up PyENV on Ubuntu for Beginners

Easy Steps to Set Up PyENV on Ubuntu for Beginners

PyENV is an indispensable tool for developers looking to manage multiple Python versions on a single machine. It facilitates smooth transitions between different Python environments, making it essential for projects that require specific Python versions. This versatility is invaluable for ensuring code compatibility across various Python environments.

Why Choose PyENV?

  • Versatility with Python Versions: Effortlessly switch between Python versions.
  • Project-Specific Python Setup: Assign unique Python versions to distinct projects.
  • Testing Across Versions: Ensure your code performs seamlessly across different Python versions.
  • Independence from System Python: Operates independently of the system’s Python installation.

Setting Up PyENV on Ubuntu

Follow this step-by-step guide to install PyENV on your Ubuntu machine.

Step 1: Refresh Your Ubuntu Package List

Update your package lists to ensure you’re getting the latest versions:

sudo apt update

Step 2 (Optional): Installing Prerequisites

Some additional packages may be required for PyENV to function properly. Install them using:

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

Step 3: PyENV Installation

Install PyENV by executing the installer script:

curl | bash

Step 4: Setting Up Environment Variables

To integrate PyENV into your shell, add it to your PATH:

For Bash Users

Append these lines to your ~/.bashrc:

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

For Zsh Users

Zsh users should add these lines to ~/.zshrc:

echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc

Step 5: Restart Your Shell

Activate the changes by restarting your shell:

exec "$SHELL"

Step 6: Confirming PyENV Installation

Verify PyENV is correctly installed:

pyenv --version

A successful installation will show the PyENV version number.

Installing Specific Python Versions with PyENV

With PyENV set up, you can now install specific Python versions effortlessly.

Step 1: Explore Available Python Versions

List all available versions with:

pyenv install --list

Choose your desired version from the list.

Step 2: Installing Your Chosen Python Version

For instance, to install Python 3.8.10:

pyenv install 3.8.10

This command downloads and sets up Python 3.8.10 on your system.

Step 3: Selecting a Global Python Version (Optional)

Set Python 3.8.10 as your default version:

pyenv global 3.8.10

Alternatively, specify it as the local version for a particular project using pyenv local 3.8.10.

Running Python Scripts Using PyENV

Let’s execute a Python script using the installed Python version.

Step 1: Crafting a Python Script

Create a file with the following content:

from platform import python_version
print("Hello from Python", python_version())

Step 2: Running the Script with PyENV

Execute the script with the Python version you’ve installed. If it’s set as global or local:


Otherwise, utilize PyENV’s exec command:

pyenv exec python

Congratulations! You’ve successfully installed PyENV on Ubuntu, set up a specific Python version, and executed Python scripts using PyENV. This tool greatly simplifies managing multiple Python environments, enhancing your development workflow.