Composer cannot create the vendor directory
RuntimeException: 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.
Fix it fast
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-test2Match what you see, then jump to its fix
- 1
touch fails with Permission denied in the same folderComposer runs as a user that cannot write in the project folderFix - 2
The project sits under /vagrant, and creating files there failsA Vagrant synced folder is mounted with an owner that cannot write thereFix - 3
Permissions on the share cannot be changed, or symlinks in vendor failvendor must live outside the shared folderFix
- 1
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 -un2Fix
Give your normal user ownership of the project folder, then run Composer as that user.
sudo chown -R "$(id -un)": .
composer install3Verify
composer install finishes and a vendor folder exists.
ls -d vendorConfirmed by getcomposer.org
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 vagrant2Fix
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 reload3Verify
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
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-dir2Fix
Point vendor-dir to a path inside the guest's own disk.
composer config vendor-dir /home/vagrant/myproject-vendor
composer install3Verify
composer install finishes; the autoloader is in the new directory.
ls /home/vagrant/myproject-vendor/autoload.phpConfirmed by getcomposer.org
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 fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix