How to Set GCC as Default Compiler (Homebrew on macOS)

1. Check Current Compiler

gcc --version

This will give output like:

Apple clang version 16.0.0 (clang-1600.0.26.6)
Target: arm64-apple-darwin24.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

2. Install GCC Using Homebrew

  • Run:
brew install gcc
  • It will be installed somewhere like /opt/homebrew/Cellar/gcc/15.1.0/.
  • Go to the bin directory and check the GCC version:
gcc-15 --version

This should point to the Homebrew version.


3. Set Homebrew GCC as Default

  • Go to the Homebrew bin directory:
cd /opt/homebrew/bin
  • Create symbolic links:
ln -s gcc-15 gcc
ln -s g++-15 g++

4. Verify Default GCC/G++

  • Check where the default gcc or g++ is pointing to:
where gcc

Example output:

/opt/homebrew/bin/gcc
/usr/bin/gcc