Error "could not delete" with Composer on Vagrant

TL;DR Switch to Docker. It is the industry standard.

I came across this issue and spent quite some time doing research. I've tried every possible option to fix it but none of them worked for me. For me, the bug occurred on GNU/Linux host with Vagrant and VirtualBox provider.

It turns out it's a VirtualBox bug related to the file system layer and race conditions when creating/deleting files. It occurs only for VirtualBox shared folders, not for regular ones. The sad part is that it seems like it's not going to be fixed any time soon.

Some guys reported that they were able to solve the problem using the following tricks:

  • Downgrading to VirtualBox version 6.0.4.
  • Using nfs or rsync instead of shared folders.
  • Patching composer to add some pauses after certain operations.
  • Disabling plugin usage with --no-plugins option.

But all of this seemed dirty to me. I personally was able to use a workaround suggested on GitHub which is to configure composer to install packages from sources. That's a simple and kind of clean trick which should not have significant negative side effects on your workflow. Try putting the following config into your ~/.config/composer/config.json. Or instead you can edit your composer.json accordingly depending on your needs. Keep in mind that composer.json will override your global config.

{
    "config": {
        "preferred-install": "source"
    }
}

I was trying to update project dependencies (using composer update) during a Laravel Framework upgrade exercise in my local Homestead environment (having run vagrant ssh to login as the default "vagrant" user) and none of the previous answers in this thread made any difference to the...

Could not delete /home/vagrant/projects/projectname/vendor/kylekatarnls/update-helper/src/UpdateHelper

...error message I repeatedly encountered.

The only thing that worked for me was to include a composer option as follows:

composer update --no-plugins

Plugins are used to alter or extend the functionality of Composer. The above command disables all installed plugins. Unfortunately, I'm not clear as to why this command worked for me, as I certainly haven't written any plugins myself. All I can conclude is that there was an erroneous Composer plugin installed that was causing this issue.


It happened once to me and it turns out that I was hitting composer's timeout.

You could take the following measures to gain some speed:

  1. Increase composer process-timeout (default 300) (not really needed if the following settings will help you gain speed, but can't hurt)
  2. Set dist as preferred install type.
  3. Enable https protocol for github, which is faster.

~/.composer/config.json

{
    "config": {
        "process-timeout":      600,
        "preferred-install":    "dist",
        "github-protocols":     ["https"]
    }
}

If you still have problems after that, you can also clear composer's cache:

rm -rf ~/.composer/cache

Just got the same issue.

I see the problem in accessing to some local files. In my case target directory was under "root" and I'm not the root user.

Solution

Change permissions/owner of your files/directory.

  1. Redefine owner:

    sudo chown myuser:myuser -R /path/to

  2. Maybe there is some lack of permissions for group which you are in.
    So, try to run:

    sudo chmod g+rwX -R /path/to

Or maybe you may run your command with "sudo" if it works for you (not recommended). :)

P.S. Never use 777. It's not secure.

UPD1

Another thing, you may found out useful to solve the root of the cause, to wrap up your composer binary to run it always behalf a certain user.

$ cat /usr/local/bin/composer
#!/bin/bash

# run composer behalf www-data user

set -o pipefail
set -o errexit
set -o nounset
#set -o xtrace

[[ "${DEBUG:-}" = "true" ]] && set -o xtrace || true

composer_debug=$([[ 'true' != "${COMPOSER_DEBUG:-}" ]] || echo '-vvv' )

sudo -u www-data -- /usr/bin/composer ${composer_debug:-} $@