Create a gist now

Instantly share code, notes, and snippets.

Instructions on how to change preinstalled Mac OS X PHP to MAMP's PHP Installation and then install Composer Package Management

Change default Mac OS X PHP to MAMP's PHP Installation and Install Composer Package Management


Instructions to Change PHP Installation


First, Lets find out what version of PHP we're running (To find out if it's the default version).

To do that, Within the terminal, Fire this command:

which php

This should output the path to the default PHP install which comes preinstalled by Mac OS X, by default it has to be (Assuming you've not changed it before):

/usr/bin/php

Now, 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.10/bin (MAMP 2.1.3)

To do this, We need to edit the .bash_profile and add the MAMP version of PHP to the PATH variable.

Follow these simple steps:


  1. Within the Terminal, run vim ~/.bash_profile

  2. Type i and then paste the following at the top of the file:

    export PATH=/Applications/MAMP/bin/php/php5.4.10/bin:$PATH
    
  3. Hit ESC, Type :wq, and hit Enter

  4. In Terminal, run source ~/.bash_profile

  5. In Terminal, type in which php again and look for the updated string. If everything was successful, It should output the new path to MAMP PHP install.

  6. In case it doesn't output the correct path, try closing the terminal window (exit fully) and open again, it should apply the changes (Restart in short).

Install Composer Package Management


Now you can fire the command to install the composer globally (So you can access it from anywhere):

$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

You can verify your installation worked by typing the following command within the Terminal:

composer

It'll show you the current version and a list of commands you can use if the installation was successful.

@thejwalker

These are good instructions but for some reason my path is not updating. I'm comfortable with command line and vim but after running source ~/.bash_profile I still get /usr/bin/php

My MAMP is running PHP 5.4.3 and I'm on OSX 10.7.5

Thanks for posting.

@xulfiqarameen

Still getting the same /usr/bin/php

@tvalentius

thanks for the instruction, help's me a lot setting up laravel on mac :)

@zeratool
zeratool commented May 8, 2014

@xulfiqarameen edit your ~/.bash_profile replace it with your homebrew php install.

@zeratool
zeratool commented May 8, 2014

@JonWalkerTN if you;re saying under apache via browser, you must edit "/etc/apache2/httpd.conf" and look for the line "LoadModule php5_module" and replace it with your php homebrew install.

@zhiachong

@JonWalkerTN are you using oh-my-zsh by any chance? That's what I'm using, and instead of editing ~/.bash_profile, edit your ~/.zshrc instead, and add the exact same lines. Then run source ~/.zshrc. That solved the issue for me.

@JonasLandstrom

Thanks a lot, 2 hours of pain solved!

@dysan1376

It worked!! thanks!

@phpguru
phpguru commented Dec 15, 2014

Yosemite comes with Apache 2.4 & PHP 5.5 preinstalled. You don't need MAMP, only MySQL or MariaDB.

Regardless, you can use PHP 5.5, PHP 5.4 or PHP 5.3 as needed. Try the following...

sudo mv /usr/local/bin/php /usr/local/bin/php55
sudo mv /usr/bin/php /usr/bin/php55

Install PHP 5.4

brew install php54 php54-mcrypt php54-mysql

Install PHP 5.3

brew unlink php54
brew install php53 php53-mcrypt php53-mysql

Switch to PHP 5.4

brew unlink php53
brew link php54

Switch to PHP 5.5

brew unlink php54
brew unlink php53
sudo ln -s /usr/local/bin/php55 /usr/local/bin/php
sudo ln -s /usr/bin/php55 /usr/bin/php
@digitalnerve

Many thanks - very helpful

@ghost
ghost commented Feb 28, 2015

Solved my problems :). Trying to install Laravel5 on a MAMP Pro server, and had all sorts of problems, especially (missing mcrypt).
Using this solution so it uses MAMP's php, everything now works.

@kirensiva

After following the steps which, vim command not working. Please help me to solve this

@irazasyed
Owner

These are some pretty old instructions. I've already moved to Vagrant setup for the dev environment. You guys need to explore Vagrant really! Much better there.

You can use this awesome vagrant box called Homestead. Not limited to Laravel, can be used for anything.

@Dat-TSC
Dat-TSC commented Jun 23, 2015

You have to close then re-open the terminal to see the effect. Cheers.

@pandyatama17

oh man i love you so much

@cheldernunes

Tank you !! very helpful

@sethdaniel

Thank you so much! Nothing I was doing was working until this. I was about ready to punch monkeys in the face. This write up saved a bunch of monkeys from being punched.

@cord
cord commented Feb 9, 2016

for me this was necessary to set the php version:

vim ~/.profile

PATH=/usr/local/php5/bin:$PATH

source ~/.profile

then php -v shows the correct php is used

@verapi85

@cord thank you for your comment, it works for me as well.

@rotexhawk

This switches it for the cli but apache might still be using the old php.

@kslusarczyk89
kslusarczyk89 commented May 18, 2016 edited

Im using oh-my-zsh and according to @zhiachong advice i did the following instructions with ~/.zshr but it didn't work. After typing which php in iterm i still get /usr/bin/php. Actually when I edit bash_profile it works fine but after closing console I have to constantly type those instructions. Any advise?

@rajamohammed

@cord thank you it helped me.

@devansh1110

Wow, Thanks. It took forever to find these instructions. Everyone said to update ~/.profile.. and it never worked. This guide was truly amazing!

Thanks A lot

@samben00

zhiachong Thanks for the answer.

I was using oh-my-zsh . so I had to edit ~/.zshrc

@juliafealves

Thank you! :)

@stevenhillsr

I have created a script that lets you select the version of php you would like to run and creates a sym-link to that version. You can find it here and I have a blog about how to put it to use which you can find here if anyone is interested.

@LTroya
LTroya commented Sep 9, 2016

It works like a charm!

Thanks.

@ghost
ghost commented Sep 20, 2016

thanks alot !

@AustinPaquette

@irazasyed - thank you very much, you helped me in ways no other resource online seemed to be able to. Appreciate you posting this very very much.

@christophe-44-27

@irazasyed : I love you !

@stefanjagger

Thank you!

@hengsoheak

Thanks it is solved my problem in mac too

@Sifoon
Sifoon commented Nov 22, 2016

@irazasyed : Thanks very much ! clean & clear and the most important it works wellll.

@jordanbrauer

Great stuff!! Thanks a lot man. It's hard to find reliable AMP setups for Mac OS X.
I myself am interested in eventually uninstalling MAMP in favour of updating the preinstalled Apache and PHP along with an install of MySQL, but this was an awesome work around for the time being! 👍

@justenpeters

Can someone share the commands to set it back to default?

@Tuan-T-Nguyen

Thank you so much

@Narayanareddy96

i have solved with this
vim ~/.bash_profile
added

export XAMPP_HOME=/Applications/XAMPP
export PATH=${XAMPP_HOME}/bin:${PATH}
export PATH

saved !

working perfect :-)

@doub1ejack
doub1ejack commented Feb 20, 2017 edited

If you're using macport you can just do this:
$ sudo port select php php56

Replace php56 with whatever version you want to switch to. No need to worry about the OS-installed version.

@n4nidhinchandran

Great ! Thanks a lot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment