Jan 26, 2025

MUHAMMAD GHIFARY

I’ve lost count of how many times I’ve set up Python environments on macOS, yet I still sometimes struggle to remember all the steps to recreate the suitable setup, especially when starting fresh on a new machine. So, I decided to jot down the process—both for myself and for anyone else looking to configure their Python environment, particularly for AI and machine learning projects. Let’s dive in!

  1. Installing Homebrew

Homebrew is a fantastic package manager that simplifies software program installation via the command line on macOS (and Linux, too). Think of it as your app store, but for command-line tools. I follow the guideline from https://mac.install.guide/homebrew/3.

To get started, open Terminal and run:

$ /bin/bash -c "$(curl -fsSL <https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh>)"

Once installed, we integrate Homebrew into $PATH by executing these two commands:

$ (echo; echo 'eval "$(/opt/homebrew/bin/brew shellenv)"') >> ~/.zprofile
 
$ eval "$(/opt/homebrew/bin/brew shellenv)"

To ensure everything is set up correctly, check Homebrew’s status:

$ brew doctor

Now we can see

Your system is ready to brew.
  1. Installing Python with Pyenv

While Homebrew allows for direct Python installation (can easily done through brew install python), managing multiple Python versions across various projects can be cumbersome. Homebrew-installed Python is not well-suited to perform such tasks.

I prefer using another tool like Pyenv. It’s a tool designed specifically for managing multiple Python versions, which is a lifesaver when juggling projects that require different versions of Python.

Here’s how I set it up.

First, install Pyenv with Homebrew: