* By “The Right Way”, I mean following the guidance and practices at the PHP: the Right Way website. I make no claims this is the “best” way 🙂

Works n my machine badgeMac OS X is a pretty good web developer OS. It comes as standard with PHP, Ruby and Apache all out of the box, and the underlying UNIX system makes it easy to add in other languages and components to suit your needs. On top of that, some of my favourite development tools are on the Mac, so unless I’m writing .NET code, nearly all my development is on an (ageing) Mac Mini.

Now, while all that stuff comes as standard on OS X, lately it seems Apple has made it harder to get to. The versions shipped with OS X also tend to be a little behind the latest releases. As a result, most Devs I know use something like MAMP to make the server-side of their environment as easy as running an app. Personally, while I think MAMP works, and is a good time-saver (and I’ve been using it for the last year or so), but I like to get into the nitty-gritty of the system and get things running “native”. So last night I fired up the terminal and got PHP set up on my Mac with the latest version, and following the Right Way Guidelines. As a result I have PHP 5.4, Composer, the PHP Coding Standards Fixer, and MySQL all setup quite slickly (i.e. to my preferences).

The whole process was pretty easy, but does involve the command line. If this makes you uncomfortable, then it might be best to skip the rest of this post.

This all worked on my Mac, but I make no guarantees about it working on yours, and I’m not responsible if you break something.

If you find any glaring problems with this guide then leave a comment/get in touch, and I’ll make any required edits.

Step 1: Setup Your PATH

Edit the hidden .bash_profile file in your home directory. If you use Sublime Text 2 you can use the following command:

subl ~/.bash_profile

TextMate has a similar mate command, or you can use vi(m)/nano/emacs/whatever.

It’s possible you already have a line defining your PATH variable. It’ll look something like export PATH=<something>. I’ve found it most useful to change the PATH so /usr/local/bin is at the start, making sure anything you install there is used over the system defaults in /bin. Add this as a line below your existing PATH definition (or just add it in, if you don’t have an existing line):

export PATH=/usr/local/bin:${PATH}

Step 2: Install Brew

Strictly speaking, Brew (aka Homebrew) isn’t required, but I used it to install MySQL later, and it does make it stupid easy to install stuff into OS X. I think you should install it. The best instructions are found on the Homebrew home page, so go have a read there. There are a few pre-requisites, but nothing too difficult.

Step 3: Install PHP-OSX

Now we’re beginning to get somewhere! PHP-OSX is the latest versions of PHP compiled for OSX by Liip. Installation is a real doddle, from the command line:

curl -s http://php-osx.liip.ch/install.sh | bash -s 5.4

Follow the prompts given, including entering your password. After a few moments everything will have installed. For convenience I created a symbolic link to the newly installed PHP binary in /usr/local/bin:

ln -s /usr/local/php5/bin/php /usr/local/bin/php

Step 4: Install Composer

Now we have PHP installed, it’s time to look at the nice-to-haves, like a good package/dependency manager. Composer is relatively new on the block, and allows others to download your code and automatically grab any dependencies by running a simple command.

You can install Composer in your project, or you can install it globally. I prefer globally. As with PHP, installation is simple, from the command line:

curl -s http://getcomposer.org/composer.phar -o /usr/local/bin/composer
chmod +x /usr/local/bin/composer

Step 5: Install PHP Coding Standards Fixer

Another nice-to-have, this little tool will try to find and fix parts of your code where it does not conform to one of the PHP Coding Style Guides. Installation is almost identical to Composer:

curl http://cs.sensiolabs.org/get/php-cs-fixer.phar -o /usr/local/bin/php-cs-fixer
chmod +x /usr/local/bin/php-cs-fixer

Step 6: Install MySQL

If you installed Brew in step 2, then you’re good to go with this little command:

brew install mysql

It’ll take a few minutes, but you shouldn’t need to intervene at all. Once done you will need to run two more command to setup the MySQL tables:

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

If you didn’t install Brew, then you will need to install MySQL through some other means, such as packages on the MySQL website. I can’t help you with that, I’m afraid.

For managing MySQL, I use the excellent Sequel Pro, which is a successor to the venerable CocoaSQL.

As a next step you should look into changing the root password of your MySQL setup. This is a local dev environment, and likely only used locally by yourself, but it’s the proper thing to do.

