47

I have MAMP installed. Now I am trying to run a script from the command line, but I can't seem to get it to work.

How should I set up my environment so that I can run a script from the command line and use the PHP version I installed with MAMP?

Update: I agree with jjeaton below, here is a nice solution of creating an alias to MAMP's PHP:

# add this to your ~/.bash_profile
alias phpmamp='/Applications/MAMP/bin/php/php5.3.6/bin/php'

Now you can use it from the command line:

$ phpmamp --help
Community
  • 1
  • 1
Andrew
  • 227,796
  • 193
  • 515
  • 708
  • 2
    [This answer](http://stackoverflow.com/questions/4262006/how-to-use-mamps-version-of-php-instead-of-the-default-on-osx) has a better solution that won't mess with your stock php install. – jjeaton Feb 20 '12 at 12:50
  • 1
    Don't forget the to enter : source ~/.bash_profile to reload your bash profile ! – rebe100x Jan 23 '17 at 05:57

5 Answers5

40

Please note that with version 2.0.5 of MAMP, the path has changed. It is now one of the following:

/Applications/MAMP/bin/php/php5.2.17/bin/
/Applications/MAMP/bin/php/php5.3.6/bin/

Therefore the command to add MAMP's php command should probably look like this:

export PATH=/Applications/MAMP/bin/php/php5.2.17/bin/:$PATH

or like this (depending on which version of PHP you want to use):

export PATH=/Applications/MAMP/bin/php/php5.3.6/bin/:$PATH
pdeli
  • 416
  • 5
  • 2
11

Another way that works that may be a little cleaner with regard to PHP versions is to create an alias in your bash profile that points to the specific php binary that you want to run when you run things like composer or other cli tools. This has the benefit of avoiding some potential library and php.ini config compatibility issues with the installed version of php in OSX.

For instance, if you want to point to php 5.4.1 in MAMP, edit your .bash_profile file in your editor of choice (nano, vi, etc.):

# nano ~/.bash_profile

Add this below your PATH statement:

alias php=/Applications/MAMP/bin/php/php5.4.10/bin/php

Save and quit (CTRL+X in nano, :wq in vi). Quit Terminal. The next time you try to call php from the cli, you'll be using the 5.4.10 version installed with MAMP.

Remember to update this path if you update MAMP with a more recent version of PHP.

sstringer
  • 486
  • 7
  • 12
  • Do you need to restart your machine for this to take effect? – Mr. Concolato Mar 10 '16 at 17:14
  • for me this is the best option so I can create different aliases for different projects that need different php versions on the cli, ie php7.2, php7.1, etc... – Pierre-Verthume Larivière Apr 21 '20 at 16:01
  • Mr. Concolato you dont need to restart your server but instead you need to either open a new terminal or re-read the .bash_profile with source ~/.bash_profile. Note that for me my .bash_profile was not working because it had a source ~/.profile at the beginning and the file ~/.profile did not exist (I didnt put that there..) after commenting this line with # I was able to run source ~/.bash_profile without errors – Pierre-Verthume Larivière Apr 21 '20 at 16:03
10

Run this in your Terminal:

export PATH=/Applications/MAMP/bin/php5/bin/:$PATH

Should do the trick. It will - as Tom Haigh mentioned - add the MAMP PHP executable to the path so you can use "php" instead of the full path.

phidah
  • 5,794
  • 6
  • 37
  • 58
  • 1
    php -i | grep php.ini - this should give you the MAMP php ini location if it is working properly, rather than /etc – Tom Haigh Jun 05 '09 at 22:48
  • 1
    Andrew, you can use "which php" to get the path of the php executable that the php command will use. – phidah Jun 06 '09 at 09:50
  • In MAMP 1.9.4, this needs to be export export PATH=/Applications/MAMP/bin/php5.3/bin:$PATH - since /Applications/MAMP/bin/php5/bin/ no longer exists. This points to PHP 5.3, but there may be a way to point at whatever MAMP is set to use instead. – Alex King Jan 28 '11 at 16:46
  • @Alex King, that depends on what version of PHP you are using. – amateur barista Mar 20 '11 at 00:25
  • Hi @phidah only your solution worked for me on Mountain Lion 10.8.5 with MAMP 2.2. But I cannot seem have it listed neither in ~/.profile nor in ~/.bash_profile. Just wondering Where has this been added. Even its working good. – Jaguar Oct 08 '13 at 18:48
2

Yes, I think it is here: /Applications/MAMP/bin/php5/bin/php

You can either add /Applications/MAMP/bin/php5/bin/ to the front of your path or create a symlink in /usr/bin (there probably is one there already for the default PHP installation)

Tom Haigh
  • 57,217
  • 21
  • 114
  • 142
  • so would the PHP CLI be built in to /Applications/MAMP/bin/php5/bin/php because I don't see anything about PHP CLI in /Applications/MAMP/bin/php5/bin – Andrew Jun 05 '09 at 10:36
  • I thought that the php binary was in there on mine, but maybe not. It will just be called 'php'. I will have a look on my Mac later. – Tom Haigh Jun 05 '09 at 10:41
0

Just replace the PHP version in this command:

/Applications/MAMP/bin/php/php7.4.21/bin/php  index.php
Suraj Anand
  • 53
  • 1
  • 1
  • 7
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 24 '22 at 14:03