Globally Installing Composer and Drush on a Mac / MAMP

Tatenda Chuma
Frontend and App Developer
Topic
Intermediate
Installing Composer and Drush on a Mac

Drush and Composer provide command-line access to the Drupal 7 / 8 installation.

Nowadays using command-line in Drupal Development becoming an essential skill for developers. Although I'm not a big fan of the command-line, in the case of Drush and Composer I found it really useful and it made a big difference in my development time.

I found that 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 contributed modules, including the database updates. With FTP-access this takes much longer.

In this blog I will be taking you step by step in installing Drush and Composer on Mac / MAMP.

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

For this case, we are using  MAMP 4 on Mac OS Sierra.

Environment setup

Open your terminal and navigate to your home directory. To this this type cd ~/ in the terminal and press enter.

cd ~/

This will take you to the root directory.

echo $PATH

Your path should look something like this

/Applications/MAMP/bin/php/php7.0.22/bin:/Users/your_username bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications

By entering echo $PATH, we want to ensure we have /usr/local/bin inside of our path. This the path that we will use to install our composer files.

If you cannot see /usr/local/bin in your path, we need to export the path and refresh our .bash_profile

Exporting our $PATH

export PATH=$PATH:/usr/local/bin

If you have a .bash_profile in your root directory you can skip this step, else we need a create a .bash_profile inside our home directory “ cd ~/  “

touch .bash_profile

What is a bash profile?

There is a hidden file in your Mac's user directory named .bash_profile. This file is loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command line interface.

To see the bash profile, on finder and navigate to Go -> Home -> double click .bash_profile

Please note, if you don’t see the .bash_profile, its because it’s a hidden file. To see all hidden file, press shift + cmd + . (dot)

Add the following header script to the .bash_profile and save

source ~/.profile

Installing composer

https://getcomposer.org/download/

Running the following scripts in your terminal at the root directory

cd ~/

This will take you to the root directory.

php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"

This will pull the composer set up files to your machine

php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd

586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"

This will verify the installer which is very important

php composer-setup.php

This will download the composer set up file into the current directory. But we don’t want that, we want to install composer globally. To do this we need to specify where to install composer.

php composer-setup.php –-install-dir=/usr/local/bin –-filename=composer

This will install composer to user/local/bin directory

To check if composer is working simply type composer on terminal.

All done, easy as that!

Please note if you get an error that says:

The defined install dir (/usr/local/bin) does not exist

Run the below script and enter your password

sudo mkdir/usr/local/bin

and then re-run

php composer-setup.php –-install-dir=/usr/local/bin –-filename=composer

That’s it

Installing Drush

Drush is a command line shell and Unix scripting interface for Drupal. At this point, installing Drush is a piece of cake. Simply type the following into the Terminal and hit “Enter”

Open your .bash_profile and paste the code below

source ~/.profile

export PATH=”$HOME/.composer/vendor/bin:$path”

Run

composer global require drush/drush:dev-master

Tatenda Chuma
Frontend and App Developer