Errata

  • Pear doesn’t seem to work, which is slightly annoying, but (to me) no real biggie. I didn’t test this with the built-in version of PHP, so I don’t know whether it worked beforehand. I’ll post an update once I figure it out.
  • I’d like to make bash script smart enough to stop MySQL when the PHP web server stops, but my early attempts haven’t managed to get this working (most likely due to the Ctrl-C used to stop the web server also stopping the script).
  • Throughout this process we’re running scripts directly from the web. This is pretty risky behaviour, especially with unknown/untrusted sources. You should always take a look at the raw script before running it, so you don’t get hit by something malicious.

Today I fired up TextMate to do my first bit of serious PHP coding since my stroke. I’ve been almost entirely XHTML/CSS since getting out of hospital last August, with a little light coding (ASP mostly) since then.

Probably the closest I’ve got to writing any real PHP in 8 months has been learning the basics of WordPress themes from Blog Design Solutions… To be honest, I’ve not had the same drive or determination to “Just Code It” as I once did.

I’ve been reading 37 Signals’ excellent book, Getting Real today. I’m about three quarters of the way through. I doubt I’ve ever said this about a “tech” book before, but it’s a real “page-turner”; Getting Real pulls you in and is real hard to put down once you get started. All the praise you may have heard about this book is justly deserved—it’s essential reading for developers… hell, it should probably be essential reading for anyone who has to work on just about any type of product or in a team.

While I’ve been reading Getting Real, I’ve been feeling like I want to write code again; I want to write something simple, elegant and real. I want to stop thinking about some of the ideas I’ve had over the last few month – no years – and actually do something. So I set-up a development site and database for one such idea, opened TextMate and created a new project.

It hit me like a slap in the face; I’ve forgotten how to do this. It’s like I’m back on square one… like someone sucked most of my programming ability out of my head. I can remember lots of stuff about various PHP functions, syntax and a million myriad details, but actually doing anything with any of it is another matter. I started thinking about the initial, basic class/data structure I would need and it was like the lights were on but nobody was home.

Looking on the bright-side: if I do have to relearn myslef this stuff, it means I’ll be able to do it with a clean slate and Be Real from the very outset…

Something I like to do for each of my Macs, is keep a backed-up folder containing all the “essential” apps for both machines (along with all registration codes for easy reinstallation). I was updating my iBook copy this evening and thought I’d share. Some of these are generic apps that I’d use on either machine, while others are iBook specific.

Side-note – My iBook is pretty exclusively a web development machine.

Textmate. Textmate describes itself as “the missing editor for OS X”. I’m not sure what that’s meant to mean but hey, I’ll take their word for it! Textmate was the first Mac App I ever bought and is by far the best file editor I have ever used. Textmate handles just about any text file format you can throw at it, and above all else, lets you work without getting in your way. Seriously worth trying.

Ecto. Ecto is the best blogging client in the world. Especially on the Mac. I used to be a fan of MarsEdit (I own licenses for both), but Ecto won out in the end for its wider range of features. If you’re in any way serious about this blogging malarkey, I’d recommend you give it a try. Even the Windows version is recommended (although, naturally, it’s nowhere near as good as the Mac edition!)

Transmit. If Textmate was the first Mac App I paid for, then Panic Software’s Transmit was the second. It’s a S/FTP client with more features than you’re ever likely to need, but wrapped in a highly polished and intuitive interface. Transmit makes working with files on remote servers as effortless as working with local files in Finder. The synchronisation features alone are worth the price of the license.

Coconut Battery. More of a useful widget than an application, Coconut Battery can tell you near enough anything you need to know about the status and health of your battery. Ideal for checking if you want to know if you’ll be needing to buy a replacement any time soon.

Colloquy. Sometimes, the best – or only – way to get help on open-source applications/scripts, etc, is to jump into an IRC room. Colloquy is the best OS X IRC client I’ve found. As a rule, I don’t like chat or IM applications (they tend to be little more than a distraction), but Colloquy has helped me get access to the right answers on more than one occasion.

That’s really it for my iBook’s list of essential apps; I try to keep laptops “light”, hence the short list. My iMac list is probably huge in comparison. Besides, my other needs are either met by the apps bundled with OS X – Safari for web browsing, Terminal.app for command-line activity (remote sessions, etc.), iTunes and what-not, or by web applications –PHPMyAdmin for MySQL administration, Roundcube for accessing my IMAP email anywhere. Are there any applications you can’t live without on your Apple laptop? Please share!