Drupal 7 with Drush Loader and Non-Composer Project

Submitted by nigel on Tuesday 9th October 2018
drush status

I've just upgraded my bare-metal Linux server which host many of my Drupal projects. My main driver was to get it up to PHP 7.1. I also in that process I elected to replace the existing /usr/local/bin/drush command with the drush loader which automatically runs project-based drush commands installed by composer into the vendor directory. 

The problem with this approach of course is if you have Drupal 7 virtual hosts which were built without composer, then they would lose drush capability. Thankfully there is a simple workaround for this - by retrospectively adding a skeleton composer.json file to the project. 

The Current Situation
I change directory into one of my many D7 projects
$ cd /var/www/html/tube
$ 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.
This is the diagnostic you will receive if you attempt to run drush in a non-composer project.
The composer.json Skeleton
Now we have to add our composer skeleton to our project. So in your favourite editor add:
composer.json
{
    "require": {
        "drush/drush": "~8"
    },
    "extra": {
        "installer-paths": {
            ".": ["type:drupal-core"]
        }
    }
}
Most of this will be self-evident to composer users. The "drush/drush": "~8" means the version of drush should be kept at 8 with minor version increases allowed. The installer paths "." option basically means install the vendor directory that will house the drush binaries in the current directory. The reason is simple: I created this particular project long before child www or docroot directories became the norm.
Composer Install
Once the json file has been created, we are ready to go.
$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 20 installs, 0 updates, 0 removals
..
snipped
...
drush/drush suggests installing drush/config-extra (Provides configuration workflow commands, such as config-merge.)
Writing lock file
Generating autoload files
<code>
Vendor Directory
The vendor directory will now have been created and can be witnessed with
$ ls -las vendor
total 60
4 drwxrwxr-x 14 nigel www-data 4096 Oct  9 21:22 .
4 drwxrwxr-x 11 nigel www-data 4096 Oct  9 21:21 ..
4 -rw-rw-r--  1 nigel www-data  178 Oct  9 21:22 autoload.php
4 drwxrwxr-x  2 nigel www-data 4096 Oct  9 21:21 bin
4 drwxrwxr-x  2 nigel www-data 4096 Oct  9 21:21 composer
4 drwxrwxr-x  4 nigel www-data 4096 Oct  9 21:21 consolidation
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 dnoegel
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 drush
4 drwxrwxr-x  4 nigel www-data 4096 Oct  9 21:21 jakub-onderka
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 nikic
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 pear
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 psr
4 drwxrwxr-x  3 nigel www-data 4096 Oct  9 21:21 psy
4 drwxrwxr-x 10 nigel www-data 4096 Oct  9 21:21 symfony
4 drwxrwxr-x  4 nigel www-data 4096 Oct  9 21:21 webmozart
Running drush status
Ok so now we should be good to go. Let's try it out!
$ drush status
 Drupal version                  :  7.59                                            
 Site URI                        :  http://default                                  
 Database driver                 :  mysql                                           
 Database hostname               :  127.0.0.1                                       
 Database port                   :                                                  
 Database username               :  drupal                                          
 Database name                   :  xxxx                                            
 Database                        :  Connected                                       
 Drupal bootstrap                :  Successful                                      
 Drupal user                     :                                                  
 Default theme                   :  xxxx                                  
 Administration theme            :  seven                                           
 PHP executable                  :  /usr/bin/php                                    
 PHP configuration               :  /etc/php/7.0/cli/php.ini                        
 PHP OS                          :  Linux                                           
 Drush script                    :  /var/www/html/xxxx/vendor/drush/drush/drush.php 
 Drush version                   :  8.1.17                                          
 Drush temp directory            :  /tmp                                            
 Drush configuration             :                                                  
 Drush alias files               :                                                  
 Install profile                 :  standard                                        
 Drupal root                     :  /var/www/html/xxxx                              
 Drupal Settings File            :  sites/default/settings.php                      
 Site path                       :  sites/default                                   
 File directory path             :  sites/default/files                             
 Temporary file directory path   :  /tmp                    
Good to go! We are done here!