Since Drush 11, the only way to install drush is through PHP Composer. It should be very easy, but there is a dependency issue currently exist for drush 11 standalone installation.
1. Prerequisite
Install PHP Composer and understand how it works.
2. Install Drush
2.1 Install Drush After Drupal
Make sure your drupal project was installed by Composer. Navigate to the directory containing composer.json, composer.lock and the directories of vendor and web.
composer require drush/drush
2.2 Install Drush Before Drupal
In the case that you need to have drush before drupal, navigate to the desired project directory.
composer require symfony/http-kernel -W
composer require drush/drush
As of Mar 2023, the latest drush 11 misses its dependency on symfony. Please refer to this discussion, and no fix is provided yet. If you do not require sympony/http-kernel library, you’ll bump into the following error message.
$ ./vendor/bin/drush
PHP Fatal error: Uncaught Error: Class "Symfony\Component\HttpKernel\Kernel" not found in /home/ubuntu/test3/vendor/drush/drush/src/Preflight/Preflight.php:209
Stack trace:
#0 /home/ubuntu/test3/vendor/drush/drush/src/Runtime/Runtime.php(84): Drush\Preflight\Preflight->loadSymfonyCompatabilityAutoloader()
#1 /home/ubuntu/test3/vendor/drush/drush/src/Runtime/Runtime.php(51): Drush\Runtime\Runtime->doRun()
#2 /home/ubuntu/test3/vendor/drush/drush/drush.php(77): Drush\Runtime\Runtime->run()
#3 /home/ubuntu/test3/vendor/drush/drush/drush(4): require('...')
#4 /home/ubuntu/test3/vendor/bin/drush(120): include('...')
#5 {main}
thrown in /home/ubuntu/test3/vendor/drush/drush/src/Preflight/Preflight.php on line 209
3. Verify Drush Installation
By now, Composoer shall install the source code of drush to ./vendor/drush/drush, and its binary to ./vendor/bin/drush. You may Use the following command to verify the installation.
./vendor/bin/drush -V
You shall be able to see its correct version.
4. Put Drush in PATH?
Composer, Drupal, and Drush are all designed to work at per project level so that you have greater flexibility to work on multiple projects in the same system. If you only maintain one project, you can add ./vendor/bin/drush in front of your current PATH. That make the drush the only copy invoked.
If you maintain multiple projects with drush install with each project, you may choose to use drush launcher, which is globally accessible while invoke different drush binary according to your working directory.
wget -O drush.phar https://github.com/drush-ops/drush-launcher/releases/latest/download/drush.phar
chmod +x drush.phar
sudo mv drush.phar /usr/local/bin/drush
Now, if you invoke drush in a random directory, it’ll tell you this.
ubuntu@myhost:~$ drush
The Drush launcher could not find a Drupal site to operate on. Please do *one* of the following:
- Navigate to any where within your Drupal project and try again.
- Add --root=/path/to/drupal so Drush knows where your site is located.
Please note, drush launcher only works when drush and drupal are both correctly installed. It does not work for drush without drupal.
5. Conclusion
Drush 11 needs sympony/http-kernel whether it’s provided with drupal or you manually resolve it. For further reading, you may refer to the Drush 11 official document. If you have any thoughts, please leave a comment below.