Maaz Zindani

Loading the index…

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

require(vendor/autoload.php): Failed to open stream: No such file or directory

ErrorWarning: require(vendor/autoload.php): Failed to open stream: No such file or directory

Short answer

PHP looked for vendor/autoload.php at a path where it does not exist. Either Composer never ran in this folder, so vendor is missing, or the require uses a path relative to the wrong directory. Run composer install next to composer.json and require the file with __DIR__.

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

Fix it fast

  1. 1Find out which cause you have

    Check whether the file exists at all, from the folder that holds composer.json.

    ls -l vendor/autoload.php
    ls composer.json
  2. 2Match what you see, then jump to its fix

    1. 1ls: cannot access 'vendor/autoload.php': No such file or directoryComposer was never run here, so there is no vendor folderFix
    2. 2vendor/autoload.php exists, yet the error still names the short relative pathThe path is relative to the wrong directoryFix
    3. 3autoload.php exists, but a class from your own namespace is not found afterwardscomposer.json changed and the autoloader is staleFix

What does the error look like?

  • Warning: require(vendor/autoload.php): Failed to open stream: No such file or directory
  • Fatal error: Uncaught Error: Failed opening required 'vendor/autoload.php'

Cause 1 of 3

Composer was never run here, so there is no vendor folder

1Check

vendor/ is normally excluded from Git. A fresh clone, a ZIP upload or a deploy without a Composer step has composer.json but no vendor folder.

ls composer.json composer.lock
cat .gitignore | grep vendor

2Fix

Run Composer in the folder that contains composer.json. composer install reads composer.lock and creates vendor/ including autoload.php.

composer install

3Verify

The file now exists.

ls -l vendor/autoload.php

Confirmed by getcomposer.org

Did this fix it?

Cause 2 of 3

The path is relative to the wrong directory

1Check

A relative path is resolved against the current working directory or include_path, not against the file that contains the require. A script in a sub-folder, or a cron job started from elsewhere, looks in the wrong place.

find . -name autoload.php -path "*vendor*" -maxdepth 3
pwd

2Fix

Build the path from the location of the current file with __DIR__. Count the folders between this file and the folder that holds vendor/.

<?php
// file is in the project root
require __DIR__ . '/vendor/autoload.php';

// file is in public/ and vendor is one level up
require __DIR__ . '/../vendor/autoload.php';

3Verify

Run the script from a different folder and it still loads.

cd / && php /var/www/app/send-mail.php

Confirmed by getcomposer.org

Did this fix it?

Cause 3 of 3

composer.json changed and the autoloader is stale

1Check

After you add or change the autoload field in composer.json, the generated autoloader has to be rebuilt.

grep -n -A4 '"autoload"' composer.json

2Fix

Regenerate the autoloader.

composer dump-autoload

3Verify

The class now loads.

php -r 'require "vendor/autoload.php"; var_dump(class_exists("App\\Mailer"));'

Confirmed by getcomposer.org

Did this fix it?

How do I stop it coming back?

  • Add composer install --no-dev to the deploy script, run in the release folder.
  • Always require with __DIR__, never a bare relative path.
  • Commit composer.lock so every machine installs the same versions.
  • Do not commit vendor/; rebuild it on each deploy.

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

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.