How to override the path of PHP to use the MAMP path?
Solution 1
Everytime you save MAMP config (PHP section), it saves the current version of PHP on ~/.profile
file and creates the alias for php, pear and pecl, to point to the current configured version. (Note: you need to check "Make this version available on the command line" option in MAMP)
However, you need to refresh your terminal (open another session) to get this file refreshed. You can also type source ~/.profile
to refesh the aliases manually.
If you want to extract this curerent version in a PHP_VERSION variable - as commented above - for further use, you can do:
export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`
And then you'll have $PHP_VERSION available with the current version of MAMP.
Finally, if you want to run your php using the current configured version on mamp, you just need to add to your ~/.bash_profile
the following:
export PHP_VERSION=`grep "alias php" ~/.profile | cut -d"/" -f6 | cut -c4-`
export PHPRC="/Library/Application Support/appsolute/MAMP PRO/conf/" #point to your php.ini folder to use the same php settings
export PATH=/Applications/MAMP/bin/php/php$PHP_VERSION/bin:$PATH
Now, even script that relies on /usr/bin/env php
will read the correct version from Mamp config.
Solution 2
In your home folder /Users/David
for exmaple, you can create a .bash_profile
. In here you can export variables and then add them to your path.
Open up the file to edit it in your favourite editor, I use vim.
Then you can add in your path
export MAMP_PHP=/Applications/MAMP/bin/php/php5.3.6/bin
export PATH="$MAMP_PHP:$PATH"
You want your bit ahead of the $PATH
as that already includes /usr/bin
which is where the system PHP lives. So the system will always find your MAMP version first.
Save this file and then reboot your Terminal and you'll see that you should get your MAMP version.
To test I use php -v
as OSX Lion uses 5.3.10 and my MAMP is using 5.3.6
You can also test using which php
which will output the path to your current php executable.
Solution 3
The fact that the previously accepted answer refers to php 5.3.6, while the current version of MAMP ships with 7.2.1 as the default (as of early 2018), points out that this is not a very sustainable solution. You can make your path update automatically by adding an extra line to your .bash_profile
or .zshrc
to get the latest version of PHP from /Applications/MAMP/bin/php/
and export that to your path. Here’s how I do it:
# Use MAMP version of PHP
PHP_VERSION=`command ls /Applications/MAMP/bin/php/ | sort -n | tail -1`
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH
(Use source ~/.bash_profile
after making your changes to make sure they take effect.)
As others have mentioned, you will likely also want to modify your shell to use MAMP’s mysql executable, which is located in /Applications/MAMP/Library/bin
. However, I do not recommend exporting that folder, because there are a bunch of other executables there, like libtool
, that you probably don’t want to be giving priority to over your system installed versions. This issue prevented me from installing a node package recently (libxmljs), as documented here.
My solution was to define and export mysql
and mysqladmin
as functions:
# Export MAMP MySQL executables as functions
# Makes them usable from within shell scripts (unlike an alias)
mysql() {
/Applications/MAMP/Library/bin/mysql "$@"
}
mysqladmin() {
/Applications/MAMP/Library/bin/mysqladmin "$@"
}
export -f mysql
export -f mysqladmin
I used functions instead of aliases, because aliases don’t get passed to child processes, or at least not in the context of a shell script. The only downside I’ve found is that running which mysql
and which mysqladmin
will no longer return anything, which is a bummer. If you want to check which mysql is being used and make sure everything is copacetic, use mysql --version
instead.
Note: @julianromera points out that zsh doesn’t support exporting functions, so in that case, you’re best off using an alias, like alias mysql='/Applications/MAMP/Library/bin/mysql'
. Just be aware that your aliases might not be available from subshells (like when executing a shell script).
Solution 4
I found that on Mavericks 10.8 there wasn't a .bash_profile and my paths were located in /etc/paths
In order to have the new path (whether this is a mamp or brew install of php) take effect it needs to be above the default /usr/bin/php in this paths file. eg.
/Applications/MAMP/bin/php/php5.3.6/bin
/usr/bin
AFter the change, open a new terminal window and run 'which php' that should now point to your updated path
Solution 5
you might still run into mysql binary not being found in that manner
open terminal, type
touch ~/.bash_profile; open ~/.bash_profile
edit as follows below, save, quite and restart terminal or alternately
source ~/.bash_profile
to execute new PATH without restarting terminal
and in the fashion of the DavidYell's post above, also add the following. You can stack various variables by exporting them followed by a single PATH export which I demonstrated below
export MAMP_PHP=/Applications/MAMP/bin/php/php5.6.2/bin
export MAMP_BINS=/Applications/MAMP/Library/bin
export USERBINS=~/bins
export PATH="$USERBINS:$MAMP_PHP:$MAMP_BINS:$PATH"
cheers
sf_tristanb
CTO @ Y-Proximité (Lyon - France) | @sf_tristanb Symfony developper | Personal project : http://www.seek-team.com
Updated on February 14, 2022Comments
-
sf_tristanb over 2 years
After screwing up entirely my PHP configuration on MAC trying to get the SOAP module working (-bash: /usr/bin/php: No such file or directory ....) I now have to use MAMP but each time I have to type the path
Applications/MAMP/bin/php5.3/bin/php to do command line.
How to just type php instead the entire path on MAC ? I double checked and i do not have a file named
.profile
norbash_profile
Thanks
PS: Here's what output echo $PATH :
echo $PATH /Applications/MAMP/Library/bin/:/Applications/MAMP/bin/php5/bin/:/opt/local/bin:/opt/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/git/bin:/usr/X11/bin