Post

How to Install pyenv on macOS

A concise and clear guide to installing pyenv on your Mac to manage multiple Python versions.

How to Install pyenv on macOS

Managing multiple Python versions on macOS can be tricky—but pyenv makes it easy. This guide walks you through installing and using pyenv to streamline your Python development workflow.

Step 1: Install pyenv via Homebrew

First, install pyenv using Homebrew:

1
brew install pyenv

Make sure Homebrew is installed. If not, check out my How to Install Homebrew on macOS post for detailed instructions on how to install Homebrew.

Step 2: Configure Your Shell

Once installed, add the complete configuration to your shell file. First, check which shell you’re using:

1
echo $SHELL

Then add these lines to your shell configuration file:

  • For zsh: ~/.zshrc
  • For bash: ~/.bash_profile or ~/.bashrc
1
2
3
4
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"

Quick way to add all lines at once (for zsh):

1
2
3
4
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

Apply the changes:

1
source ~/.zshrc  # or source ~/.bash_profile depending on your shell

Step 3: Install a Python Version

You can now install any version of Python you need. For example:

1
pyenv install 3.13.3

Step 4: Set Your Python Version

Use one of the following commands to set the desired Python version:

  • Set globally:
    1
    
    pyenv global 3.13.3
    
  • Set locally for a project:
    1
    
    pyenv local 3.13.3
    
  • Set for the current shell session:
    1
    
    pyenv shell 3.13.3
    

Step 5: Refresh and Verify

After setting your Python version, refresh pyenv:

1
pyenv rehash

Then verify the installation:

1
python --version

You should see the Python version you’ve set via pyenv.

Troubleshooting: “Command not found: python”

If you get zsh: command not found: python in new shells, try these fixes:

1. Verify your configuration:

1
cat ~/.zshrc | grep pyenv

2. Check pyenv status:

1
2
3
which pyenv
pyenv versions
pyenv global

3. If python command doesn’t work, try python3:

1
python3 --version

4. Complete reconfiguration (if needed): Re-run the configuration commands from Step 2, then:

1
2
source ~/.zshrc
pyenv rehash

5. Test in a new terminal window to ensure it works across all shells.

Conclusion

With pyenv properly configured, managing Python versions on Mac becomes simple and reliable. The key is ensuring your shell loads the pyenv configuration in every new session, which the complete setup above handles automatically.


Sources:

☕ Support My Work

If you found this post helpful and want to support more content like this, you can buy me a coffee!

Your support helps me continue creating useful articles and tips for fellow developers. Thank you! 🙏

This post is licensed under CC BY 4.0 by the author.