MongoDB Driver Not Found

|
| By Webner

Problem – Getting “Class ‘MongoDB\Driver\Manager’ not found” error while inserting some data in the collection (Laravel)

Check the following three points in order to resolve the MongoDB driver issue in Laravel:
1. PHP Module
2. MongoDB Extension
3. MongoDB Library

1. PHP Module –
Check if the PHP module is loaded for MongoDB or not. To verify this, run the following command:

                   $php -m | grep mongodb

If you are getting the o/p with “mongodb” keyword then you don’t need to load MongoDB module. Otherwise, follow the steps below:

                   $ sudo apt-get update

Next, run the following command in order to install PHP 7.2 and related modules-

                   $sudo apt-get install php7.2 libapache2-mod-php7.2 php7.2-common php7.2-cli php7.2-mongodb php-pear php7.2-dev

2. MongoDB Extension –
The PHP driver consists of two components; the MongoDB extension and the library, where users can use only the extension but they are strongly encouraged to use the extension and library altogether. MongoDB library helps to provide a high-level API consistency with other MongoDB language drivers.

Next, we need to install the Driver and Library. Check using the following command if the extension and the library are installed already:

                   $php --ri mongodb | grep version

You will get the following result or if there is no o/p then you need to load both the MongoDB extension and the
Result:
MongoDB extension version => 1.6.0
libbson bundled version => 1.15.1
libmongoc bundled version => 1.15.1


3. MongoDB Library –
Install driver (extension and library)-

Install the extension with the help of PECL driver using below command:

                   $ pecl install mongodb
                   $ echo "extension=mongodb.so" >> `php --ini | grep "Loaded Configuration" | sed -e "s|.*:\s*||"`

Now install the PHP library with the Composer by running the following command from your project root:

                   $ composer require mongodb/mongodb

Now clear the cache of the Laravel project and restart it if possible. Commands for both the processes are given below. Make sure you are running the following commands from your project root.

                   $ php artisan cache:clear

Now Restart Apache:

                   $ sudo service apache2 restart

Leave a Reply

Your email address will not be published. Required fields are marked *