Building Emacs with Xcode 4
So you want the latest version of Emacs (currently 23.3) and being a developer you want to build it yourself; not download someone else’s precompiled version.
The problem is if you’re using Xcode 4 when you try to build the source for 23.3 you’ll get a nasty error message saying Emacs hates your freedom and you should die, die and go to hell. Or something like that, I’m paraphrasing.
Luckily this is fixed in the development version of Emacs so let’s live by the seat of our pants, mix our metaphors, and install Emacs 24.0!
What are we trying to accomplish?
When we’re done we’ll be able to:
- Build Emacs ourselves using Xcode 4.0.1’s version of GCC.
- Make Emacs use our Apple’s native GUI not X11.
- Run Emacs from the command line with or without a GUI.
This article may seem painfully long but what we’ll be doing is really simple. You’ll spend more time waiting for downloads and builds than following the steps I’ve outlined.
Also some of the steps may be unnecessary like the make then make install’s. They’re just cargo-culting on my part because I’m not very familiar with the Unix build system; I’ve read that I should do them both without explanation and they work so I’ll use ‘em till I learn more and understand what I’m doing.
Building development software
Apparently the source code for development versions of Unix apps don’t included the configure files needed to create a Makefile specific to your OS. This is true for Emacs as well so we need to create that configure file ourselves.
The good news is Emacs includes a shell script, autogen.sh, that will create the configure file for us. The bad news is the build tools that come with OS X won’t work because they are hopelessly out of date.
So, before we can even think about building Emacs we need to update our GNU build tools.
Update your path first!
The very first thing we need to do is make sure /usr/local/bin is first in the path. This is where the new GNU build tools are installed by default. By making sure /usr/local/bin is first in the path we make sure our updated build tools are used instead of the older ones.
To test our path open up a terminal and type the following:
$ echo $PATH > /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/X11/bin
If you see /usr/local/bin before /usr/bin, like in the above output, you’re good to go and you can skip ahead to Updating the GNU build tools.
If /usr/bin is before /usr/local/bin you need to edit your path. Open up the /etc/paths file and move /usr/local/bin so it’s above /usr/bin:
$ sudo nano /etc/paths
When you’re done it should look something like this:
/usr/local/bin /usr/bin /bin /usr/sbin /sbin
Now you need to reload the environment so close the terminal window and completely exit the Terminal app. You can’t just close the window you need to quit Terminal completely; otherwise the path won’t be updated.
Updating the GNU build tools
Now that our shell environment is set up to point to where we’re going to install the newer build tools we can get started actually installing them.
Create a temporary source directory
In the terminal window create a source directory in your home directory to download the source code into. For this tutorial I’ll use source.
$ mkdir ~/source
Download the build tools
We need to update automake, autoconf, and the M4 macro processor so download the following apps into the ~/source directory.
GNU M4 version 1.4.16 (or higher):
http://ftp.gnu.org/gnu/m4/m4-1.4.16.tar.gz
Autoconf version 2.68 (or higher):
http://ftp.gnu.org/gnu/autoconf/autoconf-latest.tar.gz
Automake version 1.11.1 (or higher):
http://ftp.gnu.org/gnu/automake/automake-1.11.1.tar.gz
Now in the terminal window uncompress all the tarballs in the source directory:
$ cd ~/source $ for a in `ls -1 *.tar.gz`; do tar -zxvf $a; done
When it’s done you should have three directories with the source code for each app.
Build and install the GNU M4 macro processor
We need to compile and install the build tools in a specific order; Automake requires Autoconf which in turn requires GNU M4 so we’ll start with that and work our way forwards.
Back in the terminal window do this:
$ cd ~/source/m4* $ ./configure $ make $ sudo make install
Make sure the new version shows up:
$ m4 --version > m4 (GNU M4) 1.4.16 > [etc...]
If you don’t see 1.4.16 or higher then completely exit and shutdown your terminal or exit out of your SSH session. I had to do this for some steps but not others so you’ll need to verify version numbers after each install.
Build and install Autoconf
With the GNU macro processor updated we can now update Autoconf.
Returning to our terminal window:
$ cd ~/source/autoconf* $ ./configure $ make $ sudo make install
Same deal as with the GNU M4 we want to make sure we have the right version:
$ autoconf --version > autoconf (GNU Autoconf) 2.68 > [blah, blah, blah]
You should see 2.68 or higher. If not shutdown your terminal or exit out of your SSH session. I didn’t need to exit for this one to show the correct version.
Build and install Automake
The final tool we need to update is Automake. This is used along with Autoconf to build the configure file.
Returning to our terminal window:
$ cd ~/source/automake* $ ./configure $ make $ sudo make install
Now we check we’ve got the right version:
$ automake --version > automake (GNU automake) 1.11.1 > [something, something, something]
The version should be 1.11.1 or higher. If not shutdown your terminal or exit out of your SSH session. I did have to exit out for this one to take affect.
You have Git installed right?
If you don’t already have git you’ll need to install it so you can download the latest build of Emacs.
You can download Git from here: http://git-scm.com/
I use Homebrew to install lots of these little utilities but unfortunately they don’t have the build tools we need.
http://mxcl.github.com/homebrew/
Download, build, and install Emacs
We can now clone the latest development version of Emacs from the source repository and build it ourselves.
Download the source
$ cd ~/source $ git clone git://git.savannah.gnu.org/emacs.git
Create the configure file
$ cd ~/source/emacs $ ./autogen.sh > Checking whether you have the necessary tools... > (Read INSTALL.BZR for more details on building Emacs) > > Checking for autoconf (need at least version 2.65)... > ok > Checking for automake (need at least version 1.11)... > ok > Your system has the required tools, running autoreconf...
Configure, build, and install
We need to set a few parameters when running configure so it uses our native Cocoa libraries and ignores some image libraries.
If you have Homebrew installed you can install all but the png and the gif libraries but that’s out of the scope of this article. (I haven’t had any problems not installing the libraries)
Note that there are two lines here, not three. The \ allows us to break a single line over multiple lines.
$ ./configure --with-jpeg=no --with-png=no --with-gif=no \
--with-tiff=no --with-ns --with-x=no
$ make && make install
Copy Emacs to the application directory
Using the “—with-ns” parameter means when we build Emacs it won’t actually install anything in our path but instead will build a fully contained Emacs.app in the nextstep directory. We need to copy the app file somewhere it can be used and the logical place would be the /Applications directory.
$ cp -R ~/source/emacs/nextstep/Emacs.app /Applications
(If the cp command doesn’t work try prefixing it with sudo)
You can now run Emacs from your Applications directory and have a glorious non-X11 64-bit Emacs.

