Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a PHP script from the command line with MAMP?

Tags:

php

macos

mamp

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
like image 929
Andrew Avatar asked Jun 05 '09 08:06

Andrew


4 Answers

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
like image 133
pdeli Avatar answered Sep 30 '22 12:09

pdeli


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.

like image 45
phidah Avatar answered Oct 04 '22 12:10

phidah


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.

like image 42
sstringer Avatar answered Sep 30 '22 12:09

sstringer


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)

like image 43
Tom Haigh Avatar answered Oct 01 '22 12:10

Tom Haigh