Trying to install my first Drupal module with Composer like #Drupal #Composer #Help

Facing issue to install my first Drupal module with Composer.

Hey @Ashish07

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 9 or 10 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 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 :slight_smile:

composer require drupal/ < modulename >

Let’s assume it will be an Admin Toolbar module. Then the command will look like this:

composer require 'drupal/admin_toolbar

Composer will update the composer.json by adding Admin Toolbar to the requirements.

4. Download a Specific Version of Contributed Modules or Themes

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/admin_toolbar:^3.4’