Install (download) Drupal 8 with composer

Today composer is the recommended approach to install (o more precisely to download)  Drupal 8. This is true for the core but also for contributed modules and themes.

So now, to start a new Drupal 8 project, we need to download it via composer and not as we did before with drush or drupal console. Of course, we'll still use drush or drupal console to install contrib modules or themes, but not for downloading them.

The main benefit of using composer is that it allows us to systematically manage a sprawling list of dependencies (and their subsidiary dependencies) and ensure that the right versions for each package are used or updated.

An official project composer template has been created for installing Drupal with composer. We will create our project directly using this template, which is also available on Packagist.

Installing composer

First you need to install composer. Please see Getting Started on getcomposer.org to install Composer itself.

Download Drupal core using Composer

To create a new project based on the official composer template we can run the following Composer command:

composer create-project drupal-composer/drupal-project:8.x-dev my_project_name_dir --stability dev --no-interaction

Don't forget to change 'my_project_name_dir' with the name of the directory where you want to install Drupal.

This will download the drupal-composer/drupal-project project from Packagist into a folder named 'my_project_name_dir'. It also automatically executes composer install which will download Drupal 8 and all its dependencies.

In fact, this create-project composer command is the equivalent of doing a git clone https://github.com/drupal-composer/drupal-project.git  my_project_name_dir followed by a composer install of the vendors.

# create-project is equivalent of a git clone and a composer install
git clone https://github.com/drupal-composer/drupal-project.git my_project_name_dir
composer install

What does the template do?

When installing the given composer.json some tasks are taken care of:

  • Drupal will be installed in the web-directory.
  • Autoloader is implemented to use the generated composer autoloader in vendor/autoload.php, instead of the one provided by Drupal (web/vendor/autoload.php).
  • Modules (packages of type drupal-module) will be placed in web/modules/contrib/
  • Theme (packages of type drupal-theme) will be placed in web/themes/contrib/
  • Profiles (packages of type drupal-profile) will be placed in web/profiles/contrib/
  • Creates default writable versions of settings.php and services.yml.
  • Creates web/sites/default/files-directory.
  • Latest version of drush is installed locally for use at vendor/bin/drush.
  • Latest version of DrupalConsole (yes !!!) is installed locally for use at vendor/bin/drupal.
  • Creates environment variables based on your .env file. See .env.example.

Please note that Drupal will be installed in 'my_project_name_dir/web/'

The project template also comes with a .gitignore file that keeps Drupal core and all the contributed packages outside of Git, similar to the regular vendor/ packages. So based on an updated composer.json file, we can maintain a smaller Git repository and recreate our project any time. A lot of the benefits of Drush Make have now been incorporated into a Composer flow.

What can I do if I don't want the install Drupal in the web/ directory?

Imagine that we need to install Drupal in the docroot/ directory instead of the web/ directory.

In this case we'll do the following:

# 1. Download the project template with git
git clone https://github.com/drupal-composer/drupal-project.git my_project_name_dir

# 2. Change the 'installer-paths' in the 'extra' section of the new downloaded composer.json
cd my_project_name_dir
vi composer.json

# 3. Run composer install 
composer install

Install your project

Now that we have downloaded the Drupal project and all its dependencies, we can install our Drupal project by running the DrupalConsole command drupal site:install or go to your local website (localhost://) to install the site.