Portable pyenv Setup for Python Vulnerability Research

This guide describes a repeatable, cross‑Linux workflow for setting up isolated Python environments with pyenv for security and CVE research. It assumes basic terminal familiarity and a non‑privileged user account.


Overview

This guide has three goals:

  • Make environments reproducible across machines and distros.

  • Keep each target’s tooling isolated from others.

  • Provide a standard pattern you can reuse for every Python security research project.

The workflow:

  1. Install system build dependencies (per Linux family).

  2. Install pyenv and pyenv‑virtualenv.

  3. Use a standard “research environment template” for each target project.


Prerequisites

  • A Linux host (VM or physical), using one of:

    • Debian / Ubuntu

    • Fedora / RHEL / CentOS

    • Arch / Manjaro

  • A regular non‑root user account with sudo access.

  • Basic familiarity with a shell (bash or zsh).

For security work, using a dedicated research VM or container is strongly recommended.


Step 1: Install OS Build Dependencies

Install the libraries required to build Python from source. Run the block that matches your distribution family.

Debian / Ubuntu

Fedora / RHEL / CentOS

For Fedora or newer RHEL/CentOS that use dnf:

For older RHEL/CentOS that use yum, replace dnf with yum in the commands above.

Arch / Manjaro

If a package name is missing on your specific distro/version, install the closest *-dev or *-devel equivalent for:

  • OpenSSL

  • zlib

  • bz2

  • readline

  • sqlite

  • ncurses

  • tk

  • libffi

  • lzma (xz)


Step 2: Install pyenv (Any Linux)

This section is distro‑agnostic.

  1. Install pyenv with the official installer:

  2. Add pyenv to your shell startup file.

    For Bash (~/.bashrc):

    For Zsh (~/.zshrc):

  3. Reload your shell:

  4. Confirm installation:

If this prints a version, pyenv is installed correctly.


Step 3: Standard Research Environment Template

Use the same pattern for each Python target you analyze. Only update a few variables per project.

Template Variables

Pick values per target:

  • TARGET_NAME should match the project or repo.

  • PY_VERSION is the Python version you want to test (you can repeat this for multiple versions).

  • ENV_NAME combines both for clarity.

  • WORK_ROOT is a single directory where all security work lives.

Environment Creation Script

Run this block in a shell after setting the variables:

At this point:

  • Entering "$WORK_ROOT/$TARGET_NAME" will automatically select "$ENV_NAME" (due to pyenv local).

  • The environment contains a consistent set of security and dev tools.

  • You can now clone and install the target project into this environment.


Step 4: Using the Environment for a Target Project

Once the environment is ready:

  1. Go to the workspace:

  2. Clone the target repository into this directory (example):

  3. Ensure the correct environment is active:

  4. Install project dependencies (examples):

(Adjust filenames according to the project.)

From here, run tests, static analysis tools, proof‑of‑concept scripts, and exploit code in this environment without affecting other projects.


Step 5: Rebuilding an Environment Later

On a new machine or VM:

  1. Repeat Step 1 and Step 2 for the new system.

  2. Recreate the environment with the same variables:

  3. Rebuild the environment:

If you keep tools-baseline.txt, project-requirements.txt, and the scripts in version control (for example, a private “security-envs” or “research-environments” repo), you can reconstruct setups with high fidelity.


Python Security tooling

Alongside pyenv and virtualenv, a base Python tooling set that you can install into each project’s env is:

  • Static analysis / SAST: bandit, pip-audit, and optionally safety for dependency checks, plus mypy for type checking.​

  • Testing and coverage: pytest, coverage, and hypothesis for property‑based testing and fuzz‑like input generation.​

  • General dev utilities: black, isort, flake8 or ruff to keep your own scripts consistent and easier to maintain across projects.​


  • Use dedicated VMs or containers per project or per group of related projects.

  • Avoid using the system Python for any research tasks.

  • Snapshot your VM after completing this setup so future projects can start from a clean baseline.

  • Prefer non‑privileged users and minimal permissions, especially when running proof‑of‑concept exploits.

Last updated