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

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

Posted by: admin November 17, 2017 Leave a comment

Questions:

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?

Answers:

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

Questions:
Answers:

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
Questions:
Answers:

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.

Questions:
Answers:

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.

Questions:
Answers:

Well, the ‘file exists’ error is probably because you attempted to create a sym-link with the name of a file that was already there. I assume you were in the directory containing the php version you were trying to replace or that this was a second attempt and you did not first remove the existing sym-link. I agree with the others with regard to not “replacing/modifying” the default version of php.

Based on the second part of the question, the best way to get around having to type the full path, the answers suggesting an alias are right on point with that. When multiple versions are involved though, that means having to call something other than php to run the version you want to run.

I have a script that lets me “select” the version of php that I would like to work with which then creates a sym-link to that version and lets me simply enter ‘php’ as my command when I want to use it. I wrote a blog about it here where you can get the script. Based on the answer given by @ioCron I may need to revisit my script to account for the different config folders associated with each version.

Questions:
Answers:

Well none of this was working for me with OSX10.12.5

i have mac ports php70 installed at /opt/local/bin

which php showed:

/usr/bin/php

I set up the aliases and local paths etc, which mostly worked for me, but other programs were failing (like composer) so the solution for me was to prepend:

/opt/local/bin
/opt/local/sbin

to the file /etc/paths

then it all worked a charm!

Leave a Reply

Your email address will not be published. Required fields are marked *