/ Home
Pyenv
1. Prerequisites
Ensure Homebrew is installed:
brew --version
If not installed:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
2. Install pyenv via Homebrew
brew update
brew install pyenv
Verify installation:
pyenv --version
3. Configure Shell Environment
You must initialize pyenv in your shell startup file.
For zsh (default on modern macOS)
Edit ~/.zshrc:
nano ~/.zshrc
Add at the end:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
Apply changes:
source ~/.zshrc
For bash
Edit ~/.bashrc or ~/.bash_profile:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
4. Install Python Build Dependencies (Important)
macOS requires additional libraries to compile Python properly:
brew install openssl readline sqlite3 xz zlib tcl-tk
For Apple Silicon, also run:
export LDFLAGS="-L$(brew --prefix openssl)/lib"
export CPPFLAGS="-I$(brew --prefix openssl)/include"
(You may add these exports to your shell config if needed.)
5. Install a Python Version
List available versions:
pyenv install --list
Install a specific version (example):
pyenv install 3.11.9
Set it globally:
pyenv global 3.11.9
Or per project:
cd your-project
pyenv local 3.11.9
Verify:
python --version
which python
6. Optional: pyenv-virtualenv (Highly Recommended)
brew install pyenv-virtualenv
Add to ~/.zshrc:
eval "$(pyenv virtualenv-init -)"
Create a virtualenv:
pyenv virtualenv 3.11.9 myenv
pyenv activate myenv
7. Common Troubleshooting
pyenv not found after install
brew --prefix pyenv
echo $PATH
Ensure ~/.pyenv/bin is before system Python in PATH.
Python build fails
xcode-select --install
Conflicts with Conda
If you use Conda, do not auto-activate base:
conda config --set auto_activate_base false
Restart terminal.
Deactivate
source deactivate
Summary (Quick Install)
brew install pyenv
brew install openssl readline sqlite3 xz zlib tcl-tk
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
echo 'eval "$(pyenv init --path)"' >> ~/.zshrc
echo 'eval "$(pyenv init -)"' >> ~/.zshrc
source ~/.zshrc
pyenv install 3.11.9
pyenv global 3.11.9