Skip to content

Instantly share code, notes, and snippets.

@josephschmitt
Last active September 23, 2021 16:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save josephschmitt/ae8cab59024b72c1985e4bcf28f9eeb5 to your computer and use it in GitHub Desktop.
Save josephschmitt/ae8cab59024b72c1985e4bcf28f9eeb5 to your computer and use it in GitHub Desktop.
Function to update the PATH to point to the "correct" brew installation depending on architecture
# This function is for use on Macs with multiple brew installations (usually M1/arm64) Macs. This
# function will attempt to set your brew PATH to point to the correct brew based on the system arch.
#
# Usage:
# # will automatically set brew based on architecture
# $ switch_brew
#
# # will set brew for arm64
# $ switch_brew arm64
#
# # will set brew for x86_64
# $ switch_brew x86_64
function switch_brew {
armBrewPath="/opt/homebrew/bin"
switch_to=${1:-"$(uname -m)"}
if [ "${switch_to}" = "arm64" ]; then
PATH="${armBrewPath}:$PATH"
else
PATH=${PATH/":$armBrewPath"/} # delete any instances in the middle or at the end
PATH=${PATH/"$armBrewPath:"/} # delete any instances at the beginning
fi
}
@josephschmitt
Copy link
Author

josephschmitt commented Sep 23, 2021

This function should be added to your ~/.bashrc or ~/.zshrc (depending on which shell you use).

See homebrew's installation instructions for more context, or this explanation.

Additionally, this blog post goes into more explanation on the multi-brew setup.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment