Building Git on Mac OS X

    2 August 2007, mid-afternoon

This is a brief guide for those who want to build and install Git under MacOS X. The install process is fairly straightforward. The only hiccup is that the development environment for MacOS X 10.4 doesn’t come bundled the Expat XML Parser. I’m not sure who these instructions will be helpful to, as most people bothering to install Git probably know what they’re doing.

If you follow the steps below you will end up installing Git in /usr/local. If you haven’t already done so, make sure that /usr/local/bin is in your $PATH. Open up your .bash_login file and add the following, if you don’t see the path there already:

export PATH="/usr/local/bin:$PATH"

The search path is what your shell uses to determine the location of programs it can run. When you type git in the command line, it will scan through the various directories it knows applications are stored looking for git. The which command can be used to determine where an application is running from — i.e. after you have installed Git, the command which git should return /usr/local/bin/git.

Before we can build Git, we will need to download, build, and install the Expat XML parser. If you are on Leopard you can skip this step. The latest version of the library is 2.0.1, which can be downloaded from the project’s Sourceforge site. Once downloaded, open a terminal window and change to the directory the file is located. You’ll need to run the following commands:

tar xvzf expat-2.0.1.tar.gz 
cd expat-2.0.1
./configure --prefix=/usr/local
make
sudo make install

This will build and install the Expat library and header files in /usr/local, which is where you should be installing 3rd party libraries such as this. The first command extracts the source code from the archive you downloaded. The configure script is used to simplify building applications and libraries across multiple platforms. configure will perform some tests to make sure your system is capable of building the library, and will generate the Makefile used in the actual compilation process. make is what actually builds the program, executing all the commands needed to turn the source code into a working program; sudo make install is what actually copies the library and header files to their final locations. The reason we need to run make install with super-user privileges is that your normal account doesn’t have write access to /usr/local/.

Once done, you can now build Git. The process is similar to what was done to build the Expat library. You can download the latest version of Git from its home page — currently this is version 1.5.2.4. Once downloaded, open up your terminal again and change to the directory the archive is stored. You’ll need to run the following commands:

tar xvzf git-1.5.2.4.tar.gz
cd git-1.5.2.4
make configure
./configure --prefix=/usr/local
make all
sudo make install

You can erase the source directories, git-1.5.2.4 and expat-2.0.1, if you are so inclined.

You now have a working copy of Git. You can now type Git on the command line to see what all the fuss is about. If you have no clue what to do next, check out the tutorial online.

(Note: I haven’t covered building and installing the documentation here, as it is more trouble than it’s worth — trusts me. All the documentation is available online.)

Update 0ct 28th: You do not need to download and install Expat for Leopard. Expat comes installed Leopard.

Update March 1st 2008: See this comment below if the build process complains about gettext. I believe this is a requirement of the tcl-tk GUI Git ships with.

Update March 10th 2010: This advice on installing the man pages without building works perfectly.

 

