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

10

73

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?

Lansing answered 23/11, 2010 at 23:15 Comment(0)
B
98

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

Banerjee answered 24/11, 2010 at 1:28 Comment(6)
In later versions, alias phpmamp='/Applications/MAMP/bin/php/php5.3.20/bin/php' replacing the 5.3.20 portion with your versionFelloe
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.Greenlet
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'Electret
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.Wastepaper
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.Global
making alias is recomendedVallo
V
110

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
Villanovan answered 21/6, 2013 at 16:47 Comment(5)
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/.Barbour
Unfortunately, this won't work in El Capitan anymore... the best solution now is the bash_profile one...Above
I'm on El Capitan, and this worked without the additional steps.Darrickdarrill
Just tried on Catalina 10.15.6, mv: rename /usr/bin/php to /usr/bin/php.bak: Operation not permittedAbhenry
It's worked fine in high sierra. Thank's for save my timeGalloway
B
98

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

Banerjee answered 24/11, 2010 at 1:28 Comment(6)
In later versions, alias phpmamp='/Applications/MAMP/bin/php/php5.3.20/bin/php' replacing the 5.3.20 portion with your versionFelloe
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.Greenlet
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'Electret
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.Wastepaper
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.Global
making alias is recomendedVallo
O
86

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.

Oneself answered 25/10, 2014 at 11:14 Comment(9)
This really did the trick for me, also is the easiest way to change the PHP version quickly!Pleo
This is the safest and easiest way, and won't require you to re-run the script(s) each time the OS updates.Darciedarcy
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.Nolly
This is the best answer as of now, safest & really easiest!!Odometer
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.Cheroot
@Cheroot good comment! Thank you! This source ~/.bash_profile resolve to take the right PHP!Abhenry
Glad I could help @StanislavStankovCheroot
Worked for me. Mamp Pro - Mac OS Big Sur (beta)Svetlanasvoboda
this worked for me on mac ox 11.0.1. Thank you.Hixon
S
8

2024 - 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.2/bin:$PATH

replace 'php8.2' with you php version in your MAMP.

Somber answered 20/6, 2021 at 23:21 Comment(0)
O
5

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.

Ought answered 4/8, 2016 at 18:56 Comment(0)
B
2

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

Buerger answered 15/1, 2019 at 22:57 Comment(3)
MAMP Pro Version 4 also has that featureGadfly
I've seen this referenced a lot, but that option does nothing for me. Maybe it requires some other configuration?Svetlanasvoboda
Legend! This worked for me. Upvoting. Wish it could be accepted answer.Essieessinger
L
2

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.

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

let me know if you need help.

Lobectomy answered 19/1, 2021 at 7:2 Comment(0)
S
2

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

Steerage answered 9/5, 2021 at 7:15 Comment(0)
E
0

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.

Everett answered 8/9, 2016 at 13:15 Comment(0)
A
0

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!

Antisepticize answered 5/7, 2017 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.