Homebrew now has the GCC package so you can install it with this command:

brew install gcc
Answer from CommittedEel on Stack Overflow
Top answer
1 of 4
11

Homebrew now has the GCC package so you can install it with this command:

brew install gcc
2 of 4
7

The way I do it is:

  1. Download the source for GCC and numerous supporting packages. The instructions are in the gcc-4.x.y/INSTALL/index.html file in the GCC source code, or online at http://gcc.gnu.org/install/.
  • GNU Multiple Precision Library (GMP) version 4.3.2 (or later) from http://gmplib.org/.
  • MPFR Library version 2.4.2 (or later) from http://www.mpfr.org/.
  • MPC Library version 0.8.1 (or later) from http://www.multiprecision.org/.
  • ISL Library version 0.11.1 from ftp://gcc.gnu.org/pub/gcc/infrastructure/.
  • CLooG 0.18.0 from ftp://gcc.gnu.org/pub/gcc/infrastructure/.
  1. Use a script to extract the source for GCC and the support libraries into a directory, create the object directory, and run the build.

This is the script I used for GCC 4.8.2:

GCC_VER=gcc-4.8.2
tar -xf ${GCC_VER}.tar.bz2 || exit 1

(
cd ${GCC_VER} || exit

cat <<EOF |
    cloog 0.18.0 tar.gz
    gmp 5.1.3 tar.xz
    isl 0.11.1 tar.bz2
    mpc 1.0.1 tar.gz
    mpfr 3.1.2 tar.xz
EOF

while read file vrsn extn
do
    (
    set -x
    tar -xf "../$file-$vrsn.$extn" &&
    ln -s "$file-$vrsn" "$file"
    )
done
)

mkdir ${GCC_VER}-obj
cd ${GCC_VER}-obj
../${GCC_VER}/configure --prefix=$HOME/gcc/gcc-4.8.2
make -j8 bootstrap

When that finishes, run the install too. Then add $HOME/gcc/gcc-4.8.2/bin (the name you specify in --prefix plus /bin) to your PATH ahead of /usr/bin.

With a decent MacBook Pro with a 5400 rpm spinning disk, it takes an hour or two to compile everything (using the -j8 option to make), and requires multiple gigabytes of disk space while compiling. SSD is nice when doing this (definitely faster)!


GCC 4.9.0 was released on 2014-04-22. I've installed it using basically the same process, but with CLooG 0.18.1 and ISL 0.12.2 (required updates) and GMP 5.1.3 (and 6.0.0a), MPC 1.0.2 (or 1.0.1) and MPFR 3.1.2 on Mac OS X 10.9.2 Mavericks and an Ubuntu 12.04 (Precise Pangolin) derivative. Beware that the gmp-6.0.0a.tar.xz extracts into directory gmp-6.0.0 (not gmp-6.0.0a as you might expect).


Between 2014 and 2017-09-27, I've built GCC versions 4.9.0, 4.9.1, 5.1.0, 5.2.0, 5.3.0, 6.1.0, 6.2.0, 6.3.0, 7.1.0 with only minor variations in the build script shown below for GCC 7.2.0 on macOS v10.12 (Sierra). The versions of the auxiliary libraries changed reasonably often.


macOS Sierra and High Sierra

On 2017-08-14, I used a minor variant of the script above to build GCC 7.2.0 on macOS v10.12 (Sierra) (using Xcode 8 as the bootstrap compiler). One change is that CLooG doesn't seem to be needed any more (I stopped adding it with GCC 6.2.0). This is my current script:

#!/bin/bash

#export DYLD_LIBRARY_PATH=$(clnpath $(dirname $(dirname $(which g++)))/lib:$DYLD_LIBRARY_PATH)
unset DYLD_LIBRARY_PATH

TAR=/opt/gnu/bin/tar
VER_NUM=7.2.0
GCC_VER=gcc-${VER_NUM}
TGT_BASE=/opt/gcc
TGT_DIR=${TGT_BASE}/v${VER_NUM}
CC=/usr/bin/clang
CXX=/usr/bin/clang++

extract() {
    echo "Extract TAR -xf $1
}

if [ ! -d "$GCC_VER" ]
then extract ${GCC_VER}.tar.xz || exit 1
fi

(
cd ${GCC_VER} || exit

nbncl <<EOF |
    gmp     6.1.2   tar.lz
    isl     0.16.1  tar.bz2
    mpc     1.0.3   tar.gz
    mpfr    3.1.5   tar.xz
EOF

while read file vrsn extn
do
    tarfile="../$file-$vrsn.$extn"
    if [ ! -f "$tarfile" ]
    then echo "Cannot find $tarfile" >&2; exit 1;
    fi
    if [ ! -d "$file-$vrsn" ]
    then
        (
        set -x
        extract "$tarfile" &&
        ln -s "$file-$vrsn" "$file"
        ) || exit 1
    fi
done
)

if [ $? = 0 ]
then
    mkdir ${GCC_VER}-obj
    cd ${GCC_VER}-obj
    ../${GCC_VER}/configure --prefix="${TGT_DIR}" \
        CC="{CXX}"
    make -j8 bootstrap
fi

Make sure your version of tar supports all four different compressed file formats (.lz, .gz, .xz, .bz2), but since the standard Mac version of tar does that for me, it'll probably work for you too.

On 2017-09-27, I failed to build GCC 7.2.0 on macOS v10.13 (High Sierra) (using Xcode 9 for the bootstrap compiler) using the same script as worked on v10.12 (Sierra). The immediate error was a missing header <stack>; I'll need to track down whether my Xcode 9 installation is correct — or, more accurately, why it isn't correct since <stack> is a standard header in C++98 onwards. There's probably an easy fix; I just haven't spent the time chasing it yet. (Yes, I've run xcode-select --install multiple times; the fact that I had to run it multiple times because of network glitches may be part of the trouble.) (I got GCC 7.2.0 to compile successfully on 2017-12-02; I don't recall what gymnastics — if any — were required to get this to work.)

Time passes; version numbers increase. However, the basic recipe has worked for me with more recent versions of GCC. I have 7.3.0 (installed 2018-01-2), 8.1.0 (installed 2018-05-02), 8.2.0 (installed 2018-07-26), 8.3.0 (installed 2019-03-01) and now 9.1.0 (installed today, 2019-05-03). Each of these versions was built and installed on the current version of macOS at the time, using the current version of Xcode for the bootstrap phase (so using macOS v10.14.4 (Mojave) and Xcode 10.2.1 when building GCC 9.1.0).

🌐
OS X Daily
osxdaily.com › 2023 › 05 › 02 › how-install-gcc-mac
How to Install GCC on Mac
April 24, 2023 - GCC, which stands for Gnu Compiler Collection, is a commonly used compiler with support for C, C++, Objective C, Go, Fortran, Ada, and more languages, and is frequently used for compiling command line programs, writing software, and more, whether ...

Homebrew now has the GCC package so you can install it with this command:

brew install gcc
Answer from CommittedEel on Stack Overflow
🌐
GNU
gcc.gnu.org
GCC, the GNU Compiler Collection - GNU Project
The GNU Compiler Collection includes front ends for C, C++, Objective-C, Objective-C++, Fortran, Ada, Go, D, Modula-2, COBOL, Rust, and Algol 68 as well as libraries for these languages (libstdc++,...). GCC was originally written as the compiler for the GNU operating system.
🌐
Atomic Spin
spin.atomicobject.com › gnu-compiler-collection
Switching to GNU Compiler Collection for macOS Users
May 21, 2025 - Navigate to brew.sh, copy their install command, and paste it into your macOS terminal and follow the instructions. We need Homebrew to allow us to install GCC. Next, from your terminal, run brew install gcc.
🌐
Helsinki
wiki.helsinki.fi › xwiki › bin › view › HUGG › Helsinki University Geodynamics Group (HUGG) wiki › Tutorials and HOWTOs › GNU compiler install on Mac OS X
GNU compiler install on Mac OS X - XWiki
Step 3: Now that Xcode is fully installed, we can move on to downloading the GNU compilers from the High-Performance Computing for Mac OS X website. The easiest way to get there is to open a web browser, Safari in this case. And type in "High-performance computing Mac OS X" in the Google search.
🌐
Softpedia
mac.softpedia.com › development › compilers › gcc
GCC - Download (Mac) - Softpedia
Download GCC 15.2.0 for Mac - A comprehensive and useful compiler system that comes with in-built front-ends and libraries designed to support a wide variety of programming languages
Find elsewhere
🌐
Reddit
reddit.com › r/c_programming › do i need gcc on mac?
r/C_Programming on Reddit: do i need gcc on mac?
February 3, 2024 -

this might be a completely novice question but i am currently taking CS1 and my professor asked us to download VSCode and use GCC for the compiler. thing is I'm on a mac and from my understanding macs already come with an integrated compiler called clang (correct me if I'm wrong please). it has been a hassle trying to figure out how to get gcc to work on mac and i was wondering if this was really necessary for the class. i don't know what it means to use different compilers so if it really is necessary - like my professor would notice if I'm not using gcc - then if someone could please explain why that would be much appreciated.

