Setting up a Mac on OS X 10.8 as a dev machine from scratch

Last updated on the .

Originally, these were the notes I took down when setting up the development environment on my new MacBook Air in June 2012, but it’s now become a step by step guide to setting up a full development stack on Mac OS X 10.8 Mountain Lion. I’ll aim to keep this post up to date with the latest dev environments/workflows as new tools are released in order to setup the leanest/efficient development machine possible.

The only prerequisite is an up-to-date install of OS X 10.8 Mountain Lion.


  1. Install Homebrew
  2. Install Git
  3. Install RVM
  4. Install Node
  5. Install MySQL
  6. Install CouchDB
  7. Configure Apache

Using Sublime Text 2 from the Command Line

Before we begin, i’d recommend enabling Sublime Text 2 Command Line support as, for some reason, this isn’t available out of the box.

To setup the command line functionality, type the following into the terminal.

sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

1. Homebrew:

1.1 Requirements:

Homebrew has two dependencies: XQuartz, and Xcode*.

It’s a common delusion that the entire 2.8gb install of Xcode is required, however, the only part of Xcode Homebrew requires, is the Command Line Tools.

XQuartz

As of the 26th July 2012, Apple has dropped X11 from its OS. OS X Mountain Lion does not come bundled with X11, and instead, Apple recommend developers install the more up to date open source XQuartz project, which it will continue to support.

Download XQuartz here then install it, log out, then log back in.

Command Line Tools

Homebrew requires Xcode to be installed in order to work, but Xcode is massively bloated for what is actually required by homebrew to build apps.

OSX-GCC-Installer by Kenneth Reitz was a way round this bloat, but lacked the 10.6/7 SDKs due to licensing restrictions. Apple, however, took interest in this project and were keen to ship something official.

Thus, Apple announced the Command Line Tools for Xcode. It’s a 171 MB download that includes all of the tools Homebrew should ever need. Plus, Homebrew officially supports this package.

Download the Command Line Tools for Xcode here and install it.

1.2 Installing Homebrew:

ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

The script installs Homebrew to /usr/local so that you don’t need sudo when you brew install.

It’s bad practice to install packages using sudo.


Packages can run arbitrary scripts, which makes sudoing a package manager command as safe as a chainsaw haircut. Sure, it’s fast and definitely going to cut through any obstacles, but you might actually want that obstacle to stay there. — Isaac Z. Schlueter


1.3 Changing ownership of usr/local:

This step isn’t required for brew, but since we’re on the subject of sudo, i recommend changing ownership of /usr/local from root to $user. This will benefit you when using node’s package manager, and will mean you won’t have to use sudo when installing into /usr/local:

sudo chown -R $USER /usr/local

There is a massive debate about chown-ing /usr/local, and i agree with Isaacs comments here.


2.0 Git:

brew install git

Next, to setup git. Run the following commands in the Terminal.

git config --global user.name "Your Full Name"
git config --global user.email "Your Email Address"

Make sure you use the same email address for Git, GitHub, and Heroku.


3.0 RVM:

RVM stands for Ruby Version Manager. It’s a command line tool which allows you to easily install, manage, and work with multiple ruby environments from interpreters to sets of gems. It also installs gems to your ~/ home directory, instead of using the global root of your machine.

RVM helps improve security

RVM helps ensure that all aspects of Ruby are completely contained within user space, strongly encouraging safer, non-root use. Use of RVM rubies thus provides a higher level of system security, and therefore reduces risk and cuts overall system downtime. Additionally, since all processes run at the user level, a compromised ruby process cannot compromise the entire system.

Say goodbye to sudo gem installs

Yep, that’s right! By using RVM, you won’t have to sudo to install gems on your machine.

3.1 Install the stable release version:

curl -L https://get.rvm.io | bash -s stable

Finish the installation by either running the following command, or closing all shell windows.

source /Users/USERNAME/.rvm/scripts/rvm

Replace username with your Mac username.

Before installing the latest version of ruby with rvm, we need to install libksba.

brew install libksba

3.2 Then install Ruby 1.9.3 with:

