ENGINEERING JOURNAL

Composer Autoload Fails on Linux: Case-Sensitive Class Loading Pitfalls

Composer autoload works locally but fails on Linux with Class not found? Usually it is a case-sensitive PSR-4 file name. Here is how to find and fix it.

THE ARTICLE 01 — 02

Your app works on your laptop. You deploy it to a Linux server and get a 500 error: Class "App\Services\PaymentService" not found. Nothing else changed. The code is identical, the deploy ran clean, and Composer reported no errors. The most likely cause is a case mismatch between a class name and its file name, and Linux is the only filesystem in the chain that cares.

Why it works locally and breaks on the server

Composer's PSR-4 autoloader turns a class name into a file path. The class App\Services\PaymentService maps to app/Services/PaymentService.php. On macOS and Windows, the filesystem is usually case-insensitive, so paymentservice.php, Paymentservice.php, and PaymentService.php all resolve to the same file. On Linux, they are three different files, and only the exact match exists. The autoloader asks for PaymentService.php, the file on disk is paymentservice.php, and PHP reports the class as not found.

The classic symptoms

Look for this pattern before you start debugging anything else:

  • The error is Class not found or Interface not found, not a syntax error.
  • It happens only on the Linux server, never on your local machine.
  • It appears after a deploy, often for a class that was recently added or renamed.
  • Running composer dump-autoload on your laptop does not reproduce it.

If all four are true, check file name casing first. It will save you an hour of reading logs that point nowhere.

How PSR-4 maps class names to files

The mapping lives in composer.json. A typical Laravel entry looks like this:

"autoload": {
    "psr-4": {
        "App\\": "app/",
        "Database\\Factories\\": "database/factories/",
        "Database\\Seeders\\": "database/seeders/"
    }
}

For each namespace segment, Composer appends the segment to the directory and adds .php. So App\Http\Controllers\OrderController must live at app/Http/Controllers/OrderController.php, with every directory and the file name matching the case of the class exactly. The namespace App maps to app because that is what the mapping says, but everything after the prefix must match character for character, including case.

Finding the mismatch

Composer can check this for you. Run this in the project root:

composer dump-autoload --strict-psr

With --strict-psr, Composer scans your PSR-4 directories and warns about every class whose file path does not match its fully qualified name. Fix every warning it prints. This is the single most useful command for this class of bug, and it costs nothing to add to your deploy checklist.

If you already know which class is missing, compare the file name directly:

ls app/Services/
grep -r "class PaymentService" app/Services/

If ls shows paymentservice.php while the class is declared as PaymentService, you have found it. Also check directory names. A folder named app/services instead of app/Services breaks every class inside it on Linux, and it is easy to miss because the files themselves look fine.

The fix, and the git trap

The fix itself is simple: rename the file so its name matches the class name exactly, then run composer dump-autoload on the server. But there is a trap. If you rename a file by changing only its case on macOS or Windows, git may not notice. The working tree and the index both look unchanged to a case-insensitive filesystem, so the rename never gets committed and the deploy still ships the old name.

Use git mv so the rename is recorded explicitly:

git mv app/Services/paymentservice.php app/Services/PaymentService.php
git status
git commit -m "Fix case of PaymentService filename for Linux"

Then verify the commit actually contains the new name with git show --name-status HEAD. If the rename does not appear there, it did not happen. You can also set git config core.ignorecase false in the repository so git stops pretending case-only differences do not exist, though on a case-insensitive filesystem this can surface other quirks, so test it on a scratch clone first.

Preventing it from coming back

This bug returns whenever someone adds a class from a case-insensitive machine, so make the check automatic:

  • Add composer dump-autoload --strict-psr --optimize to your CI pipeline and fail the build on warnings. It runs in seconds.
  • Run your tests inside Docker on a Linux image, even if you develop on macOS. The container filesystem is case-sensitive, so the bug fails locally before it reaches production. My Docker Compose setup for local Laravel development already runs the app on Linux, which catches this for free.
  • Keep class names, file names, and directory names consistent from the start. When you create a class, create the file with the exact same casing in the same step.
  • After any rename, check git status before you push. If a case-only rename does not show up, it will not deploy.

As a final check before any deploy, my PHP deployment checklist for shared hosting includes the autoload dump and a quick smoke test of the routes that touch recently changed classes.

Takeaway

A "Class not found" error that only appears on Linux is almost always a case mismatch between a class name and its file name. Your laptop forgives it, the server does not. Run composer dump-autoload --strict-psr, fix what it reports, commit case-only renames with git mv, and put the strict check in CI so the next developer on a Mac does not ship the same bug again.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