again sorry for the seemingly novice question but this is my first real programming class where i have to type out actual code and use specific software. i took intro to C but all i learned there was different functions and how they work, none of this additional stuff was taught so I'm a bit out of my element here to say the least.

🌐
Nmattia
nmattia.com › posts › 2023-01-09-introduction-build-gcc-gnu
Nicolas Mattia – Intro to building GCC and some other GNU projects (focus on macOS & Apple Silicon)
My development machine is an Apple Silicon (M1) MacBook and hence the focus of this article is on macOS, but most of it will apply to Linux and potentially Windows (with WSL) too. GCC is not just a single compiler, but (similar to clang) it’s a compiler collection — the GNU Compiler Collection.
🌐
Quora
quora.com › What-is-GCC-Does-it-come-pre-installed-in-macOS-If-so-why-arent-all-apps-written-using-it-then
What is GCC? Does it come pre-installed in macOS? If so, why aren't all apps written using it then? - Quora
Answer (1 of 2): 1. GCC is short for “GNU compiler collection”. It is based on multiple languages, where the developers created three phases for compiling * the first phase was parsing and semantic translation to an internal form that was ...
🌐
Codeforces
codeforces.com › blog › entry › 101012
[Mac Tutorial] Setting Up GNU's C++ Compiler on MacOS - Codeforces
gain free contributions help them install GNU's GCC. GCC has many useful libraries that Clang (Mac's default c++ compiler) doesn't have.
🌐
Apple Community
discussions.apple.com › thread › 5250041
how to install gcc compiler on mac - Apple Community
July 13, 2017 - If one does not have the current OS X/macOS command-line developer compiler tools installed with the clang/llvm compilation tools, then homebrew won't be able to spend an hour compiling the less efficient GNU compiler.
🌐
SourceForge
sourceforge.net › projects › gdcmac
GCC D Compiler for Mac OS X download | SourceForge.net
Download GCC D Compiler for Mac OS X for free. GCC D Compiler (GDC) for Mac OS X (10.2-10.4) See http://gdcproject.org/ for new GDC development.
🌐
Homebrew
formulae.brew.sh › formula › gcc
gcc — Homebrew Formulae
GNU compiler collection · https://gcc.gnu.org/ License: GPL-3.0-or-later WITH GCC-exception-3.1 · Development: Pull requests · Formula JSON API: /api/formula/gcc.json · Formula code: gcc.rb on GitHub · Bottle (binary package) installation support provided for: Current versions: Other versions: Revision: 1 ·
🌐
nixCraft
cyberciti.biz › nixcraft › howto › macos › mac os x: install gcc compiler with xcode
Mac OS X: Install GCC Compiler with Xcode - nixCraft
December 11, 2017 - How do I install gcc on Mac OS X 10.8.x? Xcode includes command line development tools such as gcc and friends. First, make sure Xcode is installed. If it is not installed on OS X, visit app store and install Xcode. Fig.01: Make sure Xcode developer tools are install OS X ... Fig.02: Installing gcc compiler on Mac OS X Xcode will download package and install copies of the core command line tools and system headers into system folders, including the LLVM compiler, linker, and build tools.
🌐
Quora
quora.com › Is-the-GCC-compiler-safe-to-install-on-a-Mac-computer
Is the GCC compiler safe to install on a Mac computer? - Quora
Answer (1 of 6): I'm not sure what would be unsafe about it. Frustrating, yes. But unsafe? If you are truly worried, that's what Time Machine backups are for. You have backups, right? I currently have a self-built copy of GCC 9.2 on my personal MacBook Pro. You'll want to first install Apple's ...
🌐
MacPorts
ports.macports.org › port › gcc12
Install gcc12 on macOS with MacPorts
The GNU compiler collection, including front ends for C, C++, Objective-C, Objective-C++ and Fortran. ... If not done already, install MacPorts.
🌐
Millersville
cs.millersville.edu › ~gzoppetti › InstallingGccMac.html
Installing GCC for Mac Users
This is a guide for installing GCC on a Mac using Homebrew. Install Homebrew. After Homebrew installs, type "brew install gcc" in the terminal. Once the install completes, attempt to compile a C++ source file using the following command: "g++-12 -Wall Program.cc -o Program".
🌐
Quora
quora.com › How-can-we-compile-C-programs-in-macOS-using-GCC-GNU-Compiler-Collection
How can we compile C programs in macOS using GCC (GNU Compiler Collection)? - Quora
Answer: The question is do you care about accessing the full features of the MacOS operating system or a modern version of GCC? If you care about the full feature of MacOS the the first step is to install the GCC from the MacOS package manager. The ...