WP-CLI is an awesome collection of command line tools for WordPress but if your development environment includes MAMP you might run into some problems.

The goal of this post is to highlight some of the common stumbling blocks you might encounter when trying to install WP-CLI for MAMP and how to fix them.

  1. Error Establishing A Database Connection
  2. Can’t connect to local MySQL server through socket
  3. Undefined Index: SERVER_NAME

Error Establishing A Database Connection

The error might occur if WP-CLI is unable to locate MAMP’s version PHP. To fix you this you need to find out where MAMP’s PHP is located by typing which php in the command-line.

$ which php
/Applications/MAMP/bin/php/php5.4.4/bin/php

Now that you know which version of PHP MAMP is running you can add it as a constant to your .profile or .bash_profile using an editor of choice. The following example uses nano.

$ nano ~/.profile

Add this to your .profile or .bash_profile and hit CTRL + O to write followed by CTRL + X to exit nano.

export WP_CLI_PHP="/Applications/MAMP/bin/php/php5.4.4/bin/php"

You need to quit and relaunch the terminal or reload the source using

$ source ~/.profile

To verify that the constant was added you can try echoing WP_CLI_PHP

$ echo $WP_CLI_PHP/Applications/MAMP/bin/php/php5.4.4/bin/php

Now cd into your wp-cli directory and run sudo utils/dev-build once again

$ cd <path-to-wp-cli>
$ sudo utils/dev-build

Once installed, navigate into a WordPress installation and type wp

Can’t connect to local MySQL server through socket

WP-CLI may search for mysql in /tmp/mysql.sock so we need to create a symbolic link to our MAMP mysql.sock

$ sudo ln -s /Applications/MAMP/tmp/mysql/mysql.sock /tmp/mysql.sock

Undefined Index: SERVER_NAME

Examine your WordPress wp-config.php file or create a new one if neccessary. This error could occur if you are using a custom config file as opposed to the default one provided by WordPress.