How do I install GCC via Homebrew?
I am using Homebrew exclusively (i.e. I don’t, and won’t, use MacPorts or Fink). I also want to keep manual installations to a bare minimum.1
… But how can I install GCC?
First of all, I’ve of course installed Xcode but the current version doesn’t ship with a decently up to date GCC (I need ata least 4.5, but would prefer the most recent one) – in fact, it doesn’t ship with a proper GCC at all (it only ships Clang) and that seems to be a problem for Homebrew …
I’m aware of a list of custom GCC and cross compilers but in fact all of those installations require an already installed GCC – at least, brew
ing them fails with linker errors on Lion which I attribute to Clang, and -use-gcc
doesn’t work for obvious reasons.
brew doctor
only mentions what I already know, that there is no GCC (4.2.x) installed.
1 I’ve previously mixed MacPorts, Homebrew and some manual installations and have ended up with a maintenance and versioning hell. I don’t want to go there again.
I saw somebody link to this old post today. The best way to install GCC in homebrew right now is just brew install gcc
. If you have the XCode Command Line Tools (they are separate from XCode; you can install them with xcode-select --install
) installed, a precompiled version will be installed (which is very fast).
Homebrew solution
To answer my own question, homebrew-versions
now has a fairly up to date formula of GCC. It can be installed using
brew install [flags] https://raw.github.com/Homebrew/homebrew-versions/gcc48.rb
Where [flags]
should include all the required languages, e.g. (--enable-cxx --enable-fortran
).
This will install the executables with a suffix, i.e. gcc
has to be accessed as gcc-version
to avoid clashes. If necessary, one can create appropriate symlinks to make this version the default.
Manual installation
Alternatively, an up-to-date GCC (as of the time of writing) can be compiled manually using the following shell script:
VERSION=4.7.0
PREFIX=/usr/gcc-$(VERSION)
LANGUAGES=c,c++,fortran
MAKE=make
# Or
# MAKE='make -j 4' # to compile using four cores
brew-path() { brew info $1 | head -n3 | tail -n1 | cut -d' ' -f1; }
# Prerequisites
brew install gmp
brew install mpfr
brew install libmpc
# Download & install the latest GCC
mkdir -p $PREFIX
mkdir temp-gcc
cd temp-gcc
wget ftp://ftp.gnu.org/gnu/gcc/gcc-$VERSION/gcc-$VERSION.tar.gz
tar xfz gcc-$VERSION.tar.gz
rm gcc-$VERSION.tar.gz
cd gcc-$VERSION
mkdir build
cd build
../configure \
--prefix=$PREFIX \
--with-gmp=$(brew-path gmp) \
--with-mpfr=$(brew-path mpfr) \
--with-mpc=$(brew-path libmpc) \
--program-suffix=-$VERSION \
--enable-languages=$LANGUAGES \
--with-system-zlib \
--enable-stage1-checking \
--enable-plugin \
--enable-lto \
--disable-multilib
$MAKE bootstrap
make install
# Uncomment for cleanup …
# cd ../../..
# rm -r temp-gcc
This will stage GCC into the path /usr/gcc-4.7.0
. Now all you need to do is either create symlinks to the executables or add the bin
directory to the $PATH
variable.