Create a gist now

Instantly share code, notes, and snippets.

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.

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.

Still getting the same /usr/bin/php

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

zeratool commented May 8, 2014

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

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.

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

Thanks a lot, 2 hours of pain solved!

It worked!! thanks!

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

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.

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

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 commented Jun 23, 2015

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

oh man i love you so much

Tank you !! very helpful

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

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

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

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?

@cord thank you it helped me.

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

zhiachong Thanks for the answer.

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

Thank you! :)

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 commented Sep 9, 2016

It works like a charm!

Thanks.

@ghost

ghost commented Sep 20, 2016

thanks alot !

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

@irazasyed : I love you !

Thank you!

Thanks it is solved my problem in mac too

Sifoon commented Nov 22, 2016

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

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! 👍

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

Thank you so much

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 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.

Great ! Thanks a lot

rryybbaa commented Mar 4, 2017

This works for me. Thanks

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.

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

davidLpt commented Apr 28, 2017 edited

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 commented Apr 29, 2017 edited

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"

Memedawn commented May 28, 2017 edited

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?

Thanks, this works perfectly!

it's not working .
giving same file path

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 commented Jul 9, 2017

worked perfect form me

tutkun commented Jul 19, 2017

Thank you!

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 commented Aug 4, 2017 edited

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, :))
???

Many thanks man :)

Thanks a lot @Narayanareddy96, your solution work fine here

jrran90 commented Aug 20, 2017

@cord many thanks ;)

Thanks!

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