Skip to content

Python version management with pyenv

Posted on:January 13, 2024 at 06:49 PM

tp.web.random_picture

Manage Multiple Python versions using pyenv and virtual environments

Table of contents

Open Table of contents

TL;DR

$ sudo apt-get 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
$ sudo apt-get install python3-openssl
$ curl https://pyenv.run | bash
$ pyenv install --list # list the python versions
$ pyenv install -v 3.8.0 # install python version

Introduction

While working on different Python projects we might need to work with different versions of Python. along with that we also might need to experiment with new features in the latest released versions.

To easily manage different versions of Python we need a Python version manager. pyenv is a version manager for Python.

Issue with the system python?

If you are on a Mac or a Linux system, they already have Python installed on the operating system. It is known as “System Python”. System Python is installed in the root directory. you can find out the current version using:

$ which python3
# /usr/bin/python3

When we install a package into the system python, it is installed as a global package and we need to use the sudo pip3 install command. As all the packages are installed globally with this approach, we tend to get an issue when we want to install multiple versions of a library. Along with that, we do not have any control over the Python version that comes bundled with the system.

Even if we install Python directly on the system, it will install it in the user space. We will not need sudo for the installation of the package. However, the issue with multiple versions of the same package will still be there.

Switching Python versions is also an issue if we want to install multiple versions of Python.

Use package(version) manager

To overcome the issues with system/global python we can use package manager. The package manager will allow us to manage the following issues.

pyenv is one of the Python version managers. We will go through the details of how to install and manage pyenv

Installation

Before we install pyenv itself we need to install system-specific dependencies. most of these dependencies are written in C and they are required to install Python from source.

Build Dependencies

pyenv builds Python from the source, which means we need to install utility dependencies to use pyenv.

Ubuntu

$ sudo apt-get 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

We also need to install python3-openssl on Ubuntu

$ sudo apt-get install python3-openssl

Fedora/CentOS/RHEL

$ sudo yum install gcc zlib-devel bzip2 bzip2-devel readline-devel \ sqlite sqlite-devel openssl-devel xz xz-devel libffi-devel

MacOS

$ brew install openssl readline sqlite3 xz zlib

When running Mojave or higher you will also need to install the additional SDK headers:

$ sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Installation using pyenv-installer

After successfully installing build dependencies we will install pyenv using the installation script.

$ curl https://pyenv.run | bash

This command will install pyenv along with a few plugins.

Once this is done we need to update the bash profile. add the following lines at the end of the bash profile. the file name will be dependent on the shell that you are using. common file names based on files are bashrc, zshrc, bash_profile

# ~/.bashrc
Load pyenv automatically by adding
the following to ~/.bashrc:

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

Once done we need to either restart the terminal or source the shell file.

$ source ~/.bashrc
# or
$ exec "$SHELL"

Useful pyenv commands

List Python versions

$ pyenv install --list

Install python

$ pyenv install -v 3.10.0

Currently installed Python versions

$ pyenv versions

Set global python version

$ pyenv global 3.10.0

Set local python version for project development

$ pyenv local 3.10.0

Uninstall python

$ pyenv uninstall 3.10.0

pyenv Commands

$ pyenv commands