Skip to content

Pyenv Installation

The Duskfall Portal Crew edited this page May 7, 2025 · 1 revision

pyenv (Python Version Manager)

pyenv is a powerful tool for managing multiple Python versions on your system. It allows you to install, switch between, and set global or project-specific Python versions without interfering with the system Python installation.

Table of Contents

  1. Why Use pyenv?
  2. Installing pyenv
  3. Using pyenv
  4. Using pyenv with Virtual Environments
  5. Troubleshooting
  6. Further Information

1. Why Use pyenv?

  • Multiple Python Versions: Allows you to easily install and use multiple Python versions (e.g., Python 3.8, 3.10, 3.11) on the same system.
  • Project Isolation: You can specify a specific Python version for each project, ensuring compatibility and preventing conflicts between projects that require different Python versions.
  • No System Python Interference: pyenv leaves your system's Python installation untouched, preventing accidental modifications or breakage.
  • Easy Switching: Switching between Python versions is straightforward.

2. Installing pyenv

The installation process for pyenv varies slightly depending on your operating system and shell. Here's a general overview and some common examples:

  1. Prerequisites:

    • Ensure you have the necessary build tools installed (e.g., compilers, headers). These are often required to build Python from source. The install instructions will vary based on your OS.
  2. Install pyenv:

    • Using a Package Manager (Recommended for some Linux distributions):

      # Example for Debian/Ubuntu (check pyenv's documentation for exact details)
      sudo apt update
      sudo apt install pyenv
    • Using git (General method): This is a more general method that works on most systems, and is used when a package manager is not available.

      git clone https://github.com/pyenv/pyenv.git ~/.pyenv
      # Add pyenv to your shell (see instructions below for different shells).
  3. Configure Your Shell: After installing pyenv, you need to configure your shell to initialize it. This usually involves adding some lines to your shell's configuration file (e.g., .bashrc, .zshrc, .config/fish/config.fish).

    • Bash/Zsh:

      Add the following lines to your .bashrc or .zshrc file (replace <your_username> as needed):

      export PYENV_ROOT="$HOME/.pyenv"
      [[ -f "$PYENV_ROOT/bin/pyenv" ]] && eval "$(pyenv init -)"

      Then, source your shell configuration file to apply the changes:

      source ~/.bashrc  # or source ~/.zshrc
    • FISH:

      Add these lines to your ~/.config/fish/config.fish file:

      set -x PYENV_ROOT "$HOME/.pyenv"
      if test -d "$PYENV_ROOT"
        set -x PATH "$PYENV_ROOT/bin" $PATH
        eval (pyenv init -|psub)
      end

      Then, restart your FISH terminal or run source ~/.config/fish/config.fish.

  4. Verify Installation:

    pyenv --version

    This command should display the pyenv version if the installation was successful.

3. Using pyenv

  1. List Available Python Versions:

    pyenv install --list

    This command lists the Python versions that are available for installation.

  2. Install a Specific Python Version:

    pyenv install 3.11.x  # Replace 3.11.x with the version you want

    This will download, compile, and install the specified Python version. This can take a while.

  3. List Installed Python Versions:

    pyenv versions

    This lists the Python versions you have installed.

  4. Set a Global Python Version:

    pyenv global 3.10.x  # Set a global version.  This will be the default.

    This sets the global Python version used by default for all projects.

  5. Set a Local Python Version for a Project:

    cd <your_project_directory>
    pyenv local 3.10.x  # Sets a version for this project only.

    This creates a .python-version file in your project directory, specifying the Python version to use for this project. pyenv will automatically use that version when you're in that directory.

  6. Check Which Python Version is Active:

    python --version
    pyenv version

4. Using pyenv with Virtual Environments

pyenv works seamlessly with virtual environments.

  1. Create a Virtual Environment:

    python3 -m venv .venv  #  This creates a new one from the python version set by pyenv

    This will use the currently active Python version (determined by pyenv) to create the virtual environment.

  2. Activate the Virtual Environment: (Standard activation method for your system - see main documentation for details)

  3. Use pip to install packages within the activated virtual environment: (as always)

    pip install -r requirements.txt

5. Troubleshooting

  • Shell Configuration: Ensure your shell is configured correctly with pyenv init. Double-check your shell configuration file for any typos.
  • Rehashing: After installing a new Python version, run pyenv rehash to update the shims.
  • PATH Issues: If you're still encountering issues, verify that pyenv is correctly adding the necessary directories to your PATH environment variable.
  • Build Dependencies: You might need to install build tools (e.g., compilers, headers) before installing Python versions. The specific packages needed depend on your Linux distribution (see troubleshooting in the Advanced Installation Guide).
  • Proxy Settings: pyenv may use proxy settings.

6. Further Information

Clone this wiki locally