rvm install 1.9.3

Now install Sass and Compass.

gem install sass
gem install compass

4.0 Node:

According to the node docs, Brew installs of node are known to be buggy, the node package manager is not included in the install etiher, so the node devs themselves reccommend using the Macintosh package installer:

Download the Node Macintosh package installer

Note (8th Feb 2013):

Node devs recently made some changes so that Node no longer installs on OS X via homebrew if Xcode is not installed. We haven’t installed Node via homebrew, however this issue still affects us.

Next time you run npm install you may come across the following error:

xcode-select: Error: No Xcode is selected. Use xcode-select -switch <path-to-xcode>, or see the xcode-select manpage (man xcode-select) for further information.

To fix this, all you need to do is:

sudo xcode-select --switch /usr/bin

4.1 Yeoman

Yeoman is a robust and opinionated set of tools, libraries, and a workflow that can help developers quickly build beautiful, compelling web apps. Tools included are, Yo (a scaffolding tool), Grunt, Bower and more.

npm install -g yo grunt-cli bower

5.0 MySQL:

Sidenote - Debugging / $PATH issues

I actually had a few issues at this point with my new MacBook air and had to roll back the install of MySQL in order to fix a few issues…

I ran brew doctor to discover that /usr/bin was ahead of /usr/local/bin in my $PATH.

In order to get round this, i needed to edit /etc/paths to fix the order.

subl /etc/paths

This isn’t a file i recommend playing with often. However, i’ve mentioned it here, as it might come in handy to people needing to do a little DIY machine debugging.

Opening a new terminal window and running brew doctor again should confirm this.


5.1 Install

brew install mysql

You’re then requried to setup a few things. Run the following commands:

unset TMPDIR
mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp

Followed by the following: (you might also need to restart your machine to fix a conflict with Lion’s copy of MySQL.)

mysql.server start

Then use the alternative security script:

/usr/local/Cellar/mysql/5.5.25a/bin/mysql_secure_installation

Boom!


6.0 CouchDB:

CouchDB is a popular NoSQL database.

If you’re not planning on developing node apps with persistent storate, you can skip this step.

brew install couchdb

7.0 Configuring Apache and Virtual Hosts:

Type the following command in to edit the httpd.conf file in Sublime Text.

subl /etc/apache2/httpd.conf

7.1 Enable PHP5

Find line 111 and un-comment:

LoadModule php5_module libexec/apache2/libphp5.so

7.2 Enable Virtual Hosts

Find and uncomment the following line. It can be found around line 623.

Include /private/etc/apache2/extra/httpd-vhosts.conf

7.3 Creating a Virtual Hosts File

You then want to create a vhosts file, to store all your virtual hosts in. So enter the following commands in the Terminal.

touch /etc/apache2/other/vhosts.conf
subl /etc/apache2/other/vhosts.conf

And enter the following:

#
# Virtual Hosts
#
NameVirtualHost *:80

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for all requests that do not
# match a ServerName or ServerAlias in any <VirtualHost> block.
#
<VirtualHost *:80>
    DocumentRoot "/Users/USERNAME/Sites/SITENAME"
    ServerAlias dev.*.xip.io
    ServerName dev.foo
</VirtualHost>

Change username, sitename, dev, and dev.foo accordingly, and save the file.

7.4 Pointing Localhost to a Virtual Host Server Name

Now we need to point localhost to the server name, in order to enable the virtual host.

subl /etc/hosts

And enter the following on a new line and save:

#servername to match that in the vhosts.conf file
127.0.0.1   dev.foo

7.5 Restart Apache, and Lock ‘N’ Load!

Lastly in System Settings disable and re-enable Web Sharing in the Sharing panel.

Fire up your favourite web browser and enter the servername, you should be pointed to the doc root you entered in your vhosts file!

Neat, eh!

Now, there are plenty more options you can set for logs etc, but i’ll leave that out as not to over complicate this article.


8.0 Remaining packages:

There are a lot more packages that can be installed, and need less configuration than the ones mentioned above. I’ve put these in a .dotfile you can download here, and can be batch executed calling:

./.brew