System Python exists to keep the operating system alive. Your projects have no business touching it.
pyenv solves this by isolating Python versions cleanly, predictably, and without hacks.
Most breakage comes from three bad habits:
sudo pippyenv avoids all three by installing Python versions in user space and letting you choose which Python runs where.
Install build dependencies first:
sudo apt update
sudo apt install -y \
build-essential \
curl \
git \
libssl-dev \
zlib1g-dev \
libbz2-dev \
libreadline-dev \
libsqlite3-dev \
libffi-dev \
liblzma-dev \
tk-dev
Install pyenv:
curl https://pyenv.run | bash
Configure shell initialization (~/.bashrc or ~/.zshrc):
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Reload shell:
exec "$SHELL"
Verify:
pyenv --version
brew install pyenv
Add to ~/.zshrc:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
List available versions:
pyenv install --list
Install a specific version:
pyenv install 3.12.2
Set globally (default Python):
pyenv global 3.12.2
Or per project directory:
pyenv local 3.12.2
git clone https://github.com/pyenv/pyenv-virtualenv.git \
~/.pyenv/plugins/pyenv-virtualenv
Enable it in your shell:
eval "$(pyenv virtualenv-init -)"
Create an environment tied to a specific Python version:
pyenv virtualenv 3.12.2 myenv
Activate it:
pyenv activate myenv
Bind the environment to the project directory:
pyenv local myenv
From now on, entering this directory automatically activates the environment.
Upgrade pip first:
pip install --upgrade pip
Install packages:
pip install requests flask
Verify installation:
pip list
pip freeze > requirements.txt
Rebuild environment elsewhere:
pip install -r requirements.txt
pyenv deactivate
Related Articles
How to setup python venv
Production-grade workflow for Python version management with pyenv and virtual environments on Linux and macOS.
Heroku building a simple python application
Heroku is a platform as a service where we can build and deploy our applications, with PaaS there is no need to worry about the dependencies hassles to setup environments for developers.
How We Set Up Our KVM Hypervisor: From Bare Metal to Production-Ready VM Host
Detailed walkthrough of building a dedicated KVM/libvirt hypervisor with XFS tuning, hugepages, 10GbE tuning, and automation.