Sunday, February 16, 2025

pyenv on mac

Homebrew in macOS
  1. Update homebrew and install pyenv:

    brew update
    brew install pyenv

    If you want to install (and update to) the latest development head of Pyenv rather than the latest release, instead run:

    brew install pyenv --head
  2. Then follow the rest of the post-installation steps, starting with Set up your shell environment for Pyenv.

  3. OPTIONAL. To fix brew doctor's warning ""config" scripts exist outside your system or Homebrew directories"

    If you're going to build Homebrew formulae from source that link against Python like Tkinter or NumPy (This is only generally the case if you are a developer of such a formula, or if you have an EOL version of MacOS for which prebuilt bottles are no longer provided and you are using such a formula).

    To avoid them accidentally linking against a Pyenv-provided Python, add the following line into your interactive shell's configuration:

    • Bash/Zsh:

      alias brew='env PATH="${PATH//$(pyenv root)\/shims:/}" brew'
    • Fish:

      alias brew="env PATH=(string replace (pyenv root)/shims '' \"\$PATH\") brew"

B. Set up your shell environment for Pyenv


The below setup should work for the vast majority of users for common use cases. See Advanced configuration for details and more configuration options.

Bash

Stock Bash startup files vary widely between distributions in which of them source which, under what circumstances, in what order and what additional configuration they perform. As such, the most reliable way to get Pyenv in all environments is to append Pyenv configuration commands to both .bashrc (for interactive shells) and the profile file that Bash would use (for login shells).

  1. First, add the commands to ~/.bashrc by running the following in your terminal:

    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
    echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
    echo 'eval "$(pyenv init - bash)"' >> ~/.bashrc
  2. Then, if you have ~/.profile~/.bash_profile or ~/.bash_login, add the commands there as well. If you have none of these, create a ~/.profile and add the commands there.

    • to add to ~/.profile:
      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.profile
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.profile
      echo 'eval "$(pyenv init - bash)"' >> ~/.profile
    • to add to ~/.bash_profile:
      echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
      echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
      echo 'eval "$(pyenv init - bash)"' >> ~/.bash_profile

Bash warning: There are some systems where the BASH_ENV variable is configured to point to .bashrc. On such systems, you should almost certainly put the eval "$(pyenv init - bash)" line into .bash_profile, and not into .bashrc. Otherwise, you may observe strange behaviour, such as pyenv getting into an infinite loop. See #264 for details.

Zsh

  echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
  echo '[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.zshrc
  echo 'eval "$(pyenv init - zsh)"' >> ~/.zshrc

If you wish to get Pyenv in noninteractive login shells as well, also add the commands to ~/.zprofile or ~/.zlogin.

Fish

  1. If you have Fish 3.2.0 or newer, execute this interactively:

      set -Ux PYENV_ROOT $HOME/.pyenv
      fish_add_path $PYENV_ROOT/bin
  2. Otherwise, execute the snippet below:

      set -Ux PYENV_ROOT $HOME/.pyenv
      set -U fish_user_paths $PYENV_ROOT/bin $fish_user_paths
  3. Now, add this to ~/.config/fish/config.fish:

      pyenv init - fish | source

C. Restart your shell


for the PATH changes to take effect.

exec "$SHELL"

1 comment:

  1. Are you really advising people to update their shell config files by running commands like
    echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.zshrc
    ???
    That strikes me as incredibly ill advised. If someone is so helpless that they don't know how to open their .zshrc file with a text editor and edit it in the appropriate way, I can't imagine that using pyenv, or almost any development tool, is going to be a smooth ride.

    ReplyDelete