Hướng dẫn cài đặt Drush sử dụng Composer trên Mac OS X 10.9 Mavericks

Following on from our previous guide “Creating and using an public/private SSH key-pair in Mac OS X 10.9 Mavericks” in which we walked you through the process of setting up Git on Max OS X 10.9 Mavericks, we’re now going to look at installing Drush using Composer.

If you’re never heard of Drush before, the description provided on the  Drush GitHub repository offers a succinct and accurate explanation of what Drush is and the tools it makes available to you:

Drush is a command line shell and Unix scripting interface for Drupal. If you are unfamiliar with shell scripting, reviewing the documentation for your shell (e.g. man bash) or reading an online tutorial (e.g. search for "bash tutorial") will help you get the most out of Drush.

Drush core ships with lots of useful commands for interacting with code like modules/themes/profiles. Similarly, it runs update.php, executes sql queries and DB migrations, and misc utilities like run cron or clear cache.

Ultimately our goal in these series of guides is to give anyone both experienced and inexperienced alike the knowledge and skills required to configure their system so that an installation of Drupal can be run from it with a suite of tools available for them to use from the get-go.

Let’s begin.

Installing Composer

Rather than installing Drush manually, we’re going to let a tool called Composer do all the hard work for us. Composer is a tool for handing dependency management the full description for which can be found on the Composer site. In a nutshell though, the following sums up Composer well:

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

Let’s install Composer. Open a new instance of the Terminal by either navigating to the Applications folder within the finder followed by the Utilities sub-folder, or alternatively by pressing “Cmd+Space” and typing “Terminal” followed by the “Enter” key.

Once open, type the following command to ensure you’re in your home directory:

cd ~/

To download composer, type the following command into the Terminal and hit enter:

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

At this point you could type “~/composer.phar --help” into the command line and you’ll most likely get a list Composer help documentation, but we want to be able to access Composer simply by typing the command “composer”, so let’s move the *.phar file into our “/user/local/bin” directory.

Note: On a fresh installation of OS X 10.9, the “/usr” directory will very likely be empty. If this is the case, to enable us to successfully move Composer to “/usr/local/bin/composer” you’ll need to type the following two commands“sudo mkdir /usr/local” and “sudo mkdir /user/local/bin”. After typing the first command, you may be prompted for your system password. Simply enter it to continue.

Now we’re all set to move Composer. To do this, type the following command into the Terminal:

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

Note: If you get an error trying to move the file, prefix the command above with “sudo” and try again.

We should now be able to type “composer” into the terminal and get something other than an error returned. Try this out by typing “composer”and hitting enter.

Adding the Composer “bin” directory to our path

Once Drush is installed, we will be able to type“~/.composer/vendor/bin/drush” followed by the Drush command of our choice, but who wants to do that every time? 

To be enable the ability to type “drush” followed by our command we need to add Composer’s bin directory to our path.

In your home directory “~/“ type the command “nano ~/.bash_profile”and add the following line to the file that opens up:

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

Quit Nano, by pressing “Ctrl+X” and when asked if you wish to save the document type “Y” and hit “Enter”.

Finally we need to re-source our “.bash_profile” file by typing “source ~/.bash_profile” into the Terminal (alternatively you can quit and re-open the Terminal).

Installing Drush

At this point, installing Drush is a piece of cake. Simply type the following into the Terminal and hit “Enter”

composer global require drush/drush:dev-master

Drush should now be installed. To ensure it is, type “drush” into the command line and you should see a series of Drush help documentation.