How to use MAMP's version of PHP instead of the default on OSX

I would like to use MAMP's version of PHP instead of the default installed on my mac. I tried using

ln -s /Applications/MAMP/bin/php5.3/bin/php php

but I get a "File exists" error. What's the best way to work around this so I can just type php instead of the full path?


Solution 1:

I have created a symlink at the original php location.

1. Locate your osx php version with:

which php

The result should be:

/opt/local/bin/php

2. Backup (move) your original php binary:

sudo mv /opt/local/bin/php /opt/local/bin/php.bak

3. Create the symlink:

sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php

4. Run your new php version:

php -v

PS:

In order for this to work on El-Capitan

  • Reboot your Mac to RecoveryMode (hold Cmd+R on boot)
  • Open Terminal and enter: csrutil disable
  • Reboot
  • either : sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /opt/local/bin/php
    or sudo ln -s /Applications/MAMP/bin/php/php5.4.4/bin/php /usr/bin/php
  • Reboot again to RecoveryMode and re-enable security: csrutil enable

Solution 2:

I would not recommend trying to modify the default version of PHP that is called on the command line. Doing so may break other parts of your system as well as provide you with problems in the future, should you decide to upgrade your OS.

There is an alternative that may meet your needs. You can create an alias to your copy of MAMP's php 5.3. In my case I named the alias phpmamp. Open your terminal and type:

alias phpmamp='/Applications/MAMP/bin/php5.3/bin/php'

Now, typing phpmamp at the command line will launch the MAMP php interperter. Verify this by typing:

phpmamp --help

You will most likely want to store this, and any other alias, in a ~/.bash_profile This will allow the aliases to persist across reboots. Otherwise, the alias should only last for the particular terminal session you are in. More information about creating a .bash_profile file can be found here:

http://www.redfinsolutions.com/redfin-blog/creating-bashprofile-your-mac

Solution 3:

I prefer not to tamper with the current files, so I just prepend the MAMP PHP bin folder to the $PATH env variable.

You can edit ~/.bash_profile and add the the following line to the top

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

Just change the PHP version to the current version you are using.

Don't forget to do source ~/.bash_profile after you edit the file.

Solution 4:

I wasn't pleased with the results / solutions I've found on the net so far, because the php.ini configs weren't loaded properly in all cases and on all systems, espacially when you need modules like ioncube and others (it's even more confusing on MAMP Pro). That's why I've created my own php version aliases (with configs), so I've come up with the following solution, as example (based on MAMP Pro, remember to adjust the php.ini paths to your needs):

Edit your .bash_profile

vim ~/.bash_profile

And add the following entries:

alias php55="/Applications/MAMP/bin/php/php5.5.26/bin/php -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.5.26.ini'"
alias php56="/Applications/MAMP/bin/php/php5.6.10/bin/php -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.6.10.ini'"
alias php56cgi="/Applications/MAMP/bin/php/php5.6.10/bin/php-cgi -c '/Library/Application Support/appsolute/MAMP PRO/conf/php5.6.10.ini'"

Re-Initialize the .bash_profile in the current terminal session (otherwise you won't see any changes, unless you restart the terminal):

source ~/.bash_profile

If you have some additional modules installed, then you can test it with php56 -v and you should get a output of the ioncube, etc. modules. Otherwise test it with php56 -i | grep "yourModuleNameOrSomethingElse"

Now you are able to easily use one of the php versions like "php56" in your terminal with all configs loaded. So it's perfect for testing and building your applications through all iterations of versions including the right php.ini configs through the terminal.

For normal MAMP Users, the configs should be located in /Applications/MAMP/conf/ I think. Happy programming.

Solution 5:

2021 - For those using ohmyzsh, the file to edit is:

/Users/your_user/.zshrc

so, you can edit this file and add the path:

export PATH=/Applications/MAMP/bin/php/php8.0.0/bin:$PATH

Works Perfectly with Big Sur