Published on

Installing and Using pyenv on Ubuntu/Linux

Authors
  • avatar
    Name
    Winston Brown
    Twitter

Installing and Using pyenv on Ubuntu/Linux

For Python developers working on Ubuntu or other Linux distributions, managing different Python versions for various projects can be a challenge. pyenv is a powerful tool that simplifies this process, allowing you to switch between multiple Python versions seamlessly. This guide will walk you through installing and using pyenv on your Linux machine.

Why use pyenv?

pyenv lets you install multiple Python versions side-by-side and choose which version to use for a specific project. This helps in avoiding dependency conflicts and ensures that your projects run on the correct Python version.

Installation

The recommended way to install pyenv is by using the pyenv-installer script.

  1. Run the installer:

    curl https://pyenv.run | bash
    
  2. Configure your shell:

    You need to add pyenv to your shell's configuration file. The installer script usually does this for you, but if not, you can add the following lines to your .bashrc (for Bash) or .zshrc (for Zsh).

    • For Bash:

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
      echo 'eval "$(pyenv init -)"' >> ~/.bashrc
      
    • For Zsh:

      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
      echo 'eval "$(pyenv init -)"' >> ~/.zshrc
      
  3. Restart your shell:

    For the changes to take effect, restart your terminal or run:

    exec "$SHELL"
    
  4. Install Python build dependencies:

    Before you can install different Python versions, you need to install the necessary build dependencies. For Ubuntu/Debian, you can do this with:

    sudo apt-get update; sudo apt-get install -y 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 python3-openssl git
    

Basic Usage

Here are some of the most common pyenv commands:

  • List available Python versions to install:

    pyenv install --list
    
  • Install a specific Python version:

    pyenv install 3.10.4
    
  • List all installed Python versions:

    pyenv versions
    
  • Set the global Python version:

    pyenv global 3.10.4
    
  • Set a local (project-specific) Python version:

    pyenv local 3.9.12
    

Managing Virtual Environments with pyenv-virtualenv

pyenv-virtualenv is a plugin that helps you manage virtual environments for your projects.

  1. Installation:

    The pyenv-installer script usually installs pyenv-virtualenv for you. If not, you can install it from GitHub:

    git clone https://github.com/pyenv/pyenv-virtualenv.git $(pyenv root)/plugins/pyenv-virtualenv
    
  2. Configure your shell for pyenv-virtualenv:

    Add the following line to your shell configuration file (.bashrc or .zshrc):

    echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bashrc
    

    Remember to restart your shell after adding this line.

  3. Create a virtual environment:

    pyenv virtualenv <python_version> <environment_name>
    
  4. Activate and deactivate your environment:

    pyenv activate <environment_name>
    pyenv deactivate
    

With pyenv and pyenv-virtualenv, you can now manage your Python versions and virtual environments with ease on your Linux machine. Happy coding!