But what about the command line?
We can run Emacs from the application directory but wouldn’t it be nice if we could start it from the command line too? Plus you should be able to run the non-graphic version in case you’re SSH’d into a remote computer.
Emacs comes with a command line switch -nw, short for --no-window-system, that will open Emacs in the shell but we need to jump through a few more hoops to make it work with both the graphical version and the shell version while also accepting parameters properly.
Easy enough; create a file called emacs in the /usr/local/bin directory.
$ nano /usr/local/bin/emacs
Now with the file open in nano copy the following text into it.
#! /bin/bash # file: /usr/local/bin/emacs cd /Applications/Emacs.app/contents case "$*" in *-nw*) ./MacOS/emacs $*;; *--no-window-system*) ./MacOS/emacs $*;; *) ./MacOS/emacs $* &;; esac
Close and save (<ctrl>x then y and then press enter).
We also need to make the shell script executable so:
$ chmod +x /usr/local/bin/emacs
I again had to exit my SSH session and relog to get the proper version but here it is:
Notice that this is the 32-bit version verses the 64-bit version for the graphical interface.
Yakshemash! Great Success!
We’re done. Now you can open the graphical version of Emacs from the shell just by typing:
$ emacs
And you can open the shell version by typing:
$ emacs -nw
I some aliases in my .bash_profile file that cut down on the keystrokes:
alias em="emacs -nw" alias emw="emacs"
In closing
This is a fairly long article so I followed it myself to make sure everything works. That being said it’s not unlikely I forgot something or make a typo somewhere so please let me know if you catch any errors on my part.
Also if I forgot something or could do something better let me know that as well.