Comments

  1. Thank you.

  2. Thanks, really helpful. One few comment:- missing the:
    to the end of export PATH=”/usr/local/bin:$PATH

  3. I get close, but I crash and burn with the make all, when I get:

    /usr/bin/ld: warning /sw/lib/libiconv.dylib cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
    /usr/bin/ld: Undefined symbols:
    _libiconv
    _libiconv_close
    _libiconv_open

    Something is awry? I read that I might have “stale” MacPorts on another link… but I can’t find the /opt directory that would have these ports from my prior ppc life.

  4. What exactly are you using MacPorts for? You shouldn’t need to use it at all if you follow the instructions above.

  5. Perfect. Thank you.

  6. Worked perfectly for me as well. Thank you.

    I had been trying to decide between Git and Mercurial. Since I found this tutorial—I’ll be trying Git first.

  7. Thanks very much for this tutorial! I really ought to learn the general means to install third party packages though— I think I’ve established the pattern from your tutorial (usr/local), but far too late! When I just got this mac book, I was entirely new to unix-like operating systems having been fed on windows for a decade. So there are a few path issues and incorrectly installed packages from that time. I’ll have to go through and weed them out soon…

  8. I had a lot of trouble building git, but it was not due to your excellent instructions. As it turned out when I installed this new Intel iMac I transferred information from my old G5 during setup. This included the old /opt structure and libraries from Macports, etc. These libraries had an architecture conflict.

    Git built fine when I removed the old /opt directory.

    Thanks

  9. Great!! Brief. To the point, and it worked. Thanks

  10. UGH.. I tried doing this on OS 10.4.11. Have developer tools installed and everything. Expat installed no problem, but doing make all on git I get:

    Generating catalog po/de.msg
    msgfmt —statistics —tcl po/de.po -l de -d po/
    make1: msgfmt: Command not found
    make1: *** [po/de.msg] Error 127
    make: *** [all] Error 2

    Apparently I dont have msgfmt installed..yet all my googling for getting it on my machine turn up fruitless..

    help!

  11. I installed gettext and all is well..

  12. Strange, thanks for the info.

  13. Worked great on my 10.4.10 Mac, Thanks!

  14. Thanks! Worked for me fine.

  15. I stand corrected. I too had to follow Stevie’s advice and install gettext.

  16. Thanks for the note, Stevie. On 10.5.2 I got the same error with msgfmt, installed gettext, and then git built fine.

    (gettext takes a long time to build!)

  17. I had the same problem as Stevie. Turns out I had a version of msgfmt hiding in an old Fink /sw directory. I deleted it and everything built fine.

  18. Thank you

  19. Gr8 advice! Thanks so much!
    Needed Git to use with Heroku..

  20. I think installing the manpages is easy. Just grab an archive from: http://kernel.org/pub/software/scm/git/git-manpages-1.5.5.1.tar.bz2
    (of course update the version number for your installation.)

    Then copy those files into your man page directory. If you installed git to /usr/local then you will want to copy those files into /usr/local/man.

    Then make sure that your $MANPATH is setup correctly.

  21. Nice tip: having the build generate them is a pain. There is this endless sea of dependencies on OS X that I couldn’t be bothered to sort out.

  22. perfect,

    can i build it as a .so?

  23. This tutorial was great – I was pleasantly surprised how quickly I had git and then git-gui up and running… now, just need to get my head around git properly :)

  24. I recommend you check out the peepcode book on Git. It’s pretty good.

  25. Thanks worked like a charm!

  26. Well written and concise. Very nice style and thanks for increasing the SNR.

  27. Thank You Very Much! well written and worked like a charm. Again, Thanks.

  28. Thanks for the nice write-up and explanation of the build process into the usr/local folder.

    This worked perfectly for me on os 10.5.6. Did not need to install anything but git.

    I’m new to mac and am just learning why usr/local is a good place for some tools.

  29. I just solved this:
    /usr/bin/ld: warning /sw/lib/libiconv.dylib cputype (18, architecture ppc) does not match cputype (7) for specified -arch flag: i386 (file not loaded)
    /usr/bin/ld: Undefined symbols:
    _libiconv
    _libiconv_close
    _libiconv_open

    By doing this: – Added the line NO_FINK=1 to the Makefile file

  30. thanks!
    worked for me too on os x 10.4.11 with expat 2.0.1 and git-1.5.2.4.

    didn’t work for newer version git-1.6.5.

  31. There might be slight changes in the newer version of git. I’ll try and see what’s changed tonight.

  32. I built things without any problems. I’m running Snow Leopard though. I’m not sure what would have changed.

  33. Oreeka, in Blackburn Lancashire, are a bunch of lame ass SEO spammers.

    [ed. fixed this comment.]

Don't be shy, you can comment too!

 
Some things to keep in mind: You can style comments using Textile. In particular, *text* will get turned into text and _text_ will get turned into text. You can post a link using the command "linktext":link, so something like "google":http://www.google.com will get turned in to google. I may erase off-topic comments, or edit poorly formatted comments; I do this very rarely.