Setting Up a Local Dev Environment with Homebrew

Warning! Techy Stuff Ahead!

We want to warn our blog followers first, this is going to be quite a bit more techy so if you are not interested in those kind of things, you'll want to avoid this post, otherwise, read ahead!

What is this?

For those doing development, it is generally recommended to do development locally using code versioned in a version control system (we tend to use git and svn). This allows you to perform tasks locally before pushing it back to the rest of the developers in your group or to actual server based installations such as a staging or production environment.

Here at the Nest, we generally prefer the Mac OS X platform for our machines. OS X does ship with some of the components needed for development but not necessarily versions we want (such as php 5.3.15 vs 5.4) and we find we need a different setup. Previously we've used MAMP or XAMPP which pre packages many of the packages needed but we still ran into having issues where we didn't necessarily have the right versions. In fact, XAMPP still packages PHP 5.3.1 which is more behind than what Apple packages with OS X. This got us thinking, do we manually recompile everything or what should we do. Our search led us to Homebrew.

Homebrew is described as "the missing package manager for OS X" and it does live up to that. Those familiar with Linux based systems are familiar with using apt or yum to install packages using a syntax similar to yum install mysql. OS X doesn't support this natively. Once homebrew joins your party though it makes this very easy. This has certainly been around for some time but as we just recently found it, we wanted to share how we set it up so it can hopefully help others as well.

Note: It has been mentioned that we also look into Vagrant which also would help with setting up a local development environment but wanted to share how we setup one with Homebrew.

How do we get this setup?

The first step will be to install Homebrew. On all our systems we have them fully up to date with the latest version of OS X and also have Xcode already installed with the command line tools as well which Homebrew needs per their requirements page. To install homebrew, open up your handy dandy terminal and page this in:
ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)"

Once run that you'll see a screen similar to this and upon hitting enter be prompted for your admin password.

Install Homebrew

If you forgot to install the Command Line Tools for Xcode first, you'll receive a message similar to below. If this happens, install the command line tools for xcode and then run brew doctor first.

CLI Tools Missing

Now that we have Homebrew installed, we can start our install process. First we need to add the "tap" the brews for our various installs. We'll run the following two lines to set this up for us to install common packages as well as php brews.
brew tap homebrew/dupes
brew tap josegonzalez/homebrew-php
brew tap homebrew/versions
brew update

Tapping

Many of the production servers we're deploying too are still on MySQL 5.1 so we choose to install that version by running brew install mysql51. After that completes, it will output a mesage reminding you to set up the database running following:

  • unset TMPDIR
  • mysql_install_db
  • ln -sfv /usr/local/opt/mysql51/*.plist ~/Library/LaunchAgents
  • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql51.plist

Install MySQL

At this point, you have the db and are ready to run. It does mention that you should set a password for the root mysql user. You can do this with commands output or we prefer to run mysql_securit_installation using the path output in the screen (in this case /usr/local/Cellar/mysql51/5.1.69/bin/mysql_secure_installation)

Complete MySQL

Now that MySQL is installed it's time to setup PHP. We frequently need PHP 5.3 and 5.4 so we actually install both of these packages and switch them in our apache config as needed. to do our install, we run the following:

  • brew install php53 --with-mysql --with-imap --with-suhosin --with-home-brew-openssl --with-libmysql php53-xdebug php53-mcrypt
  • brew install php54 --with-mysql --with-imap --with-suhosin --with-home-brew-openssl --with-libmysql php54-xdebug php54-mcrypt

When we've done this, we tend to need to overwrite some formulas that seem to get installed with whichever we install first. We see this when it says "Could not link php54". To fix this, we need to get brew link php54 to execute correctly. We use the command brew link php54 --overwrite to force this. Once done, then we finish the php54 install by running brew install php54-xdebug php54-mcrypt to get those extensions loaded.

Link PHP

At this point, we now have all the brews installed that we need. Now we need to get our web server (apache) setup right. Apache is included with OS X but they seemed to remove the "Web Sharing" setting in preferences so you have to play more in terminal for this. We're going to edit our httpd.conf by typing sudo nano /etc/apache2/httpd.conf and then typing in our password when prompted.

Here are the changes we're looking for:

  • Find the bottom of LoadModule section and ensure php5_module lib/exec/apache2/libphp5.so is commented out (has # as first character on line) and then add the 2 new ones ensuring only one is active:
    • LoadModule php5_module /usr/local/opt/php53/libexec/apache2/libphp5.so
    • #LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so
  • We like to run apache as our local user to ensure we minimize permission issues so we change User and Group to be your local user
  • Generally change the default settings under <Directory /> to set AllowOverride All and comment out the Order deny,all and Deny from all
  • We enable virtual hosts conf file by uncommenting Include /private/etc/apache2/extra/httpd-vhosts.conf
  • We setup/enable our virtual hosts by editing the file /private/etc/apache2/extra/httpd-vhosts.conf

We then edit our php.ini files in /usr/local/etc/php/5.3/php.ini and /usr/local/etc/php/5.4/php.ini by using sudo nano PATH where path is the file. Here are settings we normally change:

  • Maybe turn short_open_tag = On if the code we're using requires
  • Change max_execution_time to something longer than 30 seconds if doing larger processing
  • Change memory_limit to something higher if doing larger processing
  • Ensure error_reporting is set to E_ALL
  • Ensure display_errors = On
  • Add date.timezone = America/Chicago (or your timezone)

Now that our apache and php configs are setup, we can then start/restart apache. Do this by running sudo apachectl start if not started or sudo apachectl graceful to reload if already running.

If you have additional configuration to do on mysql, you can also do that by editing/creating /etc/my.cnf. We'll sometimes do this if we want MySQL to run as the local user, enable InnoDB, or change other settings. We normally shutdown MySQL when we do this and then start once done using the following:

  • launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql51.plist
  • launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.mysql51.plist

Once all this is done, we're fully setup! We can switch from php 5.3 to 5.4 if wanted too by just changing which line is commented in the httpd.conf file and then running sudo apachectl graceful.

Restart Apache