How To Install Pyenv on MacOS

Overview

Pyenv lets you easily switch between multiple versions of Python. It's simple, unobtrusive, and follows the UNIX tradition of single-purpose tools that do one thing well.

Steps

  1. Update brew
Terminal
brew update
  1. Install zlib sqlite xz installed on your machine
Terminal
brew install zlib sqlite xz
  1. Brew install Pyenv
Terminal
brew install pyenv
  1. Restart the terminal for changes to take effect or run exec "$SHELL"
Terminal
exec "$SHELL"
  1. Add exports to the current terminal window
The zlib compression algorithm and the SQLite database are dependencies for pyenv and often cause build problems when not configured correctly. Add these exports to your current terminal window to ensure the installation completes:
Terminal
export LDFLAGS="-L/usr/local/opt/zlib/lib -L/usr/local/opt/sqlite/lib"
export CPPFLAGS="-I/usr/local/opt/zlib/include -I/usr/local/opt/sqlite/include"
  1. Check out some available versions of python > 3.9.*
Terminal
pyenv install --list | grep 3.9.
  1. Install python 3.9.4

It will take some time to finish the installation.

Terminal
pyenv install 3.9.4
  1. Make 3.9.4 as global python version
Terminal
pyenv global 3.9.4
  1. Check the version
Terminal
pyenv version

You should see 3.9.4 (set by /Users/youngjaelim/.pyenv/version).

  1. Configure .zshrc
The power of pyenv comes from its control over our shell's path. In order for it to work correctly, we need to add the following to our configuration file (.zshrc for me, possibly .bash_profile for you)
```bash echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init --path)" eval "$(pyenv init -)"\nfi' >> ~/.zshrc ```
  1. Check to make sure .zshrc updated
Terminal
cat ~/.zshrc
  1. Restart the terminal for changes to take effect or run exec "$SHELL".
Terminal
exec "$SHELL"
  1. Check python version
Terminal
python -V
  1. Upgrade the pip and setuptools packages

To avoid the persistent warning about upgrading the pip package, it would be best if you upgrade the pip package.

Terminal
pip install -U pip setuptools
pip list
all posts

Comments

© 2022 Youngjae Jay Lim. All Rights Reserved, Built with Gatsby