brew
and conda
tend not to play nicely together, but I think I have a set up that has worked quite well for me so far. It was inspired by this post.
You can add the following code to your .zshrc
:
# Deactivates conda before running brew.
# Re-activates conda if it was active upon completion.
brew() {
local conda_env="$CONDA_DEFAULT_ENV"
while [ "$CONDA_SHLVL" -gt 0 ]; do
conda deactivate
done
command brew $@
local brew_status=$?
[ -n "${conda_env:+x}" ] && conda activate "$conda_env"
return "$brew_status"
}
You want to deactivate conda
before running brew
so that brew
doesn't find conda
packages in your PATH
whenever it tries to install something. In fact, brew doctor
will complain if you have not deactivated conda
before running brew, as mentioned in the post I link to above. (See also this question.)
One thing I should mention is that conda
environments "stack", but the brew()
function I've written above does not keep track of your stack of environments. (See below for a version of this function that keeps track of this.) For example, if you do conda activate newenv
while a conda
environment oldenv
is active, then conda deactivate
will return you to oldenv
. However, if you run brew
using the function I've written above after activating oldenv
and then newenv
, running conda deactivate
will not return you to oldenv
but will deactivate your conda environments entirely.
This function also probably creates some unnecessary overhead when running brew
, as I believe you only really need to deactivate your conda
environment when running brew install
. That said, if you're the kind of person to care about that overhead enough, this answer probably doesn't tell you anything you didn't already know.
As a final note, brew cask install anaconda
does not strike me as a good idea, since conda
was designed to be installed in $HOME
, but brew cask
will want to install it in /usr/local
, so that could lead to unpredictable behaviour.
Edit: Here's is a version of the brew
function which leaves your conda environments as it found it:
brew() {
local -a conda_envs
while [ "$CONDA_SHLVL" -gt 0 ]; do
conda_envs=("$CONDA_DEFAULT_ENV" $conda_envs)
conda deactivate
done
command brew $@
local brew_status=$?
for env in $conda_envs; do
conda activate "$env"
done
unset env
return "$brew_status"
}
I've tested this in Zsh. I don't think it will work in Bash. If you want to use it in Bash, you will need to change the for loop declaration to say something like for env in ${conda_envs[@]}
. I haven't tested this, however, so please test that it does what you need before use.