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.

Glorfindel
  • 3,774
  • 7
  • 30
  • 46
Konrad Rudolph
  • 2,056
  • 2
  • 15
  • 30
  • 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
  • 1
    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 '14 at 11:54
  • @JeffBurdges Homebrew is better. In pretty much any category. – Konrad Rudolph Oct 19 '14 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. – Jeff Burdges Oct 19 '14 at 19:41
  • @Jeff Personally I haven’t yet found a package that wasn’t on Homebrew (this question notwithstanding). I cannot talk about math packages. Package descriptions do exist (`brew info …`). Apart from all other advantages, its usability is just much nicer, its interface more modern, and creating own formulas is *much* easier. – Konrad Rudolph Oct 19 '14 at 23:17
  • Homebrew has package descriptions only for – Jeff Burdges Oct 20 '14 at 07:49

7 Answers7

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.

herrtim
  • 105
  • 3
Konrad Rudolph
  • 2,056
  • 2
  • 15
  • 30
  • 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: http://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 https://github.com/Homebrew/homebrew-dupes/issues/20 – phs Jun 21 '12 at 05:51
  • @KonradRudolph everywhere I turn you're answering the questions I'm asking! Thanks :) – Rik Smith-Unna Jan 09 '13 at 09:53
  • This is OUT OF DATE NOW. – Warren P Mar 27 '13 at 21:37
  • 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
  • Excellent. I did a bit of editing on this one so that it's closer to true but still too complicated. – Warren P Mar 28 '13 at 12:18
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
quine
  • 753
  • 1
  • 7
  • 11
yihangho
  • 451
  • 5
  • 8
  • 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 09: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 00:57
  • How to add `g++` as a command in my terminal? do I have to soft link `/usr/local/Cellar/gcc48/4.8.1/bin/g++48` in my /usr/bin? – nkint Jun 07 '13 at 14:32
  • 2
    @nkint Either that or add `alias g++='g++-4.8'` in your `.bashrc`. – yihangho Jun 08 '13 at 14:38
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).

Tim Smith
  • 325
  • 2
  • 5
  • 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 '20 at 00: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: https://developer.apple.com/download/more/ – Frank Schmitt Feb 01 '21 at 19:42
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>.

Avi
  • 149
  • 2
  • 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 '17 at 19:17
  • @fsb Answer is updated. – Avi May 02 '17 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](http://apple.stackexchange.com/help/whats-reputation) you’ll be able to add [comments](http://apple.stackexchange.com/help/privileges/comment) and ask follow-up questions. To gain reputation, [answer questions that are clear and concise](http://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). – fsb May 02 '17 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 02 '17 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 08 '17 at 01:14
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.

cm2
  • 4,474
  • 1
  • 18
  • 20
  • 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 02 '12 at 13:38
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.

Darren Newton
  • 751
  • 2
  • 7
  • 13
  • 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](http://stackoverflow.com/a/7829247/106813) question.) – Jason Salaz Jan 25 '12 at 16:53
  • 2
    Note that this project has been superseded by Apple's official Command Line Tools for Xcode: http://www.kennethreitz.org/essays/xcode-gcc-and-homebrew – Arto Bendiken Mar 19 '13 at 23:57
  • the link kennethreitz.org/essays/xcode-gcc-and-homebrew is now bad. – Clem Wang Feb 09 '21 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