Some Composer Drush Tricks

Submitted by Mile23 on Fri, 03/07/2014 - 17:58

Drush and Composer, Happy Pals

OK, so you like Drush. You might have it installed using PEAR. You don't know much about Composer, so you're not sure what to make of it.

Well, read on, and find out how easy this stuff really is.

This article focuses on Mac usage, but also applies to any Unix-flavor pretty much. Not so sure about Windows. You folks can fend for yourself, but maybe help out in comments if you feel so inclined.

Part 1: Set Up Your Shell

You have: A Mac (or other unix variant), git, an open terminal window, and the ability type which php and get meaningful output.

We're going to put Drush and some other stuff in a 'binary' directory in our home folder, and we're going to do it by typing. No mouse clicks involved. You will be CLI ninja!

First we'll make the directory.

mkdir ~/bin

You could call it anything, especially if there's a bin/ already there. Suggested replacement names: phptools/, drupalstuff/, and so forth.

cd into your new directory:

cd ~/bin

Now, find the path to your new directory:

pwd

pwd tells you the full path to the directory you're currently in. It probably says /Users/your_username/bin.

Now we edit our .profile file to include this directory in PATH. If you know what that means, skip to the next paragraph. You have a dotfile in your home directory called .profile. It makes the magic incantation of export PATH=[some stuff] so that bash knows how to find commands when you type them. Let's tell it about our new directory.

Open .profile in your favorite text editor, or still at the command line, type: nano ~/.profile.

Find the line that says export PATH=[stuff] and edit it so it says:

export PATH=/Users/your_username/bin/:[stuff]

Sharp-eyed readers will realize we just added the same path that pwd told us earlier, with a colon between the different paths in PATH.

Exit nano with something like these keypresses:

  1. ctrl-X
  2. y for yes I'd like to save
  3. return for the same file name.

Now you can either quit the terminal and start it back up again, or do this: source ~/.profile.

You've now set up the shell. You shouldn't have to do this again for a while.

Part 2: The Drush and Composer Part

Let's install Drush already. As a side-effect, we'll also install Composer in a global location.

Make sure you're in the bin/ directory you just created:

cd ~/bin

Let's install Composer:

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

Now let's give it a nicer name:

mv composer.phar composer

Test by typing which composer. There. That was terribly difficult.

Now we can use Composer to create a composer.json file. Like this:

composer init

This will walk you through some interactive stuff about the little project we're creating here, and most of the defaults are fine. However, when it asks for dependencies and dev dependencies, say 'no.' Like this:

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

Finally it will ask you to confirm generation, and you can just hit return.

If you ls and look at the directory, you'll see there's now a file called composer.json. This is the file Composer uses to figure out what goes where within that directory.

One of the things Composer can do is put 'binary' files where you specify. For instance, if you were trying to install Drush, you could tell Composer to put the Drush binaries in a special place. Which sounds a lot like what we're doing here. So:

composer config bin-dir .

Pay special attention to the . at the end of that. It means we want Composer to put all binaries in the same directory as the composer.json file.

And now for the magic moment:

composer require drush/drush:*

Composer will grind for a little bit and download a bunch of stuff for you. And when it's done, you are the proud owner of a Drush installation.

Do an ls and you'll see that Drush is right there in bin/, waiting for bash to find it. So do that:

which drush

Neeto?

Part 3: The Update Part

Let's pretend you're the kind of person who updates their software all the time. I mean, if you're not. Because you are, right?

So now if we want to update Drush, we do this:

cd ~/bin
composer update

And that's it.

The way this is set up, you'll get the latest version of Drush listed on packagist.org, which in effect means dev-master. If you want to limit the stability of the branch you want, you can add this requirement in your composer.json file.

You can learn all about that from Composer's excellent documentation page on the matter: https://getcomposer.org/doc/04-schema.md#minimum-stability

And.... Done.