MAMP with Composer and MySQL on the Command Line

Many developers prefer MAMP for their local development environment. Running MySQL and Composer commands from a terminal window requires some configuration, and if you’re comfortable on the command line (or comfortable enough to be dangerous) this article will show you how to sync things up with MAMP.

Please note that this topic is not specific to Craft CMS. It applies to any PHP CMS or framework you choose.

Using Composer on the command line is not a requirement to work with Craft 3. With MAMP, you’ll do just as well (or better) to install Craft from a zip file then initiate updates from the Control Panel.

Using MAMP’s PHP Executable in Terminal #

When you run composer on the command line, it runs under the PHP executable that the terminal session is configured to use. By default, this is most likely a different PHP executable than the one MAMP provides. In that case, the terminal’s PHP version and php.ini settings (like memory_limit) will be different than MAMP’s and will cause problems.

You can see which PHP executable your terminal session will use by running which php. You can also see its memory_limit setting by running php -i | grep memory_limit.

In Craft’s Control Panel go to Utilities > PHP Info. Find include_path on that page, and you will see something like this:

.:/Applications/MAMP/bin/php/php7.0.27/lib/php

Remember that php version. Next, make sure you have a .bash_profile file in your user directory.

touch ~/.bash_profile

Now go to your user folder, find and edit .bash_profile. It’s a hidden file on Macs, so if you don’t see it, press Command+Shift+. to show and hide hidden files. Add this line to the top:

export PATH="/Applications/MAMP/bin/php/php7.0.27/bin:${PATH}"

Change php7.0.27 to match what you found previously in the Control Panel’s PHP Info page.

When you type a command in your terminal, it searches for the first matching executable in a list of colon-separated directories specified in the PATH variable. With MAMP's PHP bin directory first in line, it will find php there. If you’re curious, run echo $PATH to see the whole list.

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

/Applications/MAMP/bin/php/php7.0.27/bin/php

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

PHP 7.0 is good enough for Composer with Craft 3. If you look at the composer.json file, you’ll see a platform setting that forces Composer packages to be compatible with PHP 7.0.

Using MAMP’s MySQL Server in Terminal #

When installing Craft on the command line, your terminal session needs to use MAMP’s MySQL server. Edit the .bash_profile file from the previous section and add MAMP’s MySQL path:

export PATH="/Applications/MAMP/Library/bin:/Applications/MAMP/bin/php/php7.0.27/bin:${PATH}"

Now terminal will look for mysql in MAMP’s bin directory first. You can also run mysql and mysqldump commands on MAMP’s database when you need to. Close your terminal session and open a new one. Don’t forget start MAMP’s MySQL server if it’s not already running.

Set Important php.ini Values #

MAMP runs slowly compared to production servers, so it needs extra time to do things. It also needs plenty of memory allocated to PHP. Edit the php.ini file for the version of PHP your Craft site is running. Go to File > Edit > Edit Template > PHP (php.ini) and select the version of PHP you’re using.

  • 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

Be kind to yourself and do that for all of MAMP’s PHP versions. Do use MAMP’s own php.ini editor. If you’re tempted to find and edit the correct php.ini in the file system on your own… good luck.

Applies to Craft CMS 3.