John Siu Blog

Tech - Business Tool, Personal Toys

MacOS, Anaconda, and Homebrew

☰ Table of Content

How to make Anaconda and Homebrew play nicely together.

Anaconda is a popular python development environment across platforms. However, on MacOS, though Homebrew by default install python as python3, it may still create issue sometimes. This blog will look at the official and unofficial way to minimize potential conflict between the two.

Install Anaconda

Anaconda can be installed by downloading the package directly from official site, or via Homebrew. They do result with a different installation path.

Official Download

Download link is on Anaconda homepage: https://www.anaconda.com/

Anaconda directory: ~/anaconda3/

Homebrew

To use homebrew

1
brew install anaconda

Anaconda directory: /opt/homebrew/anaconda3/

conda init

After installing Anaconda, you need to run following once, depends on how it is installed.

Direct install:

1
~/anaconda3/bin/conda init zsh

Via Homebrew:

1
/opt/homebrew/anaconda3/bin/conda init zsh

The command will add following to the end of ~/.zshrc:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/homebrew/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/homebrew/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/homebrew/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/homebrew/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<

The above is created by a homebrew installation. Direct install will see /opt/homebrew/ changed to your home directory.

Activate Anaconda On Demand

There are situation we want to minimize Anaconda conflict with Homebrew or other non-anaconda base python projects. There are two ways to do it.

Official Way

Prevent anaconda to automatically activate the base project when a shell start, while command conda still works.

1
conda config --set auto_activate_base false

Unofficial Way

However there are are cases we want to remove any doubt and don’t want anaconda script to run at all.

In ~/.zshrc, wrap anaconda initialization code in a zsh function condaOn() like following:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
condaOn() {
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/opt/homebrew/anaconda3/bin/conda' 'shell.zsh' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/opt/homebrew/anaconda3/etc/profile.d/conda.sh" ]; then
        . "/opt/homebrew/anaconda3/etc/profile.d/conda.sh"
    else
        export PATH="/opt/homebrew/anaconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<
}

DO NOT add any indent to the conda initialization code. Just add condaOn() { before it, and } after it like above.

When you need to use Anaconda, just type condaOn:

1
2
$ condaOn
(base) $

Then start using conda commands.

John Siu

Update: 2023-07-25
comments powered by Disqus