Using PHP on the Command Line with MAMP

MAMP is a popular local development environment for macOS users.

If you are new to Craft and don’t have an existing local development environment, we strongly recommend that you check out DDEV!

Background #

Craft is primarily used over HTTP, but it also has a command-line interface (or “CLI”) for common developer tasks like clearing caches, running migrations, or rebuilding search indexes. Accessing the application in both ways may require some configuration.

Using MAMP’s PHP Executable in Terminal #

When you run php on the command line, it uses whichever PHP executable your shell finds among the configured $PATHs. This is often not one bundled with MAMP; it could be the macOS default, installed via Brew, or none at all. As a result, the terminal’s PHP version (and php.ini settings like memory_limit) can be different than the one MAMP uses for HTTP requests.

You can see which PHP executable your shell will use by running which php.

In Craft’s control panel go to UtilitiesPHP Info, and search for include_path. You should see something like this:

.:/Applications/MAMP/bin/php/php8.3.9/lib/php

Note the path segment that includes the PHP version (php8.3.9 in the example above). We can translate this into the executable’s absolute path on your system—and invoke it directly from the command line:

/Applications/MAMP/bin/php/php8.3.9/bin/php -v

# PHP 8.3.9 (cli) (built: Aug  1 2024 10:45:48) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.3.9, Copyright (c) Zend Technologies

Set up an Alias #

It would be a bit cumbersome to have to type out that entire path every time you wanted to run a Craft or Composer command! Fortunately, UNIX aliases can simplify this.

Create a .bash_profile file in your system user’s home directory (usually /Users/myusername):

touch ~/.bash_profile

Then, go to your home folder and open .bash_profile. (It’s a hidden file on Macs, so if you don’t see it, press Command+Shift+. to show hidden files. You can also run open ~/.bash_profile to open it with Text Edit!) Add this line at the top (again, replacing 8.3.9 with the desired version):

alias mampphp="/Applications/MAMP/bin/php/php8.3.9/bin/php"

Save the file, then open a new terminal window (or type source ~/.bash_profile). You can now use mampphp as a shortcut for that specific PHP version bundled with MAMP!

mampphp -v

# PHP 8.3.9 (cli) (built: Aug  1 2024 10:45:48) (NTS)
# Copyright (c) The PHP Group
# Zend Engine v4.3.9, Copyright (c) Zend Technologies

Update $PATH #

If you want to replace php directly, you can add the MAMP directory to your $PATH variable by dropping this line into the .bash_profile file we created earlier:

export PATH="/Applications/MAMP/bin/php/php8.3.9/bin/php:${PATH}"

When you type a command in your terminal, the system searches each colon-separated path in the global PATH variable until it finds an executable with that name. Prepending MAMP’s directory to the list ensures it matches as early as possible.

To verify the change, exit your terminal window and open a new one. Run which php and you should see the new MAMP path:

/Applications/MAMP/bin/php/php8.3.9/bin/php

Changes made to .bash_profile only take when you start a new terminal session or run source ~/.bash_profile.

Using Composer #

Composer is not automatically bundled with MAMP, so you must install it on your own, using the PHP executable we derived earlier—or skip it entirely and use a pre-built ZIP archive of the starter project, which can be found attached to every release.

Once Craft is installed, you can use the built-in updater via the control panel or CLI—you do not need a Composer executable on the command line to update Craft or install plugins.

Using MAMP’s MySQL Server in Terminal #

The same process can be used to access MySQL executables (like mysql and mysqldump). We cover MAMP’s MySQL paths in the Backing Up Your Database With MAMP article.

Running Craft Commands #

With access to MAMP’s PHP executable via an alias or your $PATH, you can now invoke Craft’s CLI:

mampphp craft help

# ╭───╮
# │ C │ Craft CMS
# ╰───╯
# 
# Welcome to Craft CMS version 5.4.4 (Yii 2.0.51)
# 
# ...

Set Important php.ini Values #

Some of MAMP’s default php.ini settings don’t agree with Craft’s requirements. To edit the php.ini file for the version of PHP your Craft site is running, go to FileOpen TemplatePHP (php.ini), and select the appropriate version.

  • Find max_execution_time and set it to 60 or higher
  • Find memory_limit and set it to 256M or higher
  • Save and close the file
  • Restart MAMP

Applies to Craft CMS 5, Craft CMS 4, and Craft CMS 3.