Self-paced

Explore our extensive collection of courses designed to help you master various subjects and skills. Whether you're a beginner or an advanced learner, there's something here for everyone.

Bootcamp

Learn live

Join us for our free workshops, webinars, and other events to learn more about our programs and get started on your journey to becoming a developer.

Upcoming live events

Learning library

For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.

It makes sense to start learning by reading and watching videos about fundamentals and how things work.

Search from all Lessons


LoginGet Started
โ† Back to How to's
  • python

  • pyenv

  • windows

  • macos

  • linux

Edit on Github

What is pyenv and How to Install It?

Written by:

What is pyenv?

Pyenv is a tool that allows us to manage different versions of Python on our computer, making it easy to switch between versions as needed for the environment we are developing in.

What is pyenv used for?

Pyenv has multiple uses, providing developers with greater control over their projects, including:

  • Python Version Management: Allows installing, uninstalling, and switching between different Python versions without interfering with the original system Python installation (most Linux and some MacOS computers already have Python pre-installed; we call this the "system Python installation).

  • Environment Isolation: Enables developers to create specific Python environments for each project, ensuring that each project has its own dependencies and isolated packages.

  • Compatibility Testing: Facilitates testing code in different Python versions, ensuring compatibility and identifying potential issues.

  • Package Management: Developers can easily install and manage packages using pyenv, ensuring that specific version requirements for each project are met.

Now let's look at the installation process for different operating systems.

Installing pyenv on Windows

Step 1: Install Git if you don't have it installed already. We recommend to read our guide on how to install git for windows or You can download it from the official page. The video tutorial is also recommended.

Step 2: Next step is to install pyenv-win. Open the Git Bash or PowerShell terminal and clone the pyenv repository from GitHub with the following command:

1git clone https://github.com/pyenv-win/pyenv-win.git $HOME/.pyenv

๐Ÿ“ This will download the pyenv-win repository into a new directory called .pyenv that will live under your system user folder.

Step 3: Open the windows PowerShell command line and add PYENV, PYENV_HOME, and PYENV_ROOT to your environment variables with the following commands:

1[System.Environment]::SetEnvironmentVariable('PYENV',$env:USERPROFILE + "\.pyenv\pyenv-win\","User") 2 3[System.Environment]::SetEnvironmentVariable('PYENV_ROOT',$env:USERPROFILE + "\.pyenv\pyenv-win\","User") 4 5[System.Environment]::SetEnvironmentVariable('PYENV_HOME',$env:USERPROFILE + "\.pyenv\pyenv-win\","User")

Step 4: Add the following paths to your user's PATH variable to execute the pyenv command in the terminal:

1[System.Environment]::SetEnvironmentVariable('path', $env:USERPROFILE + "\.pyenv\pyenv-win\bin;" + $env:USERPROFILE + "\.pyenv\pyenv-win\shims;" + [System.Environment]::GetEnvironmentVariable('path', "User"),"User")

Step 5: Verify the installation. First, close the current terminal and open a new one, then run the following command to verify that pyenv is installed correctly:

1pyenv --version

You should see something like this:

1pyenv 3.1.1

If you want to learn more about pyenv on Windows, you can visit its official page.

Installing pyenv on Mac

Step 1: Install Xcode by opening the terminal and running the following command:

1xcode-select --install

If it's already installed, you will see a message like this:

1xcode-select: error: command line tools are already installed, use "Software Update" to install updates

Step 2: Install Homebrew. First, check if it's installed by running the following command:

1brew --version

If it's not installed, you will see a message like this:

1-bash: brew: command not found

To install it, run the following command:

1/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 3: To avoid errors during the installation of Python versions, install some libraries with the following command:

1brew install openssl readline sqlite3 xz zlib

Step 4: Install pyenv with the following command:

1brew install pyenv

Step 5: Verify the installation. First, close the current terminal and open a new one, then run the following command to verify that pyenv is installed correctly:

1pyenv --version

You should see something like this in your terminal:

1pyenv 2.3.23

If you want to learn more about pyenv on Mac, you can visit its official page.

Installing pyenv on Linux

Step 1: Before installing pyenv, install several necessary libraries and packages with the following command:

1sudo apt install -y make build-essential libssl-dev zlib1g-dev \ 2libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev \ 3libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl \ 4git

Step 2: Clone the pyenv repository from GitHub by running the following command in the terminal:

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

Step 3: Configure the environment variables according to the shell you are using (e.g., ~/.bashrc, ~/.zshrc):

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

Step 4: Verify the installation. First, close the current terminal and open a new one, then run the following command to confirm that pyenv is installed correctly:

1pyenv --version

You should see something like this in your terminal:

1pyenv 3.1.1

If you want to learn more about pyenv on Linux, you can visit its official page.

Tools like pyenv are very useful in the development world because they allow us to manage the Python versions installed on our computer. This enables us to work on different projects without worrying about the Python versions they require, thanks to the ease of installing and switching between versions. Incorporating this tool into your workflow will make you more productive, avoid compatibility errors, and make your development smoother. We invite you to read the 4Geeks Blog for more interesting content.