PHP says the class MongoDB\Driver\Manager is not found
Class "MongoDB\Driver\Manager" not foundShort answer
MongoDB\Driver\Manager comes from the mongodb PHP extension, not from Composer. The class is missing when that extension is not installed, or is loaded in one PHP (command line or web) but not the other. Install it, enable it in each php.ini you use, then confirm with php -i.
Fix it fast
1Find out which cause you have
Ask the PHP that fails whether the extension is loaded. Run it once for the command line and once through the web server.
php -i | grep -i mongodb2Match what you see, then jump to its fix
- 1
php -i prints nothing for mongodb.The mongodb extension was never installedFix - 2
php -i shows mongodb, but the website or php artisan serve through FPM still fails.It is enabled for the command line but not for the web server's PHPFix - 3
The extension line is present but PHP does not load it.On Windows the DLL does not match your PHP buildFix
- 1
What does the error look like?
Class "MongoDB\Driver\Manager" not foundFatal error: Class 'MongoDB\Driver\Manager' not found
Cause 1 of 3
The mongodb extension was never installed
1Check
composer require mongodb/mongodb or the Laravel package only adds PHP code. The driver itself is a compiled extension that has to be installed on the machine.
php -m | grep -i mongodb
composer show | grep -i mongodb2Fix
Install the extension and add it to php.ini. The PHP manual now recommends PIE and still documents PECL.
pie install mongodb/mongodb-extension
# or
sudo pecl install mongodb
; php.ini
extension=mongodb.so3Verify
The extension appears in the PHP info output.
php -i | grep -i mongodbConfirmed by php.net
Cause 2 of 3
It is enabled for the command line but not for the web server's PHP
1Check
The manual says to add the extension line for each environment that needs it, such as CLI and web. The command line and the web server usually read different ini files.
php --ini
# and, in a temporary page served by the web server: phpinfo();2Fix
Add the extension line to the ini file used by the web PHP too, then restart that PHP service.
; ini file of the web PHP (path from phpinfo())
extension=mongodb.so3Verify
phpinfo() in the web context lists the mongodb section.
curl -s http://localhost/phpinfo.php | grep -i mongodbConfirmed by php.net
Cause 3 of 3
On Windows the DLL does not match your PHP build
1Check
The prebuilt php_mongodb.dll has to match your PHP version, thread safety (TS or NTS) and architecture, and it goes in the extension directory. The manual also lists libsasl.dll on the system PATH as a requirement.
php -v
php -i | findstr /i "Thread Architecture extension_dir"2Fix
Download the build from the driver's GitHub releases that matches those three values and enable it.
; php.ini
extension=php_mongodb.dll3Verify
The extension is listed.
php -m | findstr /i mongodbConfirmed by php.net
How do I stop it coming back?
- Install the extension in your server provisioning script, not by hand.
- Check php -m on the command line and in the web server when you add a PHP extension.
- Use the official mongodb/laravel-mongodb package for Laravel, and treat the PHP extension as a separate server requirement.
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 says the gd extension is missing on a Laravel project
composer install or composer update stops on a package that requires ext-gd
Composer refuses to install a package that requires ext-gd when the PHP binary running Composer has no GD extension.
4 causes, each with a fixBack end
composer require maatwebsite/excel fails to install
Composer says the package needs an older PHP than the one installed
Composer refuses maatwebsite/excel when the PHP version or a PHP extension does not match what the package and PhpSpreadsheet need.
3 causes, each with a fixBack end
composer install says the lock file is not compatible and asks for update
Your lock file does not contain a compatible set of packages. Please run composer update.
composer install checked the locked packages against this machine's PHP and extensions and found a conflict.
3 causes, each with a fixBack end
composer require laravel/horizon fails: ext-pcntl is missing
laravel/horizon v1.1.0 requires ext-pcntl * -> the requested PHP extension pcntl is missing from your system
Horizon needs the pcntl extension, and Composer refuses to install it where PHP lacks that extension.
3 causes, each with a fixBack end
composer require laravel/passport stops with 'requirements could not be resolved'
... but these were not loaded, likely because it conflicts with another require.
Composer found no Passport release that fits your project at once.
4 causes, each with a fixBack end
Composer says the root composer.json requires a PHP version your PHP does not satisfy
Your requirements could not be resolved to an installable set of packages.
Composer compares the php constraint in your composer.json, or in a package, with the PHP that runs Composer.
3 causes, each with a fix