The Mac is well known for doing photos, graphics, movies, and music. The fun stuff and the life stuff, if you know what I mean. That’s what many people go to the Mac for. But you know your Mac can do programming too. Definitely, right? After all, you have heard that system administrators and programmers also love the Mac. Surely there must be a good reason why they’re loving the Mac platform too?
Update: There a new edition of this post — Setting Up Your Mac For Programming.
Well, the answer is mostly a resounding yes. Definitely an easier platform to start on. Oh, maybe there needs to be some prep work. But most of it is pretty easy. It’s definitely a whole lot better than Windows. While it is arguably not alien from a Linux platform, there are indeed differences, yet at the same time, there are similarities.
I’m writing this post to help people get started using their Macs for programming, particularly in a back-to-basics environment using a Unix shell with command line tools and such. Yes, I know, there are wonderful GUIs and IDEs. But I’m going down to the basics. The sort of thing that an introductory programming course at university level would expect you to do.
First thing is that you’ll be pleased to know your Mac already comes, out-of-the-box, with a Unix shell environment and plenty of command line tools. The shell, or command line, is accessible via Applications -> Utilities -> Terminal. Or, just spotlight (Cmd-Space) for Terminal. If you like, drag the Terminal app to your Dock, so that it’s more easily accessible in future.
That’s how the Terminal looks like. Play around with it for a moment. This is the bash shell, so it’s as true to a Unix shell as you can get (and totally unlike the Command Prompt you get in Windows). I’m not trying to write a Dummies Guide to the Unix Shell in here, so at this point, if you feel totally lost, I’d advise you to Google around for some good tutorials.
You’ll be glad to note that many command line tools are already included in OS X. For example, editors like vi (my favourite!) and nano. Scripting languages like Perl, PHP, Python, and Ruby. Version control like cvs and subversion. You’ll feel totally at home if you came from a Unix programming environment.
If you need to login to a Unix host, or copy files back and forth, both ssh and scp are already in there. There’s rsync too, if you need to keep directory trees in sync.
What about compilers and, well, the real programming stuffs? Well, it’s a free download, or two, away. Go to the Mac App Store and download Xcode. Xcode is a suite of tools for developing software on OS X. Nowadays, the Xcode command line tools are hidden away within the application. So to conveniently have all your favourite GNU C compiler and other support tools directly available in the standard location (i.e. /usr/bin), you need an additional download of the Command Line Tools from within Xcode. Go to Preferences, Downloads tab, then click and install Command Line Tools.
Once you’ve done that, you’ll have all your favourite stuffs in the default search path, and hence easily accessible in the shell. Things like gcc, g++, gdb, make, strip, etc, etc.
To leverage on other additional 3rd party open source stuffs, the easiest and best supported solution is to go with MacPorts. It’s an easy-to-use system for compiling, installing, and upgrading either command-line, X11 or Aqua based open source software on OS X. Go install it, and from there you’ll have easy access to a whole bunch of other open source software not unlike how you’d use a package repository system on other Linux distributions (although certainly more like Gentoo than Ubuntu).
Starting from Mountain Lion, Apple has stopped bundling their own X Window system. Instead, they recommend you use the open source XQuartz. Get this, if you think you’ll be needing X11. XQuartz actually descends from Apple’s own X11.app program.
Let’s try a small source building exercise. TextMate is a rather powerful programmer’s text editor for OS X. Actually, I don’t use it myself. But since some people recommend it, I thought I’ll give it a try. Their next major version, TextMate 2, has been released as open source. The source is free to download, but you’ve got to build your own binary. So here we have an opportunity to get a taste of program development in OS X.
So first, some pre-requisites:
- Install Xcode and get the Command Line Tools as described above.
- Install MacPorts.
Make sure you’ve accepted the Xcode license before continuing. Otherwise it will disrupt your automated command line builds.
$ sudo xcodebuild -license
Install a bunch of prerequisites as specified by TextMate 2. It may take a while, because MacPorts basically builds all the stuffs from source. That’s why I mentioned it’s more like Gentoo (emerge) rather than Ubuntu (apt-get).
$ sudo port install ninja ragel boost multimarkdown mercurial
Then, go ahead and clone a repo of TextMate 2 and build it.
$ git clone https://github.com/textmate/textmate.git $ cd textmate $ git submodule update --init $ ./configure && ninja
Building TextMate 2 is pretty quick. At the end of the build, TextMate 2 is launched automatically. Here’s a nifty screenshot with the “Hello World” program loaded in the editor.
The application is left in ~/build/TextMate/Applications/TextMate/TextMate.app (remember OS X apps are actually directories). If you like, you can move this to your Applications folder.
There, you’ve just built your first app, one that is rather more complicated and certainly a whole lot more useful than the usual “Hello World” program.
Although I usually prefer to work with command line tools, there are certain things I’ll admit are much easier with graphical tools. One example is browsing and updating MySQL database content. I use Sequel Pro to do this because it is a whole lot more convenient than trying to do with the text-based MySQL client. It even does SSH tunnelling for me automatically.
Despite its name, Sequel Pro is a free download. There are more of such nifty applications too. The Eclipse IDE runs on the Mac too, in case you’re familiar with it and wonder about that.
If you’re into Android or Arduino, both environments provide development kits on the Mac as well. For source code management, on top of what has already been mentioned earlier, git is included in Xcode, and mercurial was built from MacPorts as part of prepping for the TextMate 2 build.
The Mac offers a truly unique programming experience because it really combines the best of both worlds. On the one hand, you have true blue Unix command line tools in a real Unix environment. Yet at the same time, you enjoy a rich graphical experience with plenty of useful GUI apps. Not just the fun and the life stuffs, but GUI apps that also help with software development.
ps: Please remember to like https://www.facebook.com/zitsengcom to follow us on Facebook!
I just got a MBA and was planning to do some PHP coding. I am looking at Eclipse and Vi(Vim).
Any opinions on which is better? (Like code completion etc…)
I was wondering what is the difference between Vim and Vi, it says on Vim that it is backwards compatible with Vi.
Xcode seems a little daunting for me though. 🙁
I don’t use Eclipse, so I can’t tell you much about it. Vi and Vim on the Mac, and also on many Linux distros, are usually the same thing (vi is symlinked to vim). Vim is the improved version of Vi. In some environments, however, they are kept distinct. E.g. if you are a SoC user, the central Solaris login hosts have the original vi.
Vim won’t do auto-complete sort of stuffs. It’s still pretty much a basic editor (though it can also be powerful in its own ways).