Drupal 8 with Drush

Installing Drush for Drupal Development


=> assumes composer is installed

=> assumes Mac

=> assume you are ssh’d into your vagrant machine that hosts your website or a remote linux server

First, install drush binaries – go to any directory on your server, e.g. $HOME

php -r “readfile(‘https://s3.amazonaws.com/files.drush.org/drush.phar’);” > drush

change up one directory for permissions reasons (see http://drupal.stackexchange.com/questions/201060/does-drush-need-to-be-installed-as-root)

php <whateverdiryouarein>/drush core-status (this should output some stuff if it’s installed right)

chmod +x <whateverdiryouarein>/drush

sudo mv <whateverdiryouarein>/drush /usr/local/bin

drush init

Next, follow instructions at http://docs.drush.org/en/7.x/install/

only use

composer global require drush/drush:8.* instead of composer global require drush/drush:dev-master

Go to your webroot to type the command (composer global require drush/drush:8.*) – for example, mine is /var/www/somedrupal8site and my docroot is /var/www/somedrupal8site/docroot

Next go to your site’s docroot and use the command

drush status

in all the stuff that spits out, you should see your database name/connection – if so, it’s connected to your drupal – yay

from here,

drush uli (generates a one time login in link)

drush upwd —password=“somepwstring” “admin” (reset admin account pw)

Command Line Output

[05:32 PM]-[vagrant@local]-[~]

$ php -r “readfile(‘https://s3.amazonaws.com/files.drush.org/drush.phar’);” > drush

[05:33 PM]-[vagrant@local]-[~]

$ php drush core-status

PHP Warning:  pcntl_exec(): Error has occurred: (errno 13) Permission denied in phar:///home/vagrant/drush/includes/startup.inc on line 359

Warning: pcntl_exec(): Error has occurred: (errno 13) Permission denied in phar:///home/vagrant/drush/includes/startup.inc on line 359

Error has occurred executing the Drush script found at /home/vagrant/drush

(errno 13) Permission denied

[05:33 PM]-[vagrant@local]-[~]

$ sudo php drush core-status

PHP Warning:  pcntl_exec(): Error has occurred: (errno 13) Permission denied in phar:///home/vagrant/drush/includes/startup.inc on line 359

Warning: pcntl_exec(): Error has occurred: (errno 13) Permission denied in phar:///home/vagrant/drush/includes/startup.inc on line 359

Error has occurred executing the Drush script found at /home/vagrant/drush

(errno 13) Permission denied

[05:33 PM]-[vagrant@local]-[~]

$ cd ..

[05:34 PM]-[vagrant@local]-[/home]

$ ls


[05:34 PM]-[vagrant@local]-[/home]

$ php vagrant/drush core-status

PHP configuration      :  /etc/php/5.6/cli/php.ini

PHP OS                 :  Linux                    

Drush script           :  /home/vagrant/drush      

Drush version          :  8.1.3                    

Drush temp directory   :  /tmp                     

Drush configuration    :                           

Drush alias files      :                           

[05:34 PM]-[vagrant@local]-[/home]

$ chmod +x vagrant/drush

[05:35 PM]-[vagrant@local]-[/home]

$ sudo mv vagrant/drush /usr/local/bin

[05:36 PM]-[vagrant@local]-[/home]

$ drush init

Copied example Drush configuration file to                           [ok]


Copied example Drush bash configuration file to                      [ok]


Copied Drush completion file to                                      [ok]


Copied example Drush prompt file to                                  [ok]


# Include Drush bash customizations.

if [ -f “/home/vagrant/.drush/drush.bashrc” ] ; then

  source /home/vagrant/.drush/drush.bashrc


# Include Drush completion.

if [ -f “/home/vagrant/.drush/drush.complete.sh” ] ; then

  source /home/vagrant/.drush/drush.complete.sh


# Include Drush prompt customizations.

if [ -f “/home/vagrant/.drush/drush.prompt.sh” ] ; then

  source /home/vagrant/.drush/drush.prompt.sh


Append the above code to /home/vagrant/.bashrc? (y/n): y

Updated bash configuration file /home/vagrant/.bashrc                [ok]

Start a new shell in order to experience the improvements (e.g.      [ok]


[05:36 PM]-[vagrant@local]-[/home]

$ cd /var/www/safro

[05:44 PM]-[vagrant@local]-[/var/www/safro]

$ ls

awi_d6/  awi_d8/  drupal/

[05:44 PM]-[vagrant@local]-[/var/www/safro]

$ cd awi_d8

[05:44 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ ls

composer.json*  composer.lock*  database/  docroot/

[05:44 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ composer global require drush/drush:dev-master

Changed current directory to /home/vagrant/.composer

./composer.json has been created

Loading composer repositories with package information

Updating dependencies (including require-dev)

Your requirements could not be resolved to an installable set of packages.

  Problem 1

    – Installation request for drush/drush dev-master -> satisfiable by drush/drush[dev-master].

    – drush/drush dev-master requires consolidation/robo dev-master -> satisfiable by consolidation/robo[dev-master] but these conflict with your requirements or minimum-stability.

Installation failed, deleting ./composer.json.

[05:45 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ composer global require drush/drush:8.*

Changed current directory to /home/vagrant/.composer

./composer.json has been created

Loading composer repositories with package information

Updating dependencies (including require-dev)

  – Installing pear/console_table (v1.3.0)

    Downloading: 100%         

  – Installing symfony/finder (v2.8.9)

    Downloading: 100%         

  – Installing symfony/polyfill-mbstring (v1.2.0)

    Downloading: 100%         

  – Installing symfony/console (v2.8.9)

    Downloading: 100%         

  – Installing symfony/var-dumper (v2.8.9)

    Downloading: 100%         

  – Installing symfony/yaml (v2.8.9)

    Loading from cache

  – Installing consolidation/output-formatters (1.0.0)

    Downloading: 100%         

  – Installing psr/log (1.0.0)

    Loading from cache

  – Installing phpdocumentor/reflection-docblock (2.0.4)

    Downloading: 100%         

  – Installing consolidation/annotated-command (1.0.1)

    Downloading: 100%         

  – Installing jakub-onderka/php-console-color (0.1)

    Downloading: 100%         

  – Installing jakub-onderka/php-console-highlighter (v0.3.2)

    Downloading: 100%         

  – Installing dnoegel/php-xdg-base-dir (0.1)

    Downloading: 100%         

  – Installing nikic/php-parser (v2.1.0)

    Downloading: 100%         

  – Installing psy/psysh (v0.7.2)

    Downloading: 100%         

  – Installing drush/drush (8.1.3)

    Downloading: 100%         

pear/console_table suggests installing pear/Console_Color2 (>=0.1.2)

symfony/console suggests installing symfony/event-dispatcher ()

symfony/console suggests installing symfony/process ()

symfony/var-dumper suggests installing ext-symfony_debug ()

phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)

phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)

psy/psysh suggests installing ext-pdo-sqlite (The doc command requires SQLite to work.)

drush/drush suggests installing drush/config-extra (Provides configuration workflow commands, such as config-merge.)

Writing lock file

Generating autoload files

[05:46 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ vi ~/.bash_profile

[05:47 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ drush status

PHP configuration      :  /etc/php/5.6/cli/php.ini

PHP OS                 :  Linux                    

Drush script           :  /usr/local/bin/drush     

Drush version          :  8.1.3                    

Drush temp directory   :  /tmp                     

Drush configuration    :                           

Drush alias files      :                           

[05:49 PM]-[vagrant@local]-[/var/www/safro/awi_d8]-[git master]

$ cd docroot

[05:50 PM]-[vagrant@local]-[/var/www/safro/awi_d8/docroot]-[git master]

$ drush status

Drupal version                  :  8.1.7                                       

Site URI                        :  http://default                              

Database driver                 :  mysql                                       

Database hostname               :  localhost                                   

Database port                   :  3306                                        

Database username               :  root                                        

Database name                   :  awi_d8_dev                                  

Drupal bootstrap                :  Successful                                  

Drupal user                     :                                              

Default theme                   :  awi                                         

Administration theme            :  seven                                       

PHP configuration               :  /etc/php/5.6/cli/php.ini                    

PHP OS                          :  Linux                                       

Drush script                    :  /usr/local/bin/drush                        

Drush version                   :  8.1.3                                       

Drush temp directory            :  /tmp                                        

Drush configuration             :                                              

Drush alias files               :                                              

Install profile                 :  standard                                    

Drupal root                     :  /var/www/safro/awi_d8/docroot               

Drupal Settings File            :  sites/default/settings.php                  

Site path                       :  sites/default                               

File directory path             :  sites/default/files                         

Temporary file directory path   :  C:\xampp\tmp                                

Sync config path                :  sites/default/files/config_KzpmwHqnglNTnzMK



[05:51 PM]-[vagrant@local]-[/var/www/safro/awi_d8/docroot]-[git master]

$ drush upwd –password=”jennifer” “admin”

Changed password for admin                                           [success]

[06:28 PM]-[vagrant@local]-[/var/www/safro/awi_d8/docroot]-[git master]

$ drush uli


[06:28 PM]-[vagrant@local]-[/var/www/safro/awi_d8/docroot]-[git master]


Q: A custom frontend controller will extend which one of the following classes?

All Action Controller need Mage_Core_Controller_Front_Action as an ancestor.

The best example of this in action is a simple HelloWorld module that is configured with routes and has a Create Action Controller for the routes.

Directory Structure:


We need a configuration file (PATH: app/code/local/nodwell/Helloworld/etc/config.xml):


and a modules file to activate our module (PATH: app/etc/modules/nodwell_Helloworld.xml):


Now, the module exists, and we can begin to add the code to make it do stuff. We need to configure a route in the config.xml. The route turns a URL into an Action Controller and a method. In our case, it will act on URLs that start with /helloworld, as in http://magento.nodwell.net/helloworld/*. So, add a frontend routers section to the config.xml:


Now, we create the actual Action Controller (PATH: app/code/local/nodwell/Helloworld/controllers/IndexController.php):

class nodwell_Helloworld_IndexController extends Mage_Core_Controller_Front_Action {        
    public function indexAction() {
        echo 'Hello Index!';

Code Generation in Magento 2

When and Why Does Code Get Created in /var/generation?


Code generation can be triggered in two ways:

1. On the fly – when the system tries to autoload a class, if it doesn’t find it, it generates it. (slower)

delete the MAGENTO_ROOT/var/generation directory

2. Command-line – process goes through the system, inspects the code, and generates the necessary classes it might need. (speedier)

run the magento setup:di:compile command from terminal on web server

Code Generation did not exist in Magento 1. In Magento 2, code generation is deployed to support a number of core concepts.

The system generates several class types – the 3 most important are Factories, Proxies, and Plugins.


Factories are used to instantiate objects that cannot be injected automatically, objects that it doesn’t make sense to create with a generic mechanism, such as those which contain data from the database.

  1. Developer declares a dependency on factory in constructor ($productFactory)
  2. Object manager injects this dependency
  3. Developer can access create() method (the only method in factory object) to create as many Product instances as he wants.

The purpose of factories is to delegate object instantiation to object managers.


Magento 2 uses the object manager to create all the dependencies and allows only one type of dependency injection: constructor injection. Thus, you cannot instantiate an object without passing all the dependencies as they are required. Proxies allow you to pass in optional dependencies.

Developer never touches PHP files to use proxy, only inside the di.xml

Whenever any proxy method is called for the first time, the original instance gets created with all its dependencies. The purpose of a proxy is to delay creation of an instance (and its dependencies) until the very first usage.

Plugins (Interceptors)

Plugins are the primary customization mechanisms for Magento 2 which replace class rewrites. Plugins allow you to hook in and do something before, after or around any public method of the application.

  1. Developer writes a plugin class depending on the requirements
  2. Developer registers a plugin in di.xml

The system generates the interceptor class. The generator tool will generate only the methods that you rewrite.

Installing Magento 2 with Composer

If you’re looking for something amusing to do some rainy afternoon, don’t install Magento 2. 😉

Before You Start

Before you start your installation, make sure your web server meets the minimum system requirements:

  • A Linux x86-64 operating system
  • 2GB of RAM
  • Composer (latest stable version)
  • Apache 2.2 or 2.4 with mod_rewrite enabled or nginx 1.8 or latest mainline version
  • MySQL 5.6 (MariaDB and Percona are compatible)
  • PHP 5.6x, 5.5.22 or higher, 7.02 up to 7.1.0 but not 7.0.5 – required PHP extensions:
    • bc-math
    • curl
    • gd, ImageMagick 6.3.7 or both
    • intl
    • mbstring
    • mcrypt
    • mhash
    • openssl
    • PDO/MySQL
    • SimpleXML
    • soap
    • xml
    • xsl
    • zip

For more information, see the magento dev docs.

Magento Access Keys

My Account
My Account Marketplace Tab – Click on My Access Keys link

Next, you need to log in to your account on the Magento Marketplace and request a key pair to authenticate the install with. It’s free. Sign up.

Your Magento Access Keys Screen
Your Magento Access Keys Screen

On the next screen, click the button to Create a New Access Key and give it any name you like. Leave this window open because you are going to need the Public Key and Private Key that it generates when you create a new access key.

For more information, see the magento dev docs.

Install Via Composer

In a terminal, on the command line, in the root of your magento 2 directory, enter the following:

composer create-project --repository-url=https://repo.magento.com/ magento/project-community-edition

It is going to prompt you for a username. Enter the Public Key you got from the Magento Marketplace. It will prompt you for a password. Enter the Private Key you got from the Magento Marketplace.

Install Via Composer
Install Via Composer

Install Sample Data

Enter the following two commands in the terminal from your magento2 root directory:

php bin/magento sampledata:deploy
php bin/magento setup:upgrade

If you are really, really inordinately lucky, you now have a working magento 2 site with sample data installed. Most likely, though, you got some errors when you tried to install the sample data, and you’re stuck.

If you try to install sample data, and get a list of packages that “could not be found in any version,” don’t panic, there’s a GIT work around.

Install Sample Data via Git

Since the above method rarely works for anyone, there is a way to install the sample data from github. Create a directory anywhere outside your web root and clone the GitHub repo using this command:

git clone https://github.com/magento/magento2-sample-data.git
Clone the repo
Clone the repo

Next, run the installer script

php -f /dev/tools/build-sample-data.php -- --ce-source="/var/www/"

After that finishes, go to your magento root, and run the updater:

bin/magento setup:upgrade

and you will see the sample data start to be installed. This process will take quite a while.

Almost Finished

Clear the cache (and fix file permissions if needed) and you should see all the sample data in the front and back ends now.
Again, if you’re lucky, your store is ready to rock and roll.
You might have an issue where your css files are not loading.

CSS is missing
CSS is missing

From your web root, recreate the static content:

php bin/magento setup:static-content:deploy

Then, reindex

php bin/magento indexer:reindex
Reindexing Magento
Reindexing Magento

Check that the folder permissions are 755 in the pub folder and its subfolders and delete all the files and folders in var/cache, var/page_cache, and var/sessions and go reload your site.

Magento 2 Sample Data
Magento 2 Sample Data