Customizing Composer to Install Drupal 9 and Manage Dependencies

17 Sep 2020 webmaster

I used Composer to install and manage Drupal 9 and all dependencies (modules, themes, libraries) in my Ubuntu 20.09 but doing some modification to customizing the structure in the new website installation. Make sure you have composer installed on your local machine before executing any Composer commands.

Please, pay attention on of the release of Drupal 8.8.0 - the recommend composer template changed from drupal-composer/drupal-project to the officially supported: drupal/recommended-project.

Create a drupal project

composer create-project drupal/recommended-project my_site_name_dir

This command will create a project in 'my_site_name_dir' and automatically executes composer install to download the latest stable version of Drupal and all its dependencies. Your 'my_site_name_dir' will contain files that should be outside of your web root and not  accessible by the web server. The web root will be 'my_site_name_dir/web'. This structure must to have some problems in your production website and I recommending that change the path using by composer before installation the structure

To do a modified install

I wanted to modify some of the properties of the downloaded composer.json before composer install is executed, use the --no-install flag when running composer create-project. For example, it is possible that you want to rename the subdirectory 'web' to something else or in my case to modify all components in the future website according to the future production website

composer create-project --no-install drupal/recommended-project my_site_name_dir

This will created olny two files in the my-site_name_dir named: composer.json and composer.lock. By my requeriment I needed to modify the composer.json to ensure that the composer install will instaling all files in the my_site_name_dir and not in the \web directory. I modified the section extra and installed-paths inside the composer.json file as bellow:

"extra": {
        "drupal-scaffold": {
            "locations": {
                "web-root": "./"
            }
        },
        "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/Commands/contrib/{$name}": [
                "type:drupal-drush"
            ],
            "./modules/custom/{$name}": [
                "type:drupal-custom-module"
            ],
            "./themes/custom/{$name}": [
                "type:drupal-custom-theme"
            ]
        },

Then running composer install command to download Drupal and all its dependencies according to my new structure in the website.

Install Drupal using the standard web interface or Drush

After composer finishes downloading the packages, you can navigate your browser to your site's url and start the setup. It'll ask for the database credentials, a name for the admin user and some basic information. Also you can install Drupal using the command line using Drush.

Add Drush in your project by running:

composer require drush/drush 

Use this command to run the command line setup wizard. Without any arguments it'll install the standard profile and ask only for database credentials. Don't forget to create the appropiated folders for files inside site\default folder and copy the default.settings.php to settings.php writeable file before doing the install command bellow:

drush site:install

See more information about Using Composer to Install Drupal 9 and Manage Dependencies in the official Drupal website documentation

Redirect to Facebook Page