J2897

Installing Multiple Python Versions on Debian

Sep 8th, 2024 (edited)
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

While Windows uses the Python Launcher (py -0) to switch between versions, on GNU/Linux, tools like pyenv offer similar flexibility, although you could even run different versions of Python in virtual environments. Here's how you can manage multiple Python versions on Debian-based servers:

Method 1: Using pyenv (Recommended)

pyenv is a tool specifically designed to manage multiple Python versions on Unix-based systems like GNU/Linux. It allows you to easily switch between different versions without breaking nor affecting the system's default Python installation.

Steps to install and use pyenv:

  1. Install dependencies for pyenv:

    sudo apt update
    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 python3-openssl git
  2. Install pyenv:

    curl https://pyenv.run | bash
  3. Update your shell configuration to include pyenv in your PATH. Add the following lines to ~/.bashrc or ~/.bash_profile:

    export PATH="$HOME/.pyenv/bin:$PATH"
    eval "$(pyenv init --path)"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
  4. Restart your shell:

    exec $SHELL
  5. Install different versions of Python using pyenv:

    pyenv install 3.9.19
    pyenv install 3.12.5
  6. Set the global or local Python version:

    • To set the global default version:
      pyenv global 3.12.5
    • To set a local version (specific to a directory/project):
      pyenv local 3.9.19  # Generates a '.python-version' file
  7. Run a specific version of Python:

    pyenv shell 3.9.19  # Temporarily use Python 3.9.19
  8. Run a script with Python 3.9.19 without changing the global or local version:

    pyenv shell 3.9.19 && python your_script.py
  9. Show all available versions. Similar to py -0 on Windows:

    pyenv versions

Method 2: Manual Installation and Managing with Virtual Environments

Alternatively, you can manually install different Python versions from source and use virtual environments to manage them.

Steps:

  1. Download and compile the desired Python version:

    wget https://www.python.org/ftp/python/3.9.19/Python-3.9.19.tgz
    tar -xzf Python-3.9.19.tgz
    cd Python-3.9.19
    ./configure --enable-optimizations
    make -j $(nproc)
    sudo make altinstall
  2. Create a virtual environment with the specific version:

    python3.9 -m venv ~/myenv39
    source ~/myenv39/bin/activate
  3. To switch between Python versions, activate the relevant virtual environment.

Conclusion

Using pyenv is the most convenient method as it simplifies managing multiple Python versions, switching between them, and even creating virtual environments tailored to specific versions. Method 2 is really just included for contrast of thought.

Add Comment
Please, Sign In to add comment