How to Install Drupal with Composer? Practical Guide

Nowadays, the recommended way to install Drupal is via Composer. It can be used to both – Drupal core and the contributed modules or themes installation. Check out our short but profound step-by-step Drupal Composer installation guide.

Drupal installation with Composer

Drupal Installation Requirements

If you have decided to build your website on Drupal, you have to prepare accurate conditions for CMS installation. The process demands two basic things – a web server and a database server. For the most popular operating systems, the basic stack calls AMP, which is an acronym for:

  • Apache (or another web server, e.g., Nginx),
  • MySql (or another database server, e.g., MariaDB),
  • PHP (in a version supported by a particular Drupal release).

If you want to install its core correctly, you have to meet the particular Drupal version’s requirements.

Why Should You Install Drupal 8 or 9 with Composer?

Composer is a tool that helps manage PHP dependencies. Why is it a convenient way to install Drupal 8 or 9? First of all, it enables easy and transparent Drupal and dependencies management – modules, libraries, themes, etc. Moreover, it ensures that you have just installed the latest version of each module or theme. There is also the official project which eases Drupal installation with Composer. It contains all the essential information needed for this process. Indeed, those are just a few arguments why Composer is highly recommended for Drupal installation – we can write down more, but it is not the point of this text.

Drupal Installation with Composer – Step-by-Step

Before Composer, the most common way to install Drupal was via Drush or Drupal Console. Now, Composer manages the download process by itself. Beneath, you will find the essential steps on how to install Drupal using Composer.

1. Install Composer

The first and most important thing is Composer installation on your local machine. You can find detailed instructions on how to do this on different operating systems, e.g., Linux, macOS, or Windows there.

2. Download Drupal Core Using Composer

To download Drupal 8 or 9 core, you should use the composer template drupal/recommended-project. Thus, you will be sure that the core dependencies are in line with the official release. It is worth noticing that since Drupal 8.8.0 the template changed from drupal-composer/drupal-project to the actual drupal/recommended-project.

You should use following command to install Drupal 8 or 9 core:

composer create-project drupal/recommended-project my_site_name_dir

This command will download the drupal/recommended-project package directly to the my_site_name_dir. It automatically activates the composer install command, which downloads Drupal core and its dependencies in the latest stable versions.

In my_site_name_dir you can find all files unavailable for the web server and those which should be placed out of the webroot.

3. Download a Specific Drupal Core Version via Composer

The abovementioned command will download the latest Drupal version e.g., 9.2.0. If you want to download another one, e.g., Drupal 8.9.1, you have to use a different command:

composer create-project drupal/recommended-project:8.9.1 my_site_name_dir

4. Download Contributed Modules and Themes Using Composer

You have already installed Drupal core. Now it is high time to expand its possibilities via modules or themes. You can do this entirely using Composer. To download the contributed module or theme with its all external dependencies, you should run the following command at the root of your Drupal site:

composer require drupal/<modulename>

Let’s assume it will be a Pathauto module. Then the command will look like this:

composer require drupal/pathauto

Composer will update the composer.json by adding Pathauto to the requirements.

5. Download a Specific Version of Contributed Modules or Themes

Similarly as in the Drupal core case, you can point out which module or theme version you want to install. Use the following command to make this:

composer require 'drupal/<modulename>:<version>'

For example:

composer require 'drupal/pathauto:^1.8'

6. Changing Locations of Packages

You can define the locations where the downloaded packages should be installed. To make this happen, you have to modify „installer-paths” in composer.json. Use this configuration:

"extra": {
    "installer-paths": {
        "core": ["type:drupal-core"],
        "libraries/{$name}": ["type:drupal-library"],
        "modules/contrib/{$name}": ["type:drupal-module"],
        "profiles/contrib/{$name}": ["type:drupal-profile"],
        "themes/contrib/{$name}": ["type:drupal-theme"],
        "drush/{$name}": ["type:drupal-drush"],
        "modules/custom/{$name}": ["type:drupal-custom-module"],
        "themes/custom/{$name}": ["type:drupal-custom-theme"]
    }
}

How Can you Install Drupal 8 or 9 Without Composer?

As previously mentioned, Composer is a recommended way to install Drupal. However, if, for some reason, you do not want or cannot use Composer, there are other solutions. For example, you can use Drush and install Drupal from the command line.

In brief, you have to add this command to your project:

composer require drush/drush

and then use drush site:install.

Managing Drupal Site with Composer

Composer is not only dedicated to installing Drupal 8 or 9. You can easily use it to manage Drupal core updates and further modules, themes, and library updates. If you are seeking a reliable source of knowledge regarding this area, check the following links.

  1. Updating Drupal Core with Composer
  2. Updating modules and themes via Composer

Drupal 8 and 9 Installation with Composer – Summary

Composer vastly speeds up and eases works connected to installing Drupal 8 or 9. You no longer have to download the CMS core manually, and what is more, you can use the tool to manage your site and its dependencies in the future. Such a solution provides comfort and security for each Drupal site owner.

Do you need help with setting up a Drupal site?

Contact us