85

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.

7
  • 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, 2012 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. Jan 25, 2012 at 12:16
  • 1
    Why use Homebrew? MacPorts is good at managing multiple versions of compilers. And MacPorts can do anything that Homebrew can do. Oct 19, 2014 at 11:54
  • @JeffBurdges Homebrew is better. In pretty much any category. Oct 19, 2014 at 13:11
  • 3
    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. Oct 19, 2014 at 19:41

7 Answers 7

21

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

2
  • Ok and what about if I execute xcode-select --install and it throws xcode-select: error: command line tools are already installed, use "Software Update" to install updates. Then I list software updates by executing softwareupdate -l and it throws nothing to update (an empty list), which means that XCode Command Line Tools are installed.
    – Israel
    Dec 22, 2020 at 0:12
  • I'm not 100% clear on this but I think macOS considers Xcode to be a replacement for/superset of the CLT but Homebrew does not. You can grab a .pkg download here: developer.apple.com/download/more Feb 1, 2021 at 19:42
39

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.

7
  • 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, 2012 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. Jun 12, 2012 at 13:54
  • 3
    brew tap homebrew/dupes; brew install gcc --use-llvm. See github.com/Homebrew/homebrew-dupes/issues/20
    – phs
    Jun 21, 2012 at 5:51
  • @KonradRudolph everywhere I turn you're answering the questions I'm asking! Thanks :) Jan 9, 2013 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. Mar 28, 2013 at 11:31
31

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
6
  • 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, 2013 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, 2013 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, 2013 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, 2013 at 0:57
  • 2
    @nkint Either that or add alias g++='g++-4.8' in your .bashrc.
    – yihangho
    Jun 8, 2013 at 14:38
4

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.

3
  • 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, 2012 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, 2012 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, 2012 at 13:38
4

In case anyone looks at this old thread, it seems homebrew/versions, used in the currently accepted answer, has been deprecated in favor of homebrew/core. Just run brew install <formula>.

5
  • Can you please explain how this has to do with the OP's question which is how to install GCC via Homebrew?
    – fsb
    Apr 28, 2017 at 19:17
  • @fsb Answer is updated.
    – Avi
    May 2, 2017 at 19:24
  • This is more of a comment on the accepted answer. It should not be an answer itself because it doesn't address the OPs question. Once you have sufficient reputation you’ll be able to add comments and ask follow-up questions. To gain reputation, answer questions that are clear and concise.
    – fsb
    May 2, 2017 at 22:12
  • Should a new question be asked to specify the version of brew where this changes? Fixing this old post seems low benefit, high delay.
    – bmike
    May 2, 2017 at 23:17
  • 1
    If I had sufficient reputation points I would have commented on the accepted solution instead. Feel free to do that yourself.
    – Avi
    May 8, 2017 at 1:14
1

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.

4
  • This only installs the versions also installed by Xcode. In other words: outdated, and not actually GCC but LLVM-GCC. Jan 25, 2012 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.) Jan 25, 2012 at 16:53
  • 2
    Note that this project has been superseded by Apple's official Command Line Tools for Xcode: kennethreitz.org/essays/xcode-gcc-and-homebrew Mar 19, 2013 at 23:57
  • the link kennethreitz.org/essays/xcode-gcc-and-homebrew is now bad.
    – Clem Wang
    Feb 9, 2021 at 19:27
-1

Command:
brew update 
brew install gcc 
 location : /usr/local/Cellar/gcc/10.2.0_4: 1
 Command:
brew update 
 brew install app location: /usr/local/Cellar/gpp/2.27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .