Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

thejwalker commented Dec 13, 2013

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

This comment has been minimized.

xulfiqarameen commented May 3, 2014

Still getting the same /usr/bin/php

@tvalentius

This comment has been minimized.

tvalentius commented May 7, 2014

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

@zeratool

This comment has been minimized.

zeratool commented May 8, 2014

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

@zeratool

This comment has been minimized.

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

This comment has been minimized.

zhiachong commented Jun 9, 2014

@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

This comment has been minimized.

JonasLandstrom commented Aug 30, 2014

Thanks a lot, 2 hours of pain solved!

@dysan1376

This comment has been minimized.

dysan1376 commented Sep 19, 2014

It worked!! thanks!

@phpguru

This comment has been minimized.

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
@tobyt99

This comment has been minimized.

tobyt99 commented Jan 11, 2015

Many thanks - very helpful

@ghost

This comment has been minimized.

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

This comment has been minimized.

kirensiva commented Apr 21, 2015

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

@irazasyed

This comment has been minimized.

Owner

irazasyed commented Apr 28, 2015

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

This comment has been minimized.

Dat-TSC commented Jun 23, 2015

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

@pandyatama17

This comment has been minimized.

pandyatama17 commented Jul 1, 2015

oh man i love you so much

@cheldernunes

This comment has been minimized.

cheldernunes commented Sep 17, 2015

Tank you !! very helpful

@sethdaniel

This comment has been minimized.

sethdaniel commented Oct 27, 2015

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

This comment has been minimized.

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

This comment has been minimized.

verapi85 commented Feb 16, 2016

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

@rotexhawk

This comment has been minimized.

rotexhawk commented Mar 18, 2016

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

@kslusarczyk89

This comment has been minimized.

kslusarczyk89 commented May 18, 2016

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

This comment has been minimized.

rajamohammed commented Jul 20, 2016

@cord thank you it helped me.

@devansh1110

This comment has been minimized.

devansh1110 commented Jul 27, 2016

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

@3issamben

This comment has been minimized.

3issamben commented Aug 24, 2016

zhiachong Thanks for the answer.

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

@juliafealves

This comment has been minimized.

juliafealves commented Sep 1, 2016

Thank you! :)

@stevenhillsr

This comment has been minimized.

stevenhillsr commented Sep 8, 2016

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

This comment has been minimized.

LTroya commented Sep 9, 2016

It works like a charm!

Thanks.

@ghost

This comment has been minimized.

ghost commented Sep 20, 2016

thanks alot !

@pqt

This comment has been minimized.

pqt commented Sep 23, 2016

@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

This comment has been minimized.

christophe-44-27 commented Oct 11, 2016

@irazasyed : I love you !

@stefanjagger

This comment has been minimized.

stefanjagger commented Oct 22, 2016

Thank you!

@hengsoheak

This comment has been minimized.

hengsoheak commented Oct 26, 2016

Thanks it is solved my problem in mac too

@Sifoon

This comment has been minimized.

Sifoon commented Nov 22, 2016

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

@jordanbrauer

This comment has been minimized.

jordanbrauer commented Nov 30, 2016

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

This comment has been minimized.

justenpeters commented Jan 11, 2017

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

@Tuan-T-Nguyen

This comment has been minimized.

Tuan-T-Nguyen commented Jan 14, 2017

Thank you so much

@Narayanareddy96

This comment has been minimized.

Narayanareddy96 commented Feb 4, 2017

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

This comment has been minimized.

doub1ejack commented Feb 20, 2017

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

This comment has been minimized.

n4nidhinchandran commented Feb 24, 2017

Great ! Thanks a lot

@rryybbaa

This comment has been minimized.

rryybbaa commented Mar 4, 2017

This works for me. Thanks

@eriktecse

This comment has been minimized.

eriktecse commented Mar 31, 2017

Thanks, @zhiachong This works for me.
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.

@rathorevaibhav

This comment has been minimized.

rathorevaibhav commented Apr 27, 2017

This worked for me. Thanks. I just changed the PHP version that I was using.

@davidLpt

This comment has been minimized.

davidLpt commented Apr 28, 2017

I follow the procedure and the change of path is ok. But '$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer' give me an curl: (56) SSLRead() return error -9806....... anybody had and solved the same issue?

UPDATE: I did a manual install

@dinkarprajapati

This comment has been minimized.

dinkarprajapati commented Apr 29, 2017

To change the php path in mac Sierra, i did the steps mentioned but still when i run

which php

I still get

/usr/local/bin/php

I tried restarting terminal and even mac but it is still the same path. I have followed every steps

  • change bash_profile

export PATH="/Applications/XAMPP/xamppfiles/bin/php-7.0.15:$PATH"

  • did source ~/.bash_profile

Any idea why the path is so stubborn

Solution:
For XAMPP, path I used was wrong. It should be

export PATH="/Applications/XAMPP/bin:$PATH"

@arkhipovandrei

This comment has been minimized.

arkhipovandrei commented May 28, 2017

thx

@Memedawn

This comment has been minimized.

Memedawn commented May 28, 2017

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

The command to install the composer globally doesn't work with me. I'm using Mac OS X system with PHP 5.6.30v, but the root login is still showing 5.4.45, which is frustrating. My PHP is in /usr/bin/php.

The Composer is downloaded and installed, but the above command does not work. I get an error message:

'The defined install dir (/usr/local/bin) does not exist.'

When I did a locate for composer, it was found in 4 locations. It was successfully installed:

Composer (version 1.4.2) successfully installed to: /private/var/root/composer.phar
Use it: php composer.phar

So, does the composer being in multiple locations mean that it is global, or do I still need to specify and why am I still getting two versions of PHP displaying?

@dazjavier

This comment has been minimized.

dazjavier commented Jun 8, 2017

Thanks, this works perfectly!

@rambhairao

This comment has been minimized.

rambhairao commented Jun 13, 2017

it's not working .
giving same file path

@arianacosta

This comment has been minimized.

arianacosta commented Jun 14, 2017

It might not be as obvious, but the export has to be of the bin folder, be carefull not to do .../bin/php as php is the executable. This will not throw any errors when loading the bash profile, but it will not load the PHP binaries as needed.

So, in my case with MAMP PRO 4.1.1 and MacOS Sierra, I put this in the ~/.bash_profile and it is working fine:

export PATH="/Applications/MAMP/bin/php/php7.1.1/bin:$PATH"

@campside

This comment has been minimized.

campside commented Jul 9, 2017

worked perfect form me

@tutkun

This comment has been minimized.

tutkun commented Jul 19, 2017

Thank you!

@renegaders

This comment has been minimized.

renegaders commented Aug 3, 2017

Hi there all.
Perhaps it's really just my own stupidity,
but I searched the web now for a day, tried dozens of options but I am still stuck.

I use mac OSX 10.7.4 (yes I know, but I have too much on it to move to a complete new version) with MAMP 3.5.1 installed.
I created .bash_profile (it was automatically saved to my administrator account root folder)
Command: which php - still tells /usr/bin/php
Command: source ~/.bash_profile tells export: Command not found

That is weird. Do I need to move .bash_profile to another location/folder? What should be (in a normal case) the path to .bash_profile?

I even tried to use other bash files like ~/.bash_aliases or ~/.bashrc Both files throw a Command not found

Yet, I once tried this option in my .bash_profile:


> PHP_VERSION=`ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
> export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH

and this, after using source ~/.bash_profile, gave me:

PHP_VERSION=php5.6.10: Command not found.
PHP_VERSION: Undefined variable.

Interesting, because php5.6.10 is used by MAMP, in contrary to 5.3.2 (mac's own apache.

So, what am I missing here? ???

Thank you for your patience reading my misery.

@renegaders

This comment has been minimized.

renegaders commented Aug 4, 2017

Update:

> [000-000-0-0:] MEE% source ~/.bash_profile
> export: Command not found.
> [000-000-0-0:] MEE% source .bash_profile
> source: Command not found.
> [000-000-0-0:~] MEE% source Users/MEE/.bash_profile
> Users/MEE/.bash_profile: No such file or directory.
> [000-000-0-0:~] MEE% /MEE/.bash_profile
> /MEE/.bash_profile: Command not found.
> [000-000-0-0:~ MEE% source /MEE/.bash_profile
> /MEE/.bash_profile: No such file or directory.
> [000-000-0-0:~] MEE% source MEE/.bash_profile
> MEE/.bash_profile: No such file or directory.
> [000-000-0-0:~] MEE% source /Users/MEE/.bash_profile
> export: Command not found.
> [000-000-0-0:~] MEE% source Users/MEE/.bash_profile
> Users/MEE/.bash_profile: No such file or directory.
> [000-000-0-0:~] MEE% 

But .bash_profile IS there, at /Users/MEE/.bash_profile !
[000-000-0-0:~] MEE% ls -a | grep .bash_profile
.bash_profile


> Did a little more trials:
> [000-000-0-0:~] MEE% source . /.bash_profile

Now Look At This:!
> [000-000-0-0:~] MEE% ./.bash_profile
> ./.bash_profile: Permission denied.
!!!

> [000-000-0-0:~] MEE% source ./.bash_profile
> export: Command not found.
> [000-000-0-0:~] MEE% which php
> /usr/bin/php
> [000-000-0-0:~] MEE% 
> 

While:

[000-000-0-0:~] MEE% open ~/.bash_profile
[000-000-0-0:~] MEE% 

Opens up the file nicely in my text editor and showing the path to php:
export PATH="/Applications/MAMP/bin/php/php5.6.10/bin:$PATH"

What's Going On (is a song, too, :))
???

@jeffersongouveia

This comment has been minimized.

jeffersongouveia commented Aug 15, 2017

Many thanks man :)

@fernandopedro

This comment has been minimized.

fernandopedro commented Aug 18, 2017

Thanks a lot @Narayanareddy96, your solution work fine here

@jrran90

This comment has been minimized.

jrran90 commented Aug 20, 2017

@cord many thanks ;)

@tinhochu

This comment has been minimized.

tinhochu commented Aug 25, 2017

Thanks!

@alongopenroad

This comment has been minimized.

alongopenroad commented Oct 23, 2017

Awesome! Thx!

@sdelrue

This comment has been minimized.

sdelrue commented Nov 6, 2017

I had a similar issue, turns out the order in which I was setting the PATH var was the problem:
ok:
export PATH="/usr/local/php5/bin:/usr/bin:$PATH"

not ok:
export PATH="/usr/bin:/usr/local/php5/bin:$PATH"
I had a php bin in both folders...

So make sure that the folder with the correct php version is placed first in the PATH variable

@jedlovescpe2

This comment has been minimized.

jedlovescpe2 commented Nov 12, 2017

It works! Thanks.

Just the cURL command only gives the error that /bin isn't writable.

But not a problem as I already installed the composer just before fixing my PHP version. Just like to know what's the error of the command.
$ curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

All settings correct for using Composer
The installation directory "/usr/local/bin" is not writable

@catalincislariu

This comment has been minimized.

catalincislariu commented Nov 19, 2017

thanks

@mapplics

This comment has been minimized.

mapplics commented Jan 8, 2018

thanks man! works like a charm!

@vladanyes

This comment has been minimized.

vladanyes commented Jan 30, 2018

Thanks bro!!

@preciousgeorge

This comment has been minimized.

preciousgeorge commented Mar 13, 2018

@Narayanareddy96 your solution was the only one that worked. Thanks man.

@madhurikulkarni

This comment has been minimized.

madhurikulkarni commented Mar 13, 2018

@irazasyed.. That was the right and a very simple solution. Solved a lot of errors including the intl missing extension with MAMP PRO.

@TopCheff

This comment has been minimized.

TopCheff commented Mar 16, 2018

puedo tener más de una versión php instalada en MAMP y poder seleccionar la que más me convenga?

@omadoyeabraham

This comment has been minimized.

omadoyeabraham commented Mar 26, 2018

Years down the line, and this just saved me a whole lot of "googling". Thanks a lot

@ajvillegas

This comment has been minimized.

ajvillegas commented Jul 19, 2018

It worked perfectly. Thank you!

@Phongveth

This comment has been minimized.

Phongveth commented Jul 29, 2018

Thank your very much @Narayanareddy96 , Worked for me , when to did follwing those steps

@pimkle

This comment has been minimized.

pimkle commented Aug 7, 2018

Thank you so much!

@vivekarora

This comment has been minimized.

vivekarora commented Sep 10, 2018

Thank you so much.... very very helpful

@afiaudofia

This comment has been minimized.

afiaudofia commented Sep 17, 2018

Thanks for this. It worked for me.

@antoniocosentino

This comment has been minimized.

antoniocosentino commented Oct 26, 2018

Worked for me, thanks a lot!

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