Install Ruby 1.9.2 with Xcode 4.3

Update & TL;DR: Just learned how to get GCC 4.2 without manually compiling with homebrew-dupes on Thoughtbot's post The Hitchhiker's Guide to Riding a Mountain Lion:

brew tap homebrew/dupes
brew install apple-gcc42

after that we can install Ruby 1.9.2 by:

rvm install 1.9.2 -C --with-gcc=/usr/local/bin/gcc-4.2

It seems that the apple-gcc42 formula directly download a binary package of GCC 4.2, and there is no need to compile all the stuff. It took me only a minute to get gcc-4.2 working. Try it out!

The following original article shows detailed steps that I did to compile GCC 4.2 manually. Read on if you're interested in it.

And also note that Ruby 1.9.3 can be compiled with Clang. A GCC 4.2 compiler is required for Ruby <= 1.9.2 only.


Ruby 1.9.2 is currently the most popular Ruby runtime. We used to install it through RVM by typing rvm install 1.9.2. But it does depend on GNU C Compiler (GCC), which has been removed from the new Xcode 4.3 (and also from the Command Line Tools). Although UNIX tools for building programs exists, including LLVM and LLVM-GCC, there is no official GNU C Compiler, which is required to build some programs like official Ruby 1.9.2 interpreter (YARV).

At this point, Ruby 1.9.2 does not support LLVM-GCC. If you type rvm install 1.9.2, you'll get the error:

The provided compiler '/usr/bin/gcc' is LLVM based, it is not yet fully supported by ruby and gems, please read `rvm requirements`.

Although Ruby 1.9.2 can be compiled with Clang (LLVM's compiler), there might be problems. If you try to force --with-gcc=clang, you'll get a warning message:

Building 'ruby-1.9.2-p318' using clang - but it's not (fully) supported, expect errors.

In fact, it can't even pass its own test scripts. I did compiled Ruby 1.9.2-p318 with Clang successfully, but when I run make test in the source directory, 8 out of 935 tests failed.

Downgrading to Xcode 4.1 (comes with GCC) will resolve the problem, but I want to keep the latest version of Xcode. Luckily if you upgrade from Xcode 4.2.1 (as I did), you may still have /usr/bin/gcc-4.2 in your system. But if you did a clean install, you will not have that compiler.

I don't want to take the risk caused by building Ruby 1.9.2 with Clang. So I built it with a manually-compiled GNU C Compiler.

Here is how.

Shortcut for Unix geeks: Get the GCC tarball, make and install it, and rvm install 1.9.2 with that compiler.

Note: rvm install 1.9.3 works fine with Clang. This article is for 1.9.2 only.

Getting Started

First, you still need to install Command Line Tools for Xcode, which can be downloaded here (a free Apple Developer ID is needed). If you have already install Xcode.app, you can navigate to Xcode > Preferences > Downloads > Components, find "Command Line Tools," and click Install.

Also, Homebrew is needed to install some 3rd-party libraries. It's a modern Package Manager for OS X, as a replacement for MacPorts and Fink. -- But there is no GCC in Homebrew at this point. Although those older package managers have GCC packages, it is not recommended to use them on modern OS X. Just use Homebrew.

Besides, if you're on a MacBook, make sure that you have already plugged the power cable. Compiling a software means most of your CPU resources will be used, and it's power-consuming -- really consuming.

For more details, you can read GCC's official detailed instructions.

Download GCC Source Code

I downloaded gcc-core-4.6.3 from one of the mirrors. Choose the proper release version that you need, or just the latest release.

You only need the core pacakge, which is named like gcc-core-X.Y.Z.tar.gz. Do not download the full package gcc-X.Y.Z.tar.gz, which contains compilers for other languages such as Ada, Fortran or Java.

Install Prerequisites

Since Command Line Tools for Xcode is installed, we now already have C90-compatible compiler, assember, linker and other utilities. But still lacking some libraries required for building GCC, which are available via Homebrew. Just type

brew install gmp mpfr libmpc

Build GCC

Unpackage the source code by tar zxf gcc-core-blah.tar.gz, cd into it, and type

./configure --prefix=$HOME/usr/local

I think it is recommended to install it in a place other than the default /usr/local/ to avoid being messed-up with other software in your system (especially with Homebrew, which lives in /usr/local/). I put it at a corner of my Home directory.

After that, type

make

to build GCC. It will take a long time. It took about 1.5 hour to build on my MacBook Pro (mid-2010). Take a break.

Install GCC

Type

(sudo) make install

Now you can remove the entire working directory for building GCC. (Just drop it to the Trash)

Install Ruby 1.9.2 with GCC

First, install readline and iconv package if you never did that:

rvm pkg install readline iconv

Now you can install Ruby 1.9.2:

rvm install 1.9.2 -C \
--with-readline-dir=$rvm_path/usr \
--with-iconv-dir=$rvm_path/usr \
--with-gcc=$HOME/usr/local/bin/gcc  # put this at the last

Change the path of --with-gcc for your need.

Gems are Still Compiled with LLVM-GCC

Unfortunately our gems are still built with LLVM-GCC, which may cause problems like rb-fsevent 0.4.3.1 (which Octopress 2.0 depends on; issue here). Upgrading the gem may resolve the problem (at least rb-fsevent 0.9.0 doesn't have this issue); or you can build the gem manually.