📒 

If you’re working with multiple Python projects, each requiring a different Python version, Pyenv makes it easy to install and switch between them. In this guide, we’ll walk through the process of installing and setting up Pyenv on Ubuntu 18.04.

Step 1: Update and Upgrade the System

To start, update the package list and upgrade existing packages to make sure everything is up to date.

sudo apt update && sudo apt upgrade -y

Step 2: Install Dependencies

Pyenv requires several dependencies to compile and manage different Python versions. Install these by running:

sudo apt 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 \
python-openssl git

Step 3: Install Pyenv

You can now download and install Pyenv. The simplest way to install it is through the Pyenv installer script.

  1. Download and run the installer script:
    curl https://pyenv.run | bash

    This will install pyenv, pyenv-virtualenv, and pyenv-update, giving you tools for managing Python versions, creating virtual environments, and updating Pyenv.

  2. Add Pyenv to your shell’s configuration:

    After installation, you need to add Pyenv to your shell so it loads each time you start a terminal. Open the shell configuration file (for example, ~/.bashrc for Bash users):

    nano ~/.bashrc

    Add the following lines to the end of the file:

    export PATH="$HOME/.pyenv/bin:$PATH"
    eval "$(pyenv init --path)"
    eval "$(pyenv init -)"
  3. Apply the changes by restarting your terminal or running:
    source ~/.bashrc

Step 4: Verify Pyenv Installation

To make sure Pyenv is correctly installed, use the following command:

pyenv --version

You should see the version number if the installation was successful.

Step 5: Installing a Python Version

With Pyenv installed, you can now install a specific version of Python. For example, to install Python 3.8.12:

pyenv install 3.8.12

You can list available versions by running:

pyenv install --list

This will show all Python versions that Pyenv can install, including stable releases and development versions.

Step 6: Setting the Default Python Version

Once you have installed your desired Python version, you can set it as the global default:

pyenv global 3.8.12

This command sets Python 3.8.12 as the default version, which will be used whenever you open a terminal. To confirm the version:

python --version

You can also set the Python version on a per-project basis. Navigate to the project directory and use:

pyenv local 3.8.12

This creates a .python-version file in the project directory specifying the Python version, so Pyenv automatically switches to it when you’re in that directory.

Step 7: Managing Virtual Environments with Pyenv

If you installed the Pyenv installer script in Step 3, you already have pyenv-virtualenv, which allows you to create isolated environments.

  1. Create a virtual environment by running:
    pyenv virtualenv 3.8.12 myenv

    Replace myenv with a name for your environment.

  2. Activate the virtual environment:
    pyenv activate myenv
  3. Deactivate it by simply running:
    pyenv deactivate

To delete an environment, use:

pyenv uninstall myenv

Summary

You’ve successfully installed Pyenv on Ubuntu 18.04 and configured it to manage multiple Python versions. You can now seamlessly switch between versions and create isolated virtual environments for each project.