Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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, brewing 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.

share|improve this question
    
What's the output of gcc --version? I get i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00) and I simply installed Xcode from the MAS. Also what's the output of echo $PATH? –  Gio Jan 25 '12 at 12:02
    
@Gio I got the same output. $PATH is the usual (this is an almost blank system, after all): /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin … but I don’t think that this matters. The formulae provided by homebrew-alt are simply outdated and don’t work with Clang. –  Konrad Rudolph Jan 25 '12 at 12:16
    
Why use Homebrew? MacPorts is good at managing multiple versions of compilers. And MacPorts can do anything that Homebrew can do. –  Jeff Burdges Oct 19 at 11:54
    
@JeffBurdges Homebrew is better. In pretty much any category. –  Konrad Rudolph Oct 19 at 13:11
    
How so? To be honest, I dislike Homebrew mostly just because it lacks (a) package descriptions and (b) math packages. Overall MacPorts has maybe six times as many packages as Homebrew, but yeah it's the obscure packages that're missing. I'm happy with MacPorts overall, although obviously I wish they'd integrate with language package managers like CPAN, CTAN, Caball, etc. –  Jeff Burdges Oct 19 at 19:41

5 Answers 5

up vote 20 down vote accepted

The solution provided by @Konrad Rudolph is not entirely correct anymore as the GCC formula that he mentioned was moved from homebrew/dupes to homebrew/versions. You can choose which version of GCC to install. For example, at the time of writing this answer, version 4.5, 4.7 and 4.8 are available. You may check out what versions are available here.

In short, you can install GCC 4.8 by using

brew tap homebrew/versions
brew install [flags] gcc48

You can view available install flags by using

brew options gcc48
share|improve this answer
    
I tried this and I got it to install but it dies like this: gcc-4.8 hello.c gcc-4.8: error trying to exec 'as': execvp: No such file or directory –  Warren P Mar 27 '13 at 21:36
    
@WarrenP Did you install with any [flags]? On my machines, I installed without any flags and it works fine. Why not you try to call gcc-4.8 without any input file and see if you get a fatal error? Also try to ls /usr/local/Cellar/gcc48/4.8.0/bin and see if that directory contains gcc-4.8. –  yihangho Mar 28 '13 at 9:28
    
It does contain gcc-4.8 and gcc-4.8 --version runs but it can't exec out to the secondary tool named as and so it dies. –  Warren P Mar 28 '13 at 12:19
    
Hmm... Frankly I have no idea how to solve this. Maybe we have to wait for some more experienced user... –  yihangho Mar 29 '13 at 0:57
1  
@nkint Either that or add alias g++='g++-4.8' in your .bashrc. –  yihangho Jun 8 '13 at 14:38

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.

share|improve this answer
1  
Trying to compile a working version of wine, I also ran into the problem of a missing gcc - unfortunately installing v4.7 via homebrew gave me some errors; so I went for the Apple v4.2 using brew tap homebrew/dupes followed by brew install apple-gcc42. This helped me to get a working gcc and wine. You can also get the binaries (as homebrew does) from here: r.research.att.com/tools –  iolsmit Jun 12 '12 at 13:23
    
@iolsmit You should definitely try the GCC 4.7 install again with a clean, up to date Homebrew. It should work like a charm. If not, please file a bug to the Homebrew-dupes maintainers. –  Konrad Rudolph Jun 12 '12 at 13:54
3  
brew tap homebrew/dupes; brew install gcc --use-llvm. See github.com/Homebrew/homebrew-dupes/issues/20 –  phs Jun 21 '12 at 5:51
    
@KonradRudolph everywhere I turn you're answering the questions I'm asking! Thanks :) –  Richard Smith-Unna Jan 9 '13 at 9:53
1  
@Warren No need to shout – but thanks for the reminder, because yihang has actually posted a better up to date answer which I’ve now accepted. –  Konrad Rudolph Mar 28 '13 at 11:31

I too had this problem. What's more is that I need to use gfortran, which doesn't come with the standard (outdated) install of Mac's llvm-gcc.

I've found the High Performance Computing builds of gcc to be up-to-date and work great on Lion.

share|improve this answer
    
I tried this. Not only did it not work, but the installation technique he's using is pretty dangerous. Took me about an hour to carefully undo everything. –  Bob Aman Apr 30 '12 at 21:36
2  
Oh, I should have said I don't install it the way he suggests. I just untar the file and make a symlink to the binary I want, making sure that symlink is somewhere in my PATH. Usually this is done by adding something "local" to my path, like ~/bin/ or something. –  cm2 Apr 30 '12 at 22:16
    
Yeah, that'd be way more sane. His install procedure is freaking nuts and I should have just assumed as much rather than giving it a go blindly. –  Bob Aman May 2 '12 at 13:38

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).

share|improve this answer

You might try this - https://github.com/kennethreitz/osx-gcc-installer - I have used it successfully after upgrading to Lion to compile packages with Homebrew and others (Ruby).

From the read me:

OSX GCC Installer Downloading and installing the massive Xcode installer is a huge hassle if you just want GCC and related tools.

The osx-gcc-installer allows you to install the essential compilers from either pre-built binary packages or helps you create your own installer.

share|improve this answer
    
This only installs the versions also installed by Xcode. In other words: outdated, and not actually GCC but LLVM-GCC. –  Konrad Rudolph Jan 25 '12 at 12:56
1  
Outdated: Yes. The point of the package is it is the Xcode shipped utilities, but without all the other huge components necessary for Xcode. Not GNU GCC: Wrong. /usr/bin/gcc is a symlink to llvm gcc, but explicit reference to /usr/bin/gcc-4.2 or setting export CC="gcc-4.2" in your .bash_profile or suitable file mitigates the issue as well. (See also this Stack Overflow question.) –  Jason Salaz Jan 25 '12 at 16:53
1  
Note that this project has been superseded by Apple's official Command Line Tools for Xcode: kennethreitz.org/essays/xcode-gcc-and-homebrew –  Arto Bendiken Mar 19 '13 at 23:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.