How to install different Python versions: pyenv

How to install different Python versions: pyenv

Use pyenv to install separate Python interpreters under a regular user account, then use a virtual environment for each project’s dependencies. Keep the distribution’s system Python intact. Choosing an interpreter, isolating packages and deciding which executable a service runs are three separate steps.

This guide targets Linux with Bash. Choose a currently supported Python release compatible with the application. Python 2.7 and older 3.x releases may appear in pyenv’s catalog even after upstream support ends; availability to install is not a security-support guarantee. Consult the Python version status table before pinning a version.

Choose pyenv, system packages or containers

pyenv is useful for development, compatibility testing and applications requiring different interpreters on one account. Most CPython installations are built from source, so build dependencies and security updates remain your responsibility. Distribution packages can simplify vendor-supported maintenance; containers can package a tested interpreter with an application. None of these choices removes the need to update dependencies.

For a container example, see the Docker Compose monitoring stack. pyenv selects commands through PATH shims; it does not provide process, network or filesystem isolation.

Install prerequisites and pyenv

Follow the current build prerequisites for your distribution and target Python release. A compiler, make, OpenSSL development files, zlib, libffi, readline, SQLite, bzip2 and xz are commonly needed; newer releases can require additional libraries. Package names differ, so do not reuse an old CentOS command blindly on modern Fedora or Ubuntu.

A manual checkout avoids executing an installer directly from a network stream. Run once as the intended application/development user, not root, with Git installed and no existing directory at this path:

git clone https://github.com/pyenv/pyenv.git "$HOME/.pyenv"

Review and pin an approved pyenv release for reproducible deployments. Follow the official shell setup for your shell. For Bash, add this once to the relevant startup files, using the documented login/non-login arrangement for your system:

export PYENV_ROOT="$HOME/.pyenv"
[[ -d "$PYENV_ROOT/bin" ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"

For Zsh, use the documented Zsh initialization in ~/.zshrc; do not source Bash configuration into Zsh. Open a new shell and check:

pyenv --version
command -v pyenv
pyenv install --list

Install and pin a supported interpreter

Choose an exact supported patch release from the catalog and set PYTHON_VERSION to it. The prompt below asks for that value instead of embedding an obsolete security patch in the tutorial:

read -r -p 'Exact approved Python patch version: ' PYTHON_VERSION
: "${PYTHON_VERSION:?Choose an exact supported version}"
pyenv install -s "$PYTHON_VERSION"
mkdir -p "$HOME/projects/python-demo"
cd "$HOME/projects/python-demo"
pyenv local "$PYTHON_VERSION"
pyenv version
pyenv which python
python --version

pyenv local writes .python-version for that directory and its descendants. Commit that file when it is the project’s agreed interpreter pin. Other contributors still need to install the version; the file does not download it automatically. pyenv global sets the account’s pyenv default, not the whole operating system’s interpreter. A shell override can take precedence, so inspect pyenv version when selection surprises you.

Isolate packages with venv

After selecting the interpreter, create a fresh virtual environment. This example expects a reviewed requirements file in the project:

python -m venv .venv
. .venv/bin/activate
python -c 'import sys; print(sys.version); print(sys.executable); print(sys.prefix)'
python -m pip install -r requirements.txt
python -m pip check
deactivate

Use python -m pip to bind package installation to that interpreter. Keep .venv out of version control. A venv normally excludes system site-packages but does not isolate the application from the host. Recreate it after changing the base interpreter; changing .python-version does not rebuild an existing venv.

The optional pyenv-virtualenv plugin has its own installation and shell-initialization requirements. It is not needed for the standard-library venv workflow above.

Run the intended interpreter in services and CI

Interactive shell initialization is not guaranteed in systemd, cron or a CI runner. For a service, use the explicit venv executable and working directory. This is a service fragment, not a complete unit or an instruction to replace an existing service:

[Service]
User=appuser
WorkingDirectory=/srv/myapp
ExecStart=/srv/myapp/.venv/bin/python /srv/myapp/app.py

The account, files and interpreter must exist with appropriate permissions. Restart a service only through its normal deployment procedure, then verify its running executable and application health. A project version file describes intent, not proof of the interpreter used by a running process.

In GitHub Actions, use the maintained setup-python action and a supported version matrix or python-version-file. Pin actions according to your repository policy and check runner compatibility. setup-python does not require a separate pyenv cache. If CI instead runs pyenv, initialize it in every shell that needs it, use a fresh venv per job, and avoid sharing writable interpreter directories across untrusted jobs.

Upgrade without breaking system Python

  1. Update the pyenv checkout to an approved release using the method used to install it. pyenv update requires the separate update plugin and is not universally present.
  2. Install the new Python patch alongside the old interpreter.
  3. Create a new venv and install the project’s reviewed dependencies. Run tests, imports, TLS checks and a representative workload.
  4. Switch the service’s explicit interpreter path through a controlled deployment and verify behavior.
  5. Keep a rollback path until the new version is accepted. Remove an old interpreter only after confirming no service or venv still depends on it.

Do not replace /usr/bin/python3 or remove the distribution interpreter to force selection. Do not copy compiled interpreters between arbitrary hosts: architecture, libc, OpenSSL and other shared-library compatibility matter. Build or package against the target platform.

Diagnose build and selection failures

pyenv versions
pyenv version
pyenv which python
command -v python
python -c 'import sys, ssl, sqlite3, bz2, lzma; print(sys.executable); print(ssl.OPENSSL_VERSION)'

If a build fails, read the verbose build log and verify the prerequisites for that exact release. Missing SSL or compression modules can leave an interpreter that starts but fails real application tasks. Compare PATH, shell overrides and any active venv before reinstalling. pyenv rehash refreshes shims; it does not repair missing compiled modules.

PGO builds with PYTHON_CONFIGURE_OPTS="--enable-optimizations" can take longer and use more build resources. Benchmark the actual workload rather than promising a universal 10–15% gain. Disk needs also depend on build artifacts, versions and installed packages.

After an upgrade, use Linux server monitoring to compare process resource use and service availability with the previous release. Fivenines host metrics can supply ongoing context, while interpreter identity, dependency compatibility and application tests remain part of deployment verification.