Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Composer cannot create the vendor directory

ErrorRuntimeException: vendor does not exist and could not be created.

Short answer

Composer tried to make the vendor folder and the operating system refused. The user running Composer cannot write in the project folder, which is common on a Vagrant synced folder or a folder owned by root. Run Composer as the owner, fix the mount's owner and group, or move vendor-dir to a writable path.

  • Applies to Composer 2, PHP 8.3, Vagrant 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See who owns the project folder and who you are. If they differ and the folder is not writable for you, the causes below apply.

    id -un
    ls -ld .
    touch .write-test && rm .write-test
  2. 2Match what you see, then jump to its fix

    1. 1touch fails with Permission denied in the same folderComposer runs as a user that cannot write in the project folderFix
    2. 2The project sits under /vagrant, and creating files there failsA Vagrant synced folder is mounted with an owner that cannot write thereFix
    3. 3Permissions on the share cannot be changed, or symlinks in vendor failvendor must live outside the shared folderFix

What does the error look like?

  • RuntimeException: vendor does not exist and could not be created.

Cause 1 of 3

Composer runs as a user that cannot write in the project folder

1Check

Compare the folder owner in ls -ld with the output of id -un. Composer's documentation advises against running it as root, so do not solve this with sudo.

ls -ld . && id -un

2Fix

Give your normal user ownership of the project folder, then run Composer as that user.

sudo chown -R "$(id -un)": .
composer install

3Verify

composer install finishes and a vendor folder exists.

ls -d vendor

Confirmed by getcomposer.org

Did this fix it?

Cause 2 of 3

A Vagrant synced folder is mounted with an owner that cannot write there

1Check

Vagrant mounts synced folders with the owner and group set to the SSH user, and parent folders belong to root. Owner and group IDs in mount_options take precedence over owner and group.

mount | grep vagrant

2Fix

Set owner and group on the synced folder in the Vagrantfile, then reload the machine.

# Vagrantfile
config.vm.synced_folder ".", "/vagrant",
  owner: "vagrant", group: "vagrant"

# then on the host
vagrant reload

3Verify

Inside the machine, creating a file in the project folder works, and composer install succeeds.

vagrant ssh -c "cd /vagrant && touch .write-test && rm .write-test"

Confirmed by developer.hashicorp.com

Did this fix it?

Cause 3 of 3

vendor must live outside the shared folder

1Check

Vagrant's documentation says symbolic link support is not consistent across synced folder types and host and guest combinations. Composer lets you place vendor elsewhere with the vendor-dir setting.

composer config vendor-dir

2Fix

Point vendor-dir to a path inside the guest's own disk.

composer config vendor-dir /home/vagrant/myproject-vendor
composer install

3Verify

composer install finishes; the autoloader is in the new directory.

ls /home/vagrant/myproject-vendor/autoload.php

Confirmed by getcomposer.org

Did this fix it?

How do I stop it coming back?

  • Never run composer with sudo; fix the folder owner once instead.
  • Set owner and group on every Vagrant synced folder that holds code you write to.
  • If vendor-dir is moved, update the require line in your entry file to the new autoload path.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Composer warns it is proceeding without cache

See also cache-read-only config if your filesystem is read-only.

Composer tried to create its cache folder, or write to it, and could not, so it runs without a cache.

3 causes, each with a fix

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.