Could not delete default.services.yml error (solved)

When requiring modules with composer on Drupal 8 or Drupal 9 you may run into the following error:

Installation failed, reverting ./composer.json to its original content.

[RuntimeException]
Could not delete ../web/sites/default/default.services.yml

To fix this you have to grant your user write permissions to this folder with:

chmod u+w web/sites/default

If you're working locally and you have the default settings.local.php file included you won't have this problem because of the following line (do not enable this on production):

$settings['skip_permissions_hardening'] = TRUE;