How to override the path of PHP to use the MAMP path?
Asked Answered
B

13

60

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 nor bash_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
Ballyhoo answered 10/11, 2010 at 14:47 Comment(0)
P
15

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.

Proportionate answered 3/10, 2018 at 0:48 Comment(1)
Is the auto update of .profile a PRO version thing, only? I've always had to manually update .profile for as long as I've been using MAMP (~10 years)Flicker
G
130

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.

Gimbals answered 18/5, 2012 at 13:21 Comment(2)
Worked like magic. I tried upgrading default php that comes with mac but no luck. finally now PATH of php points my MAMP's php.Tillio
I know this post is a little bit old, but maybe it can help someone. If you're using MAMP with oh-my-zsh, you should put these DavidYell lines in ~/oh-my-zsh/.zshrc file. You'll find the "export PATH" line (at least now in my current updated version) on line 54. Put the DavidYell's lines jst AFTER that. For me, worked like a charm.Consummate
F
116

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).

Follmer answered 1/5, 2015 at 16:9 Comment(13)
This solution is elegant, sustainable, and absolutely genius. Thank you!Assyria
export function does not work on zsh. What about using an alias? (e.g alias mysql='/Applications/MAMP/Library/bin/mysql')Cade
@julianromera I started with aliases, but in my experience, aliases don’t get passed to child processes, or at least not in the context of a shell script. So if I run a shell script that uses mysql, it won’t find my alias and will instead use the system version of mysql. Anyways, thanks for the tip! I’ll add a note about zsh.Follmer
This is a very cool way to do this, BUT the latest version of PHP on MAMP is 7 and most apps will break using that version. I wonder if theres a way to get the current default from a conf file?Farhi
@Farhi That’s a great point. Certainly would be possible to read a file to get the current active MAMP PHP version, but I don’t work with PHP anymore, so I don’t have MAMP installed. Makes it hard to explore. Could you look into your MAMP config to find the file that would have that info and post a copy of it, along with the path to where it is located?Follmer
interesting that setting an alias for php to the MAMP binary doesn't work in El Capitan, but adding the bin directory to PATH does. anyone know why? thanks!Resinous
what if you need to step back a version and not use the latest version of PHP, running into issues with Drush and Drupal. Can the above be modified: PHP_VERSION=ls /Applications/MAMP/bin/php/ | sort -n | tail -1 or does it need to be hard coded, as shown in @david's solution?Articulator
If you want to explicitly avoid using the latest version of PHP, I would figure out what exact version you want to use and hardcode it into the path, like in David Yell’s solution.Follmer
Using 'command' ahead of the ls allows for pre-existing ls aliases to be ignored: command ls /Applications/MAMP/bin/php/ | sort -n | tail -1Glorious
@AndrewPatton this doesn't work anymore on Mac Catalina :(Sift
@FredK that’s too bad! maybe that’s because the new default interactive shell is now zsh. you could try taking the lines i suggested adding to your .bash_profile and instead add them to .zprofile in your home directory, if you have one.Follmer
@AndrewPatton I've not moved to zhsSift
I got this working in Catalina by putting Andrew's code at the top of the .zshrc file instead.Punishment
P
15

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.

Proportionate answered 3/10, 2018 at 0:48 Comment(1)
Is the auto update of .profile a PRO version thing, only? I've always had to manually update .profile for as long as I've been using MAMP (~10 years)Flicker
K
13

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

Keating answered 11/6, 2014 at 10:4 Comment(2)
On Yosemite here. In my case doing which php before your suggestion still produced the correct path (since I had the path in my .bash_profile) but phpinfo() wasn't showing mcrypt (or any other extensions in my php.ini) as loaded. Doing this fixed that however, so thanks!Guff
You should be creating your own .bash_profile in your home folder so your changes only impact you.Gimbals
R
10

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

Revenue answered 24/10, 2014 at 14:15 Comment(0)
S
3

If you have to type

/Applications/MAMP/bin/php5.3/bin/php

in your command line then add

/Applications/MAMP/bin/php5.3/bin

to your PATH to be able to call php from anywhere.

Stakhanovism answered 10/11, 2010 at 14:57 Comment(4)
yeap but how to add it to the PATH variable please ?Ballyhoo
export PATH=$PATH:/Applications/MAMP/bin/php5.3/bin You may have to restart / reconnect your terminal for it to work (at least with different ones open in parallel).Stakhanovism
it seem this not permanent. when i open new terminal. it use default MAC php path at /usr/bin/phpArgentous
For permanent storage you’ll have to save it not only to your local, temporary PATH but to your systems path. Although I’m not familiar with MAC it should probably be export PATH to persistate it after you set it (modified it).Stakhanovism
R
3

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! See attached screenshot:)

screenshot

Rudder answered 16/1, 2019 at 0:20 Comment(0)
A
2

This one worked for me:

sudo mv /usr/bin/php /usr/bin/~php
sudo ln -s /Application/XAMPP/xamppfiles/bin/php /usr/bin/php
Abba answered 31/3, 2015 at 9:5 Comment(0)
K
2

For XAMPP users you can use this:

# Use XAMPP version of PHP
export PATH=/Applications/XAMPP/xamppfiles/bin:$PATH
source ~/.bash_profile

And you can check it with:

php -v
Ketron answered 27/8, 2016 at 7:16 Comment(0)
A
1

Sometimes, it's easier to do this:

sudo ln -s /Applications/MAMP/bin/php/php5.6.10/bin/php /usr/bin/php;

Mamps version of PHP at the time of posting was php5.6.10, so make sure you change that to the version you're using.

You'll be up in a jiffy.

Anchovy answered 9/7, 2015 at 14:48 Comment(0)
R
1

Probably too late to comment but here's what I did when I ran into issues with setting php PATH for my XAMPP installation on Mac OSX

  1. Open up the file .bash_profile (found under current user folder) using the available text editor.
  2. Add the path as below:

export PATH=/path/to/your/php/installation/bin:leave/rest/of/the/stuff/untouched/:$PATH

  1. Save your .bash_profile and re-start your Mac.

Explanation: Terminal / Mac tries to run a search on the PATHS it knows about, in a hope of finding the program, when user initiates a program from the "Terminal", hence the trick here is to make the terminal find the php, the user intends to, by pointing it to the user's version of PHP at some bin folder, installed by the user.

Worked for me :)

P.S I'm still a lost sheep around my new Computer ;)

Radarman answered 27/11, 2016 at 18:3 Comment(0)
I
0

This is not an ideal solution as you have to manage two ini files however, I managed to work around this on windows by copying the php ini file in mamp from the conf folder to your active php version in the bin folder.

[MAMP INSTALL]\conf\[ACTIVE PHP VERSION]\php.ini

copy to

[MAMP INSTALL]\bin\php\[ACTIVE PHP VERSION]

Investment answered 18/10, 2015 at 12:58 Comment(0)
W
0

To compliment the current accepted answer, if you assume that MAMP uses the most recent version of php5 as the default, you can add grep 'php5' in the middle:

PHP_VERSION= `ls /Applications/MAMP/bin/php/ | sort -n | grep 'php5' | tail -1`

and you are guaranteed to get the most recent php5 regardless of MAMP version.

Weiss answered 29/8, 2016 at 11:34 Comment(2)
I had hoped this would work, but I just get -bash: php5.6.10: command not found when I source ~/.bash_profileTargett
It seems to me that your bash is trying to execute php5.6.10 which is weird. The intended usage is PHP_VERSION= 'ls /Applications/MAMP/bin/php/ | sort -n | grep 'php5' | tail -1' export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATHWeiss

© 2022 - 2024 — McMap. All rights reserved.