Install the Command Line C Compilers in OS X Lion

| How-To

Out of the box, OS X Lion doesn’t have the command line C compilers. Plus, Apple has once again changed the way you install those compilers in /usr/bin. Here’s how to do it with the latest Xcode and Lion.

Back in August, right after OS X 10.7, Lion, shipped, I wrote about how a new installation does not have the C compilers in the expected place, /usr/bin. This article, “OS X Lion for UNIX Geeks: Installing the C Compilers,” provided all the background.

Recently, professor Ulf von Barth of Lund University in Sweden alerted me to the fact that the mechanism has changed yet again. In the article linked above, I explained that all you had to do was download and install Apple’s Xcode IDE, and everything would be as expected. Now, you have to do a little more. But before I proceed, you should go back and read that article for reference on the C compilers, gcc, llvm, and all that jazz.

Motivation

There may be good reasons not to have a C compiler sitting around an average user’s Mac, ready to stir up trouble if accessed by malware. Thats why it’s not in the Mac by default. On the other hand, you may be taking a C class and want to use a C compiler (gcc now points to llvm) from the command line. Or you may be an administrator, and you want the C compiler to be available in a lab setting. Or you’re a researcher, and you don’t use Xcode for scientific computing. Or it may just be for show. Some day, an IT admin will come up to you and question you about the Mac, and you proudly announce that it’s based on (BSD) UNIX. This Linux guru will open the command line, type “cd /usr/bin; ./gcc -v” and then snort. “Aha. Nothing there. It’s not real UNIX! ” Not good.

gcc -1

Uh-oh. Not there.

For whatever reason you may have, here’s what you need to do nowadays. Note that, unlike before, when everything was free, you will need to be a registered Mac developer.

Procedure

1. Download Xcode, now at version 4.3.2, just as before, from the Mac App Store. Finder -> Apple -> App Store…  It’s always been free and still is.

2. Launch the Xcode.app that was downloaded to /Applications.

3. If you stop there, you’ll find that, unlike before, the command line compilers are not installed by default. You can go hunting for them, and you’ll it all in:

/Applications/Xcode.app/Contents/Developer/usr/bin

gcc -2

Oops. gcc/llvm now buried /Applications for use by Xcode

Phew! That won’t do you any good unless you intend to use Xcode exclusively. Very likely, all your conventional Make scripts won’t know where to find the C compilers (and linker and assembler) now, and you wouldn’t want to modify them anyway. So setting up links to the new location would be messy and likely will not work, creating new headaches. What you need is everything back in /usr/bin like before.

4. After launching Xcode, go to Preferences and select the Downloads pane, then Components. There, in the list of candidate items will be the Command line tools. Click “Install.”

gcc -3

Xcode’s downloads

5. You’ll be prompted for your developer credentials…

gcc -5

…then you’ll see the classic progress bar. When that’s done, the C compilers will all be in /usr/bin, as desired. To prove that all’s well, open a terminal window, cd to /usr bin, and take a look. Voila.

gcc -4

All is well again in /usr/bin

It’s a shame we have to go through all that these days, but I can see how Apple feels that the majority of users don’t need these tools, and those who do will find out how to get them. I suppose you could call that a subordinate claws.

_________

My thanks to Dr. Ulf von Barth of Lund University in Sweden and Dr. Gaurav Khanna, Physics Dept., the University of Massachussetts for their assistance with this article.

Sign Up for the Newsletter

Join the TMO Express Daily Newsletter to get the latest Mac headlines in your e-mail every weekday.

Comments

mlvezie

WHAT?! (wisps of steam float from my ears…)

I have to PAY Apple A HUNDRED BUCKS to use GCC? Are they allowed to do that? Does the Free Software Foundation have anything to say about this?

Jeff Butts

I have to PAY Apple A HUNDRED BUCKS to use GCC?

I haven’t tried, but it looks like you can install gcc from https://github.com/kennethreitz/osx-gcc-installer instead.

gdelfino

XCode is a free download from the App Store. I just double checked.

nick martini

1. Download Xcode, now at version 4.3.2, just as before, from the Mac App Store. Finder -> Apple -> App Store?  It?s always been free and still is.

False. A year ago it was $5 on the app store.

John Martellaro

Perhaps, because it’s all provided free in the Xcode install, just in an awkward place, Apple is off the hook?  I don’t know.  The nuance is getting the compilers where you want them.

geoduck

Note that, unlike before, when everything was free, you will need to be a registered Mac developer ($99/yr).

1. Download Xcode, now at version 4.3.2, just as before, from the Mac App Store. Finder -> Apple -> App Store?  It?s always been free and still is.

Sorry but I’m confused. What do I have to pay for and what is free? Is it that the the software is free but you can’t get it unless you’re a Mac Developer? Is it that you can get the software for free but it won’t run on your mac unless some magical Developer Dust is sprinkled over your Mac? I’m just a bit confused over this part of the article.

John Martellaro

geoduck.  It appears to me that while Xcode is free, to download the command line tools, you must be a developer.

Nick Martini

I haven’t paid for anything, I have a free account and was able to install the command line stuff.

John Martellaro

Nick: There still are free developer accounts. I thought those went away. So everything is free! Sigh…

Oliver Schrenk

???

Why don’t you just download the “Command Line Tools for Xcode” from http://developer.apple.com/downloads ?

It’s a 171 MB download. You don’t need to install XCode. All you need is a free AppleID.  See the blog entry from Kenneth Reitz http://kennethreitz.com/xcode-gcc-and-homebrew.html to get some history on the topic.

geoduck

Thanks, that clarifies it.

pz

Is it really true that there is malware that uses gcc??

Anyway why fuss with Xcode if you just want gcc? You can get newer versions prepackaged as binaries at

http://hpc.sourceforge.net/

gcc.version

Probably should have been `

./gcc --version 

`.

JonGl

I haven?t paid for anything, I have a free account and was able to install the command line stuff.

Hm. I also have a free developer’s account from way back, and I don’t see gcc as an option. Now I do have MacPorts installed, so would that have something to do with it?

-Jon

JonGl

Why don?t you just download the ?Command Line Tools for Xcode? from http://developer.apple.com/downloads ?

Well, this worked at least. Thanks for that link/reminder. I don’t do a lot of commandline compiling, and haven’t for a while outside of MacPorts or Fink, but when I need it, I want to have it, so thanks! And thanks John for the article.

-Jon

Tom

I simply added the Xcode bin directory to my path with the following line in my ~/.profile

export PATH=/Applications/Xcode.app/Contents/Developer/usr/bin/:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin 

My question is, are there additional tools in the Command Line Tools that I don’t not have access to with this method?

Rupesh

Thanks for the steps!

Andy

Realizing this entry and many of the comments are old, but I just downloaded Xcode with no fuss, and also created a developer account using my Apple ID and it didn’t cost me anything.  So maybe that’s something new?

Anyway, summary, no cost for me to go from nothing to having gcc available.  Unless you count the cost of my MacBook, which wasn’t exactly free.

Johnathan Duncan

Thank you for the tutorial, I’m learning C at command line and was missing compiler which was easily installed from Xcode.

Jason Drury

Thank you very much!

Log-in to comment