Tuesday 22 November 2022

Create Drupal 9 Project with Composer

 For Drupal 9, We should use "drupal/recommended-project" composer template. This will ensure that Drupal core dependencies are same as the official Drupal release.

Composer command is as below to create Drupal 9 project

composer create-project drupal/recommended-project first_drupal9_site

This command will create a project in "first_drupal9_site" and automatically will execute composer install to download latest stable version of Drupal and all its dependencies.

Now first_drupal9_site directory will have files as below.

1. composer.json (file)

2. composer.lock (file)

3. vendor (Directory)

4. web (Directory)

The web root will be "web" directory. Except "web" directory, all other files "composer.json, composer.lock, vendor" will not accessible to the web server. 


To install Drupal 9 site, now access your localhost and access web directory. Follow the steps as per installation asks. Once these steps will finish, your first Drupal 9 site will ready.


To install specific version, run below composer command.

composer create-project drupal/recommended-project:9.4.8 first_drupal9_site


To do modified installation

Before composer install execution, if I need to modify some of the properties of the downloaded composer.json then we have to use --no-install flag while running composer create-project

Example, if I want to rename the subdirectory "web" to something else. Follow below steps

  • Run command composer create-project --no-install drupal/recommended-project first_drupal_site 
  • Change directories to first_drupal_site and edit the composer.json file to suit your needs.

    For example, to change the sub-directory from 'web' to something else, the keys to modify are the 'extra' sub-keys 'web-root' (under 'drupal-scaffold') and 'installer-paths'.

  • Run composer install to download Drupal and all its dependencies. 
To download contributed module and contributed theme with composer follow below steps.
  • Run composer require drupal/module_name. eg. composer require drupal/token
  • This needs to be executed at the root of your Drupal install but not at the same level as the core directory.
Composer will then automatically update your composer.json, adding the module to all the other requirements in the list, like this:

{
    "require": {
        "drupal/token": "^1.5"
    }
}
Composer will download the module and all the possible dependencies it may have.

Specifying a version
You can specify the version of the module / theme you want to download as follows:

composer require 'drupal/module_name:version'
(...where version is replaced with a Composer version or version constraint.)

For example:
composer require 'drupal/token:^1.5' 
composer require 'drupal/simple_fb_connect:~3.0' 
composer require 'drupal/ctools:3.0.0-alpha26' 
composer require 'drupal/token:1.x-dev' 

To avoid problems on different terminals/shells, surround the version in quotes as in the examples above.

Note!: On Windows, single quotes actually might break the version specification and lead to failure to install with 'Could not parse version constraint .....': Invalid version string "....'".

Without the quotes, e.g.:
composer require drupal/ctools:^3.7 or with double quotes instead of single quotes, e.g.:
composer require "drupal/ctools:^3.7" works well.

Reference link Drupal.org

No comments:

Post a Comment