Installing Drush on MAMP

November 5, 2013
Drush logo

Last article update: March 9, 2017
(tested on new MacOS Sierra install, without a problem)

Drush provides command-line access to the Drupal installation. I'm not a big fan of the command-line but in the case of Drush I make an exeption. The time it takes to set up Drush is nothing compared to the time you gain during development. Examples? Drush pm-update (drush up) updates all core and contrib modules, including the database updates. With FTP-access this takes much longer.

Right from the start, during the development on a local machine (an AMP-stack, Apache, MySQL, PHP) Drush comes in handy.

I use MAMP on Mac OS X Yosemite.

Use the PHP version of MAMP

Before starting, we need to see what PHP version MAMP uses. To see that, open MAMP, click on Preferences and then PHP. In my case, PHP version 5.6.2 is used.

MAMP php versie

When we work with MAMP, we want to use the php version of MAMP, not the system version. Therefore, first check the PHP version in use by typing this command into Terminal:

which php

Normally you will receive the output /usr/bin/php. We want to use the version under /Applications/MAMP/bin/php/php5.6.2/bin/php. To change this, we have to edit the profile with root (sudo) access. Type:

sudo nano ~/.bash_profile

Now add the following line:

export PATH=/Applications/MAMP/bin/php/php5.6.2/bin:$PATH

Then click CTRL + X, then Y and Enter. In order to see the changes we have to reload the file with the profile we just changed:

source ~/.bash_profile

... to check the PHP version with the same command we used in the beginning of this tutorial:

which php

Install Composer

Ok. From here on, we can use the documentation of the Drush project, as found on Github. The easiest way to install Drush is by using Composer.

In the terminal, browse to your home directory:

cd ~

Download Composer:

curl -sS https://getcomposer.org/installer | php

Composer install

Create a usr/local/bin directory:

sudo mkdir -p -m 755 /usr/local/bin

And from within the home directory, move the composer.phar to the newly created /usr/local/bin folder:

sudo mv composer.phar /usr/local/bin/composer

Next, we have to put Composer in our PATH, so we can access it globally. Run:

sudo nano ~/.bash_profile

And add the following line:

export PATH="$HOME/.composer/vendor/bin:$PATH"

Then click CTRL + X, then Y and Return (Enter).

In order to see the changes we have to reload the file with the profile we just changed:

source ~/.bash_profile

To test if everything is working well, just run the Composer command:

composer

Install Drush

Next, we can install Drush with one command:

composer global require drush/drush:dev-master

This will install the most current version. If you want to install another version, you can change this with the same command. For example, if you need Drush version 8 with support for Drupal 6, 7 and 8, use this command:

composer global require drush/drush:8.*

Test by using the next command:

drush status

Drush status

Add the MAMP shell path

Lastly ,we have to add the shell path to OS X. We need this to use the MySQL version of MAMP. Without this step we can use Drush to download a module but when we want to enable a module, we will receive a bunch of errors.

First, check the MySQL path, normally this is /Applications/MAMP/Library/bin. Then add this line to .bash_profile, in the same manner as the other two lines:

export PATH=/Applications/MAMP/Library/bin/:$PATH

The final .bash_profile should look like this:

bash_profile for Drush

Voila, Drush is functional on the localhost and has the right permissions to run all the commands!

When you run into problems, like "PDOException: SQLSTATE" or "Command pm-enable needs a higher bootstrap level to run", check these things:

  • Make sure Apache and MySQL are running (check this setting in the MAMP interface)
  • When running Drush, make sure you use the command 'cd' to navigate to the local folder of the website
  • When you update MAMP, the php version can change. For best result, the php version of MAMP and the path in the .bash_profile should match
  • Try '127.0.0.1' instead of 'localhost' and define the correct port (check the MAMP port, mine is '8889') in the settings.php file; try both options (debug MySQL issues with drush status -d)
  • When it's a very big installation, check the memory limits in the MAMP configuration files

Note, before reading the comments and getting confused: this article has already been revised two times. The first version featured an installation of Drush with Pear, the third a manual install and this version (14th of February 2015) has an install with Composer. The installation with Composer is by far the easiest.

Comments

Loved it, thank you.

Appears to work perfectly with High Sierra 10.13.2 Developer Beta version 5

Thanks for this tutorial. Saved so much time. You have my appreciation.

Pages

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.