Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Installing Drush with Composer

0 (0 Likes / 0 Dislikes)
Installing Drush with Composer with Joe Shindelar Drush is a command line shell and scripting interface for Drupal, a veritable Swiss Army Knife designed to make life easier for those who spend their working hours hacking away at the command prompt. The first step towards enlightenment is, of course, getting Drush installed, which admittedly can be a bit daunting if you don't have much experience installing command line applications. Or in some cases, even if you do have experience. In this tutorial, we'll walk through the recommended installation process for Drush by checking out the documentation available on the Drush website, and then using Composer to install Drush. Once it's installed, we'll confirm that Drush is running on our computer and working. Then, we'll wrap things up by talking about a couple of configuration options you might want to change once you've got it installed, and how to switch between versions of Drush. This tutorial uses the Composer command line tool. It assumes you've already got that installed and working. If you don't, check out our tutorial on getting started with Composer before you watch this one. By the end of this tutorial you should have a copy of Drush installed on your machine and be able to confirm that it's working. Let's go ahead and get started. So the first thing we want to do is just take a look at the existing documentation. This is a good resource for helping us, and there's also a handful of commands that we can copy and paste. The documentation exists on docs.drush.org. From there, I'm going to click on the Install link on the left hand side, which takes me to the installation instructions. The documentation here provides instructions for installing with Composer one Drush per project or one Drush for all projects. So the difference being if you want to have Drush bundled with your individual Drupal-based application, you could have a composer.json file that goes along with that which would allow you to have Drush installed for just that site. Or you could have Drush installed globally and use the Drush command to perform operations on any Drupal site on that host. We're going to do the latter. We're going to install one Drush for all projects. I recommend this for anyone that's installing Drush on a development environment. That way, you can just have one copy of Drush installed, but you can use it on all of the sites that you're working on. So the documentation here says basically, install Composer, which we've already got. And we can confirm that in a second. Confirm that Composer's bin directory is in your system path. This way, when we install Drush with Composer, and it installs the Drush executable, that executable will be located by Bash so we can run the command. We'll take a look at that. And then, we'll install Drush. We can install either Drush 6x, which is the current stable version, or Drush 7x, which is the current development version. The note here, Drush 7x is required if you want to use Drush with Drupal 8. We'll also talk a little bit about how we can switch back and forth between versions of Drush using Composer. Let's switch over to our command line. So just to confirm that we've got Composer installed, I'm going to run the which composer, which composer.phar command, and that tells me that I do indeed have Composer installed. It's located in the usr/local/bin. So I can find it. I'll be able to run commands with Composer. The other thing that the documentation wanted us to do was to make sure that we've got the Composer vendor bin directory in our path directory. So I can do that by running the command echo $PATH. When I run that, this prints out the entire content of my $PATH variable, which is where my current shell will look for scripts or applications that it can execute. You can see the first line here is Users/joe/.composer/vendor/bin, so anything that's install by Composer into that bin directory will be available for me to execute within my terminal. If that's not the case for you, or it's not already there, there's a couple of places to look. First thing you could try is if we switch back to the documentation. There's this handy command right here, sed -i, and then a bunch of command line stuff. What this command does is it attempts to add this string of text, PATH = $HOME, path to the Composer bin directory, to the $PATH variable as declared inside of your user's .bashrc file. If I switch back to my terminal, in my case, I've actually got it in my profile file. But if I open my .profile file, which is essentially the equivalent of a .bashrc file, you can see right up here at the top, I have that equivalent line. Export PATH equals, and then this path to my .composer/vendor/bin. It's prepending Composer's binary directory to the already existing path for my shell. Great, so that's all in place. That's the thing to make sure, though. Composer's bin directory is accessible from your shell. Once you've done that, let's take a look at having Composer find out some more information about Drush and the various versions of Drush that we can install. So I can run this command, composer show drush/drush. So I'm using the Composer show command, and I'm telling it to show me information about the Drush package from the Drush vendor. Says, there's nothing found locally, but it's going to go off and search through Packagist to see if it can find anything there. It takes a couple of seconds, but then it returns information about what Composer knows about the Drush package. So sure enough, there's the package name. A little description of what Drush is. The most important part here that we're after is this. All of the versions that are available-- that way, we can know what's available for us to install or switch between. So those are the versions that are available here. dev-master, these different variations of 7 dot versions, and so forth. It also gives you a little additional information about any other packages that are required by Drush. So this shows us that Drush is going to require PHP greater than 5.3, and these other couple of Composer libraries. Which, when we install Drush with Composer, if our system doesn't have, say, for example symfony/yaml, it'll go ahead and grab that and install the dependencies, as well. For more information about the different versions of Drush, we can again take a look at the documentation. This is listed up here at the top of the page. So the different Drush versions, then the compatible Drupal versions. So this says Drush 7, or the master version, is compatible with Drupal 6, 7, and 8, requires PHP 5.3. Drush 6 is compatible with Drupal 6 and 7, so forth. I'd recommend installing either Drush 7 or Drush 6. I actually tend to use Drush 7 most of the time. It is a development version, so you'll occasionally run into bugs and need to switch to the 6x version, but for the most part, I find it works just fine to use Drush 7. So let's go ahead and install it. In my terminal, in order to install Drush, so I want to install Drush 7, or the master version. So I've got dev-master here, like so. I'm going to go ahead, and I'm going to use Composer. And I'm going to say composer.phar global require drush/drush dev-master. The command "require" here is a little bit of a misnomer. I would have expected this to be something like "install," for example. But what this will do is say Composer, globally on my system, requires the Drush package dev-hyphen master version. In effect, it's going to download and install the Drush binaries into the appropriate place. So I'll go ahead and run that. Once that command has completed, in my case, you can see here it says nothing to install or update. It's because I've actually already got Drush installed. But what this would have told you is that this is what was just installed. We can confirm that Drush is installed by navigating to our composer/vendor/.bin directory and verifying that it's there. So I could say cd vendor/bin, like so. And if I list the contents of that, you can see, indeed, that Drush is installed inside of the composer/vendor/bin directory. Great. So you should see something similar after having run the Composer global require command. At this point, you've got Drush installed. You might need to close and reopen your terminal so that it finds the new packages that were added in that bin directory. Otherwise, you should be able to go ahead and run which drush. And it'll tell you which Drush application you've got installed. So the location here is, just as we confirmed, .composer/vendor/bin/drush. That's because we had added this part here previously to our PATH variable. You can confirm that it works by running commands like Drush --version to have it show you which version of Drush you've got installed. Or just running the command Drush with no additional arguments. And it should output a big list of the arguments that you could have passed some documentation of sorts to the Drush command. We'll cover all these commands in a different tutorial. So clear that. So now, we've got the Drush 7 or master version of Drush installed. But what if we wanted to switch to another version? That's a pretty easy thing to do with Composer. So first off, I'll say drush --version to see what I've got. I've got 7.0-dev. If I want to know what other versions are available, I can do composer.phar show drush/drush. We looked at this before. This command's going to give us information about all of the available versions. So here, I can see the other versions that I could install. Another thing to note is you can actually use shorthand in these versions, too. So if I just wanted the latest version of Drush 6, I could use 6.*, 6.* instead of having to specify a specific version. You can also see this command that we're about to execute back in the Drush documentation. So on docs.drush.org, I'm just going to run this command here, composer global require-- this is really similar to what we did before. Back in my terminal, I'll run that. And I'll call it composer.phar, like so. What that command did was it actually made some changes to the global composer.json file. And in order to have those reflected, we'll need to run Composer global update, as well. I can take a look at the composer.json for my global install. So in the .composer directory, there's a composer.json file. And if I open that up, you can see here the two packages that I've got installed globally, the Drupal Coder package, and then this Drush/Drush. And it's saying right there which version. When I executed the composer.phar global require drush version, it updated that line here. In order to have that reflected, I'm going to run composer.phar global update to update all of my global packages. So what it's going to do is read in that JSON file, recognize that there's a different version of Drush that it needs to use, and update, if you will, to that version. Now, when I run drush --version, I should see that it's outputting the latest version of Drush 6. Great, so really easy to switch between versions of Drush using the composer global require command, just like we did to install Drush in the first place. Again, that command looks like this. So global require, vendor, package, colon, the version of the package that you want to install. So we've got Drush dev-master is the version that we want to install. A couple of other things to note about installing Drush, especially if you're using an all-in-one server system like MAMP or WAMP or any of those other kind of bundled development stacks, you want to make sure that you have Drush run at the command line using the PHP that's bundled with, for example, MAMP, rather than the PHP that's bundled with OS X. So in order to do that, you'll need to ensure that that PHP is also in your $PATH, as well. You can type, which php. And that should tell you which PHP binary is being used at the command line. So in my case, it's the PHP 5.4.10 that ships with MAMP. Drush makes use of a variable named DRUSH_PHP, an environment variable. And if I wanted to, I might do something like update my Bash profile and add something like this to it. I could say export DRUSH_PHP and point that directly at the PHP binary that I wanted to use. So if for some reason I wanted Drush to use something other than the PHP that was specified in my path, I could do that here. So DRUSH_PHP is an environment variable. Drush will read that in if it exists and make use of the PHP binary located there. I'm not going to save that, though, because mine's already working. And then clear. Another thing that I want to point out is that if we switch back to the documentation, this is all focused on installing Drush with Composer, the command line tool, and really running Drush within a Linux or Unix based environment. There is really, really good support for Drush for any of the Unix based environments or Linux based. The support for Windows, however, is not perfect. It's getting a lot better, and there is documentation here about how to install it for Windows. There's this Windows ZIP package, which you can go ahead and give a try. It'll use an MSI installer and attempt to install Drush and all the necessary dependencies within your Windows environment. It mostly works. There's a few things that are a little bit broken. That is a bit beyond the scope of this tutorial, though. So I'm just going to leave you at that. If you're using Windows for a development environment, take a look at this ZIP package installer. So that's installing Drush with Composer. Just for a quick recap, what we covered in this tutorial was we took a look at the documentation on docs.drush.org. Specifically, we looked at the installation documentation. And then, we looked at using the Composer command line tool in order to install Drush and to switch between different versions of Drush. Finally, we confirmed that Drush was working by executing a couple of basic commands, checking which version we had installed, getting a list of the Drush help, and then installing a different version of Drush and making sure those commands all still worked. I hope this helps you along your way to getting Drush installed on your local host and making use of it in your day-to-day development work.

Video Details

Duration: 15 minutes and 44 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 40
Posted by: drupalizeme on Feb 3, 2015

Installing Drush with Composer

Caption and Translate

    Sign In/Register for Dotsub to translate this video.