Friday, October 3, 2014

Installing Composer on OS X Mavericks with XAMPP for Mac installed

Composer is a popular tool for the dependency management in PHP. It lets you declare dependent libraries for each particular project.

Let's have a quick look on what have installed so far on the development machine:

OS X Mavericks
XAMPP for Mac installed and configured properly (A running instance)
Hombrew installed and configured (Try brew doctor to tackle any problem before you start)

Steps as follows:

Assuming default installation path of XAMPP for Mac package installed as usual, please change into the directory where PHP executable resides:
$ cd /Applications/XAMPP/bin/


Check the version of PHP:

$ php --version
PHP 5.4.30 (cli) (built: Jul 29 2014 23:43:29) 
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2014 Zend Technologies

Now we know PHP version is 5.4. Let's remember this for use in next couple of steps.

If you try to install Composer with brew command now, you may end up with an error like this:
composer: Missing PHP53, PHP54, PHP55 or PHP56 from homebrew-php. Please install one of them before continuing
Error: An unsatisfied requirement failed this build.

The likely cause is that we are using PHP within XAMPP package whereas brew cannot detect its presence without installing its own PHP engine.
Supposing XAMPP package is installed properly, we can simply add the path /Applications/XAMPP/bin/ to $PATH environment variable to the end of the file ~/.bash_profile . If it doesn't work then another to get around this would be installing a new version of PHP package using brew command.

[OPTIONAL] So which version of brew's PHP engine to install? It's better match the version of PHP within XAMPP, i.e., 5.5.

[OPTIONAL] Let's install PHP54 package by using brew command:
$ brew install php55

Once finished, start installing Composer with brew commands like these:
$brew update;brew tap homebrew/dupes;brew tap homebrew/php;brew install composer

It includes the actions of updating and tapping the right repository for downloading Composer's source.

When brew's package for Composer is installed successfully, we can carry out next step to create composer.phar file within XAMPP's bin directory.

$ sudo php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"

We must execute the command as root privilege to avoid permission denied error.

When done, we can try composer command within /Applications/XAMPP/bin/ directory.
$ php composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 1e4229e22aefe50582734964d027fc1bfec16b1d 2014-10-02 11:34:17

Usage:
  [options] command [arguments]...


Then it should be ready for us to checkout new dependent packages within a new or existing PHP project directory.




1 comment: