Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

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

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
  • This was the best solution for me. Even when I used the php alias CakePHP console would continue to use the version in /usr/bin/. – Corie Slate Aug 8, 2013 at 17:56 Unfortunately, this won't work in El Capitan anymore... the best solution now is the bash_profile one... – AlexK Oct 27, 2015 at 8:41 Just tried on Catalina 10.15.6, mv: rename /usr/bin/php to /usr/bin/php.bak: Operation not permitted – Stanislav Stankov Aug 18, 2020 at 19:12

    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

    In later versions, alias phpmamp='/Applications/MAMP/bin/php/php5.3.20/bin/php' replacing the 5.3.20 portion with your version – Andrew Winter Mar 8, 2013 at 8:14 You can also create an alias for "php" itself, which would do what you were asking for without messing around with configs: alias php='/Applications/MAMP/bin/php5.3/bin/php' This would be necessary for automated tools like Composer to use the right version of php. Also remember to restart the terminal or type "source ~/.bash_profile" for the change to take effect. – soulkphp Jun 9, 2013 at 8:26 FWIW, the latest release has modified the path a little. Hope this helps someone avoid "find my path" purgatory. :D alias phpmamp='/Applications/MAMP/bin/php/php5.5.3/bin/php' – Mike S. Dec 27, 2013 at 0:54 I used this method before, BUT I switched to the method below, because the default php install does not have certain extensions installed (e.g. mcrypt). This is needed in Laravel installation (with Composer) and using artisan. – ATutorMe May 11, 2015 at 4:25 What about the a variant of the OP's suggestion and John's suggestion combined. Using a symbolic link, but calling it phpmamp, and creating it in /usr/local/bin instead of Applications? Would this not be a better way, as then it will persist over reboots without needing to edit the bash_profile, and it would make it easier to trace/remember that you created it, as it would exist in the bin directory, rather than being hidden in the bash profile script. – redfox05 Oct 14, 2015 at 16:24

    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.

    This really did the trick for me, also is the easiest way to change the PHP version quickly! – Maarten de Graaf Aug 18, 2015 at 9:56 This is the safest and easiest way, and won't require you to re-run the script(s) each time the OS updates. – PaulSkinner Mar 30, 2016 at 7:37 Best answer! I've used a symlink several year, but just changing the PATH variable ist the most elegant way, since you don't get reset by MacOS updates. – Matthias Kleine Oct 11, 2016 at 16:30 This is the better answer. If you have mamp installed you can just switch the install. I ran which php which gave me /Applications/MAMP/bin/php/php7.1.8/bin/php. So I first checked the install directory for Mamp and found out what versions there were. Then I ran vim ~/.bash_profile, changed the export path for php to be 7.0.22. Then I ran source ~/.bash_profile. And that was it, now when I run which php in the command line I got: /Applications/MAMP/bin/php/php7.0.22/bin/php And everything worked like a charm. – Maximus Feb 2, 2018 at 15:39

    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.

    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

    If your terminal is using zsh (oh-my-zosh) as shown in the attachment. check image Do the following.

    Mac Big Sur uses "zsh" Oh-my-zosh for the terminal. so, I did the following.

  • open terminal.
  • check if you have .zshrc file in your profile path (/Users/yourProfileName)
  • if you don't have .zshrc file, create one using (~ touch .zshrc) command.
  • add these lines: export MAMP_PHP=/Applications/MAMP/bin/php/php7.4.12/bin export PATH="$MAMP_PHP:$PATH"
  • save the file. close the terminal and reopen it. Now run "which php".
  • let me know if you need help.

    For Mac OS Catalina. Find directory /Users/<user_name>/.zprofile

    and add (for example)

    # MAMP PRO PHP
    export PATH="/Applications/MAMP/bin/php/php7.4.2/bin:$PATH"
    

    after reboot, in terminal

    which php
    

    new php version /Applications/MAMP/bin/php/php7.4.2/bin/php

    The latest version of MAMP (Version 5+) offers an easy way to make the MAMP PHP version available to the command line. Just select "PHP" in the the side bar menu and check "Make this version available on the command line". Easy peasy! :)

    screenshot

    I've seen this referenced a lot, but that option does nothing for me. Maybe it requires some other configuration? – Drew Dello Stritto Sep 23, 2020 at 23:45

    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.

    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!

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.