Skip to content

Instantly share code, notes, and snippets.

@davidwebca
Last active November 18, 2022 17:15
Show Gist options
  • Save davidwebca/0011b6da0ce6f75a4d91547fa9a94ae4 to your computer and use it in GitHub Desktop.
Save davidwebca/0011b6da0ce6f75a4d91547fa9a94ae4 to your computer and use it in GitHub Desktop.
Use MAMP's Apache selected PHP version on CLI

How to set your command line to MAMP's version

Since Mac OS comes with PHP bundled, even if you have MAMP, you'll find out that you can't use MAMP's versions on the command line. This is only true if you use the free version of MAMP since MAMP pro seems to have a feature to add that to the cli automatically.

Open your ~/.bash_profile file or ~/.zshrc if using ZSH

In this file, you might see nothing or only a few configurations already. You can safely ignore them and add this lower:

PHP_VERSION=$(grep "^[^#\;]" "/Applications/MAMP/conf/apache/httpd.conf" | grep "LoadModule php" | awk -F'[\/]' '{print $6}')
export PATH=/Applications/MAMP/bin/php/${PHP_VERSION}/bin:$PATH
export PATH=/Applications/MAMP/Library/bin:$PATH

Note

This is different from other scripts floating around which usually tries to select the latest PHP version. In my case, I needed to be able to swap around versions and not always use the latest PHP version so I had to find this solution. But if you need to need the latest PHP version at all time on cli, here's how you would change the first line:

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

This is only working when Apache is selected in MAMP. I don't know if Nginx is setup only as a reverse proxy in MAMP, but if so it could still work for you in that case, otherwise we would have to edit the grep to find the PHP version somewhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment