PHP says class Carbon\Carbon is not found
Class "Carbon\Carbon" not foundShort answer
Carbon is a Composer package. PHP only finds Carbon\Carbon when the package is installed in vendor, your script loads vendor/autoload.php, and the class name is imported with use Carbon\Carbon or written fully qualified. Check these three in that order.
Fix it fast
1Find out which cause you have
Ask Composer whether the package is installed in this project.
composer show nesbot/carbon2Match what you see, then jump to its fix
- 1
composer show says the package is not found, or vendor/nesbot is missingThe package is not installed in this projectFix - 2
Carbon is in vendor but plain PHP scripts still cannot find the classThe Composer autoloader is not loadedFix - 3
The error names your own namespace, for example App\Http\Controllers\CarbonThe class name is not imported inside a namespaceFix
- 1
What does the error look like?
Class "Carbon\Carbon" not foundClass 'Carbon\Carbon' not found
Cause 1 of 3
The package is not installed in this project
1Check
Look at whether vendor/nesbot/carbon exists and whether composer.json requires it.
ls vendor/nesbot/
grep carbon composer.json2Fix
Install it with Composer, which also writes it to composer.json.
composer require nesbot/carbon3Verify
The version shows up.
composer show nesbot/carbonConfirmed by github.com
Cause 2 of 3
The Composer autoloader is not loaded
1Check
Composer writes vendor/autoload.php for packages with autoload information. Your entry script has to require it once, before any class is used.
grep -n "vendor/autoload.php" index.php2Fix
Require the autoloader at the top of the entry file. If you edited the autoload section of your own composer.json, regenerate it.
<?php
require __DIR__ . '/vendor/autoload.php';
// after changing autoload in composer.json:
// composer dump-autoload3Verify
Run a one-line script.
php -r 'require "vendor/autoload.php"; echo Carbon\Carbon::now(), PHP_EOL;'Confirmed by getcomposer.org
Cause 3 of 3
The class name is not imported inside a namespace
1Check
In a file with a namespace, a name without an import is looked up inside that namespace. The manual says a fully qualified name with a leading backslash ignores imports and the current namespace.
head -15 app/Http/Controllers/ReportController.php2Fix
Import the class under the namespace line, or write it fully qualified.
namespace App\Http\Controllers;
use Carbon\Carbon;
$now = Carbon::now();
// or without an import:
$now = \Carbon\Carbon::now();3Verify
Reload the page. The error is gone.
php -l app/Http/Controllers/ReportController.phpConfirmed by php.net
How do I stop it coming back?
- Add every package with composer require, so composer.json and vendor stay in step.
- Commit composer.lock and run composer install on the server after each deploy.
- Import classes with use at the top of each file.
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
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 fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fix