Skip to content

Instantly share code, notes, and snippets.

@scivision
Last active August 14, 2026 19:41
Show Gist options
  • Select an option

  • Save scivision/7b580b82b2e37a4a885757ddfdf89e04 to your computer and use it in GitHub Desktop.

Select an option

Save scivision/7b580b82b2e37a4a885757ddfdf89e04 to your computer and use it in GitHub Desktop.
with Homebrew pin a specific GCC version to install and build if needed

Homebrew GCC pin version script

The script homebrew_gcc_pin.sh is used to pin a specific version of GCC installed via Homebrew. It is typical that GCC will be built from source. Building GCC might take about 30 minutes depending on the computer speed.

./homebrew_gcc_pin.sh <GCC version>

for example:

./homebrew_gcc_pin.sh 16.1.0

As usual, monitor what's happening in the GCC build with the top or htop command.

Alternative

CMake superbuild of GCC from source and GCC prerequisites.

#!/usr/bin/env bash
#
# Pin a specific version of GCC installed via Homebrew.
# It is typical that GCC will be built from source.
#
# ./homebrew_gcc_pin.sh <version>
set -Eeuo pipefail
if (( $# != 1 )); then
echo "Usage: $0 <version>" >&2
exit 2
fi
gcc_version=$1
tap_name="${USER}/local"
gcc_release=${gcc_version%%.*}
without_git_signing() {
env \
GIT_CONFIG_COUNT=1 \
GIT_CONFIG_KEY_0=commit.gpgsign \
GIT_CONFIG_VALUE_0=false \
"$@"
}
if ! brew tap | grep -Fqx -- "$tap_name"; then
without_git_signing brew tap-new "$tap_name"
fi
tap_dir=$(brew --repository "$tap_name")
if ! git -C "$tap_dir" rev-parse --verify HEAD >/dev/null 2>&1; then
without_git_signing git -C "$tap_dir" commit -m "Create $tap_name tap"
fi
if brew tap | grep -Fqx -- "homebrew/core"; then
echo "homebrew/core tap is already installed."
else
echo "homebrew/core tap needed, tapping it now..."
brew tap --force homebrew/core
fi
# Extract the GCC formula definition from homebrew-core history
extract_status=0
if ! brew extract --version="${gcc_version}" gcc "$tap_name"; then
extract_status=$?
fi
if (( extract_status != 0 )); then
exit "$extract_status"
fi
# Homebrew requires custom-tap formulas to be trusted before installation.
brew trust --formula "${tap_name}/gcc@${gcc_version}"
# Avoid auto-linking into the shared Homebrew prefix so multiple GCC versions can coexist.
# Use the versioned binary path directly when you need a specific compiler.
HOMEBREW_NO_AUTO_LINK=1 brew install "gcc@${gcc_version}"
versioned_prefix="/opt/homebrew/opt/gcc@${gcc_version}"
versioned_bin="${versioned_prefix}/bin"
gcc_script="$HOME/gcc-${gcc_version}.sh"
if [[ -d "$versioned_bin" ]]; then
echo
echo "Installed GCC ${gcc_version} is available at:"
echo " ${versioned_bin}"
if [[ -f "$gcc_script" ]]; then
echo
echo "To use this compiler, make a script like $gcc_script:"
echo
echo " export CC=${versioned_bin}/gcc-${gcc_release}"
echo " export CXX=${versioned_bin}/g++-${gcc_release}"
echo " export FC=${versioned_bin}/gfortran-${gcc_release}"
else
echo "export CC=${versioned_bin}/gcc-${gcc_release}" >> "$gcc_script"
echo "export CXX=${versioned_bin}/g++-${gcc_release}" >> "$gcc_script"
echo "export FC=${versioned_bin}/gfortran-${gcc_release}" >> "$gcc_script"
echo "To use this compiler, source the script:"
echo
echo " source $gcc_script"
echo
echo "which contains: "
cat "$gcc_script"
fi
fi
echo "if on using this GCC ${gcc_version} there are errors with stdlib headers, try using SDKROOT environment variable."
echo "For an older GCC like 15.x you might need say:"
echo "export SDKROOT=/Library/Developer/CommandLineTools/SDKs/MacOSX15.sdk/"
echo "check what directories like that exist and try."
echo "reference:"
echo "https://gist.github.com/scivision/d69faebbc56da9714798087b56de925a"
Copyright 2026 SciVision
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment