Maaz Zindani

Loading the index…

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

PHP says the class MongoDB\Driver\Manager is not found

ErrorClass "MongoDB\Driver\Manager" not found

Short 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.

  • Applies to PHP 8.3, Laravel 12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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 mongodb
  2. 2Match what you see, then jump to its fix

    1. 1php -i prints nothing for mongodb.The mongodb extension was never installedFix
    2. 2php -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. 3The extension line is present but PHP does not load it.On Windows the DLL does not match your PHP buildFix

What does the error look like?

  • Class "MongoDB\Driver\Manager" not found
  • Fatal 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 mongodb

2Fix

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.so

3Verify

The extension appears in the PHP info output.

php -i | grep -i mongodb

Confirmed by php.net

Did this fix it?

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.so

3Verify

phpinfo() in the web context lists the mongodb section.

curl -s http://localhost/phpinfo.php | grep -i mongodb

Confirmed by php.net

Did this fix it?

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.dll

3Verify

The extension is listed.

php -m | findstr /i mongodb

Confirmed by php.net

Did this fix it?

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 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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.