Post

Composer Cheatsheet

Cheatsheet for Composer with a focus on Drupal Web Development

Composer install

When you run composer install, Composer does the following:

  • Dependencies Installation: It reads the composer.lock file to install the exact versions of dependencies listed there. If composer.lock doesn’t exist, it creates one based on composer.json.
  • Creates vendor Directory: It downloads all the dependencies and places them in the vendor directory.
  • Autoload Files: It generates an autoload.php file in the vendor directory, which allows you to load all the installed libraries in your code using Composer’s autoloader.
  • Updates composer.lock (if needed): If composer.json has changed since the last install, composer.lock will be updated to reflect the new dependencies.
  • Runs Post-Scripts: If there are scripts defined in composer.json (e.g., for clearing cache or seeding a database), Composer runs them as specified.

This command is used to ensure that the exact same versions of dependencies are installed across different environments (e.g., development, testing, production), keeping the environment consistent.

Cheatsheet for Composer with a focus on Drupal Web Development

Troubleshooting

If you have just updated drupal core locally, deployed it and then tried to run composer install on your remote server and got the following error: “PHP Fatal error: Uncaught Error: Failed opening required ‘/var/www/html/vendor/composer/../twig/twig/src/Resources/core.php’”. This error due to permissions and can be solved by nuking the vendor folder and running composer install again. This gives composer install a clear run at installing all dependencies.

failed opening required

Install Theme

1
composer require 'drupal/bootstrap5:^4.0'

Install Drush module

The below use of composer is also using ddev. If not using Ddev exclude the ddev from the command.

1
ddev composer require --dev drush/drush

Update Drupal Core

Update to 10.3

1
composer require drupal/core-recommended:10.3 drupal/core-composer-scaffold:10.3 drupal/core-project-message:10.3 --update-with-all-dependencies

Update to 10.2

1
composer require drupal/core-recommended:10.2 drupal/core-composer-scaffold:10.2 drupal/core-project-message:10.2 --update-with-all-dependencies

Install a sub theme

In this example we install the radix theme as a sub theme in Drupal using composer

1
composer require drupal/radix

Install components module

1
composer require 'drupal/components:^3.0@beta'

Enable components

1
drush en components

Now create a sub theme

1
drush --include="themes/contrib/radix" radix:create radix_blocks

Enable the Sub theme

  • Go to appearance and themes
  • Install and set as default the radix subtheme

Update Drupal core with composer

Check Status report under Reports in the Administrative GUI. Fix all errors and warnings before proceeding. Before updating, make a back up with Backup and Migrate or Drush, so you can roll back if anything goes wrong. If you use Drush, update to minimum Drush 12.4.3 to be Drupal 10.2 compatible:

1
composer update drush/drush

Update Drupal core with this command:

1
composer update "drupal/core-*" --with-all-dependencies

Use Drush to run required database updates and rebuild caches:

1
2
drush updatedb
drush cache:rebuild

Check Status report under Reports. Fix all errors and warnings.

If using ddev

1
2
ddev ssh
composer update "drupal/core-*" --with-all-dependencies

Troubleshooting

Below are fixes to some errors i have needed to fix at times.

Fix the Satisfy Requirement Error

For example:

1
phpunit/phpunit 5.7.16 requires php ^5.6 || ^7.0 -> your php version (8.1.29) does not satisfy that requirement
  • open composer.json
  • update "phpunit/phpunit": "5.7.16|6.*" to a Satifiable version "phpunit/phpunit": "^9.5"

Uninstall Drupal Module

1
drush pm:uninstall module_name

Remove the Module:

1
composer remove drupal/module_name

with ddev

Uninstall the Module:

1
ddev drush pm:uninstall module_name

Remove the Module:

1
ddev composer remove drupal/module_name
This post is licensed under CC BY 4.0 by the author.