Fix "Failed opening required vendor/autoload.php" โ€” PHP Composer Autoload Not Found

beginner๐Ÿ˜ PHP2026-03-19| PHP 7.x / 8.x, Composer 2.x, Linux / macOS / Windows, any PHP framework (Laravel, Symfony, Slim, or vanilla PHP)

Error Message

Fatal error: require(): Failed opening required 'vendor/autoload.php'
#php#composer#autoload#vendor

TL;DR โ€” Quick Fix

Run composer install in your project root โ€” the folder where composer.json lives. This recreates the vendor/ directory and the missing autoload.php.

cd /path/to/your/project
composer install

No composer command? Jump to Fix 3 first.

What Triggers This Error

The full error looks like this:

Fatal error: require(): Failed opening required '/var/www/html/vendor/autoload.php'
(include_path='.:/usr/share/php') in /var/www/html/index.php on line 3

PHP hits a line like require __DIR__ . '/vendor/autoload.php'; and the file isn't there. Four things cause this:

  • vendor/ was never created โ€” you cloned the repo but skipped composer install. This is the most common cause by far.
  • vendor/ was intentionally excluded โ€” .gitignore keeps vendor/ out of version control, so it's gone after a fresh clone.
  • Wrong working directory โ€” your script runs from a different folder than composer.json, breaking the relative path.
  • Autoload map is stale โ€” you added a new class or PSR-4 namespace but didn't regenerate the autoloader with dump-autoload.

Step-by-Step Fixes

Fix 1 โ€” Run composer install (most common)

Go to the folder containing composer.json and run:

composer install

Composer downloads all dependencies into vendor/ and writes autoload.php. On production servers where dev packages aren't needed:

composer install --no-dev --optimize-autoloader

Fix 2 โ€” Check you're in the right directory

The error message tells you exactly where PHP looked. If it says /var/www/html/vendor/autoload.php, verify composer.json is actually in /var/www/html/:

ls /var/www/html/composer.json

If composer.json sits one level up (say /var/www/), the require path in your PHP file is wrong. Fix it:

// Wrong โ€” points to the wrong directory
require __DIR__ . '/vendor/autoload.php';

// Correct โ€” go up one level
require __DIR__ . '/../vendor/autoload.php';

When in doubt, use an absolute path derived from composer.json's location. It never lies.

Fix 3 โ€” Install Composer if it's missing

Getting command not found: composer? Install it globally:

# Linux/macOS
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer

# Verify
composer --version

Ubuntu/Debian users can take a shortcut:

sudo apt install composer

One caveat: the apt package often lags behind upstream. For Composer 2.x specifically, the curl method is more reliable.

Fix 4 โ€” Regenerate the autoload map after adding classes

If vendor/ exists but a specific class still isn't found, the autoload map is stale. You added new code without telling Composer about it:

composer dump-autoload

# Optimized classmap for production (faster class resolution)
composer dump-autoload --optimize

Fix 5 โ€” Fix permissions blocking Composer

On shared hosting or inside Docker containers, composer install can fail silently when it can't write to the project directory. Check ownership first:

# See who owns the files
ls -la /var/www/html/

# Fix ownership (replace www-data with your actual web user)
sudo chown -R www-data:www-data /var/www/html/

# Then retry
composer install

Fix 6 โ€” Wire it into CI/CD and Docker

vendor/ is never committed, so automated pipelines need to run composer install themselves. A minimal Dockerfile:

FROM php:8.2-fpm
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
WORKDIR /var/www/html
COPY composer.json composer.lock ./
RUN composer install --no-dev --optimize-autoloader
COPY . .

Copy composer.json and composer.lock before the rest of your source code. Docker caches that install layer and skips it on subsequent builds when those two files haven't changed โ€” a significant time saver on large projects.

For GitHub Actions:

- name: Install PHP dependencies
  run: composer install --no-dev --prefer-dist --optimize-autoloader

Verification

Quick sanity check โ€” does autoload.php exist now?

ls vendor/autoload.php
# vendor/autoload.php

Reload your application. For CLI scripts:

php index.php

Still seeing the error? The require path in your PHP file probably points to the wrong location. Compare it against where vendor/autoload.php actually lives on disk.

Preventing It From Happening Again

  • Commit composer.lock, never vendor/.
  • Add vendor/ to .gitignore โ€” it doesn't belong in version control.
  • Put composer install in your README's setup section. Future contributors will thank you.
  • In Docker, always copy composer.json and composer.lock before your source files so the dependency layer gets cached properly.

Related Error Notes