Simon Davies Digital ltd

Using composer with MAMP’s version of PHP

Posted by & filed under MAMP, PHP.

After my laptop has been running really for a couple of weeks now, I have decided to order some new RAM and do a clean install of Lion. This has given me a great opportunity to configure my development environment in a cleaner and more organised way.

As a developer I do install a number of packages to play around with and then either decide its not for me or find there was a better way configuring them and then find myself in a bit of a muddle.

One tool I have been playing with recently is Composer which is a dependency manager, think Ruby Gems but for PHP. Which is a great little tool and I’m really happy to see the community getting behind it and moving forward. After just trying to install composer I ran into a little problem which I thought was worth sharing.

After running curl -s https://getcomposer.org/installer | php, I get the following error:

Some settings on your machine make Composer unable to work properly.
Make sure that you fix the issues listed below and run this script again:

The detect_unicode setting must be disabled.
Add the following to the end of your `php.ini`:
    detect_unicode = Off

Which looks pretty simple to fix but MAMP should have this setting disabled. So I ran which php and I’m told I’m running /usr/bin/php which is the version of PHP that comes preinstalled with OS X. So we just need to swap this over to the PHP that is installed with MAMP, which is located at /Applications/MAMP/bin/php/php5.4.4/bin (MAMP 2.1.1).

To do this I edit my .bash_profile or .profile if you are using a different shell and add the MAMP version of PHP to the PATH variable.

export PATH=/Applications/MAMP/bin/php/php5.4.4/bin:$PATH

It’s important to append the existing $PATH at the end otherwise the version of PHP in /usr/bin/php will be found and loaded first instead. Save the .bash_profile and reload your profile by typing source ~/.bash_profile and run which php, and if you’ve done everything right should say /Applications/MAMP/bin/php/php5.4.4/bin/php.

Note: Tom has pointed out in the comments that in MAMP 2.1.1 You are unable to run MAMP’s PHP from the command line so you will need to update MAMP first.

  • Jens

    Hello Simon,

    That´s what I was searching for. You saved my day! Thank you!

    • Simon Davies

      No problem, I’m glad it helped!

  • Chris

    Thanks man … was just looking for the same issue

  • Nice tip, solved my problem.

  • Awesome, need to keep this page handy.

  • Thank you sir. I’m sure you saved me a days worth of work.

  • heuristocrat

    I’ll add my thanks. Saved me some needless sweat and delay in getting my environment working here. You’re a superhero!

  • disqus_7amLCVEcPV

    excellent, thanks, save a pain in the neck, although when i try to shorten the composer run command & run: $ sudo mv composer.phar /usr/local/bin/composer i get the error:
    mv: rename composer.phar to /usr/local/bin/composer: No such file or directory. any ideas

    • simonda86

      Are you in the correct directory, I.e. does composer.phar exist in your working directory? As you will need to move to the correct directory and then execute the command.

    • Hossein

      same here, Also i did something like this but i got this error again :

      sudo mv /Users/hosseinmoradgholi/composer.phar /usr/local/bin/composer

      mv: rename /Users/hosseinmoradgholi/composer.phar to /usr/local/bin/composer: No such file or directory

      • Tom Bates

        I’m running Mountain Lion and had same problem. I changed /usr/local/bin/composer to /usr/bin/composer and it worked for me.

        • ChandrajeetMaurya

          Thanks, worked for me 🙂

    • Faolan Finn

      Same problem. I don’t have a bin folder in /usr/local/ . I looked around and the thing I found that worked for me was on this link http://community.education.ufl.edu/community/blog/view/147117/set-up-composer-php-dependency-manager-on-osx, which had you stick composer.phar in /usr/bin/ and edit the bash_profile

      • simonda86

        Glad you got it working, you shouldn’t really need to edit you bash_profile as composer contains a shebang which selects the PHP interpreter, but if it works don’t worry about it.

  • Natetronn

    Thanks, that worked for me as well! I worry though, I’m going to forget it if I ever decide to change to a new version of PHP. Any thoughts on future proofing this?

    • simon

      Hi Natetronn,

      It shouldn’t really matter if you update your version of PHP, unless you delete the old version. As composer can still keep running using the old executable.

      So nothing to worry about.

      Simon

    • simonda86

      Hi Natetronn,

      It shouldn’t really matter if you update your version of PHP, unless you delete the old version. As composer can still keep running using the old executable.

      So nothing to worry about.

      Simon

  • Simon, thanks for this tip. It’s top google search reult for this problem, so clearly I’m not the only one with 🙂 One thing to note is your MAMP PHP version, since you need that version in new path in .bash_profile

  • rmwebs

    Cheers Simon – great tip!

  • miyahira

    Working!! Greate tip man o/
    Thanks!

  • My paths are exactly the same as yours, but I’m having some trouble with this.

    Am I supposed to be editing the .bash_profile file in the root of my user directory? I have added “export PATH=/Applications/MAMP/bin/php/php5.4.4/bin:$PATH” but the where php command still returns /usr/bin/php, even after restarting Apache/MySQL.

    Any advice?

    • simonda86

      Have you made sure you are reloading the bash_profile? You can do this with command “reload sources”?

      • Bingo. You’re a champion!

      • It is worth mentioning the code to do this : ‘source ~/.bash_profile’.
        It took me 30 minutes to find that!

        • simonda86

          I’ve updated the post now to include this, cheers Roikles

  • Octavian86

    Thanks a million, MAMP is supposed to help you save time, unfortunately it fails.

  • Thanks a bunch!

  • Thanks for sharing this tips, It helps me a lot!

  • Thank you! 🙂

  • Trenton Scott

    Thanks! FYI — the latest version of MAMP ships with a new(er) version of PHP.

    The steps should be:

    1. Open Terminal, run “vim ~/.bash_profile”.

    2. Type “i” and then paste the following:

    export PATH=/Applications/MAMP/bin/php/php5.4.4/bin:$PATH

    3. Type “ESC”, “:wq”, and hit “Enter”.

    4. In Terminal, run “source ~/.bash_profile”.

    5. In Terminal, type “which php” and look for the updated string.

    6. Rerun “sudo curl -sS https://getcomposer.org/installer | php” and you should be good to go.

    • Andrew Kenward

      Thanks for step 4.

    • fiddlegrrl

      works great with MAMP 2.2. Thanks!

    • Andre Harry

      THANK YOUU!!!

  • mfebber

    I have the most recent version of MAMP and this worked perfectly for me – Composer installed – thank you!

  • Tom Bates

    Quick point – the above doesn’t work with MAMP 2.1.1 due to a MAMP bug, it won’t let you use MAMP PHP on the command line. I finally figured this out after reading a thread on it, and downloaded 2.1.4 and all works fine – wasted a couple of hours along the way though! Might be worth adding a note about this to save others the frustration 🙂

    • simonda86

      Thanks Tom, I’ve added a note to the post about this. 🙂

  • Stian Karlsen

    Thanks man!

  • poolhalljunkie

    thank you, this worked for me 🙂

  • Alex Bachuk

    Thanks, it worked for me!

  • Jared Eitnier

    Thanks for the nice tip, well explained!

  • Daryl Johnson

    Great post. Helps greatly with getting composer and MAMP working

  • I got it to install using the PHP that came with the Mac. Try running
    curl -sS https://getcomposer.org/installer | /usr/bin/php

Contact

Want to hear more about Simon Davies Digital and what we can do for your business or simply want to say hello, then get in touch.

+44 7846 400933
hello@simon-davies.name