mstdmstd's avatar

How to fix Class "MongoDB\Driver\Manager" not found error under homestead ?

I have installed Homestead with mongodb

In my Homestead.yaml I have :

    ...
features:
    - mongodb: true 
    ...

But running the app I got

Class "MongoDB\Driver\Manager" not found

I have restarted mongod with command

sudo systemctl restart mongod

and I have under vagrant :

$ sudo systemctl status  mongod
● mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2023-02-17 12:49:29 UTC; 8min ago
       Docs: https://docs.mongodb.org/manual
   Main PID: 4856 (mongod)
     Memory: 162.8M
     CGroup: /system.slice/mongod.service
             └─4856 /usr/bin/mongod --config /etc/mongod.conf

Feb 17 12:49:29 homestead systemd[1]: Started MongoDB Database Server.
$ uname -a
Linux homestead 5.4.0-137-generic #154-Ubuntu SMP Thu Jan 5 17:03:22 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

When I have such problem under my ubunyi I salve it with command :

sudo apt-get install php-mongodb

But how can it be salved under homestaed?

in my hosting kubuntu I have :

$ uname -a
Linux master-at-home 5.15.0-53-generic #59~20.04.1-Ubuntu SMP Thu Oct 20 15:10:22 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux
$ dpkg -s  virtualbox
Package: virtualbox
Status: install ok installed
Version: 6.1.38-dfsg-3~ubuntu1.20.04.1

$ dpkg -s  vagrant
Package: vagrant
Status: install ok installed
Version: 2.2.6+dfsg-2ubuntu3

Thank you!

0 likes
10 replies
s4muel's avatar

not sure if that would help, but try also installing the MongoDB driver library to the project via composer

composer require mongodb/mongodb
mstdmstd's avatar

@s4muel I have installed this plugin, but I still got the error :

 Class "MongoDB\Driver\Manager" not found 

My composer.json have :


{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0.2",
        "adldap2/adldap2-laravel": "^6.1",
        "bensampo/laravel-enum": "^5.3",
        "cviebrock/eloquent-sluggable": "^9.0",
        "guzzlehttp/guzzle": "^7.2",
        "intervention/image": "^2.7",
        "jenssegers/mongodb": "^3.9.2",
        "laravel/framework": "^9.3.9",
        "laravel/sanctum": "^3.0",
        "laravel/telescope": "^4.9",
        "laravel/tinker": "^2.7",
        "league/flysystem-aws-s3-v3": "^3.5",
        "maatwebsite/excel": "^3.1",
        "mews/purifier": "^3.3",
        "mongodb/mongodb": "^1.15",
        "opcodesio/log-viewer": "^1.2",
        "psr/simple-cache": "^2.0",
        "sentry/sentry-laravel": "^2.13",
        "snipe/banbuilder": "dev-master",
        "spatie/browsershot": "^3.57"
    },
    "require-dev": {
        "barryvdh/laravel-debugbar": "^3.7",
        "barryvdh/laravel-ide-helper": "^2.12",
        "fakerphp/faker": "^1.9.1",
        "laravel/breeze": "^1.11",
        "laravel/pint": "^1.0",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "sempro/phpunit-pretty-print": "^1.4",
        "spatie/laravel-ignition": "^1.0",
        "squizlabs/php_codesniffer": "*"
    },
    "autoload": {
        "files": [
            "app/Library/helper.php"
        ],

        "psr-4": {
            "App\": "app/",
            "Database\Factories\": "database/factories/",
            "Database\Seeders\": "database/seeders/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\Foundation\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    },
    "config": {
        "optimize-autoloader": true,
        "preferred-install": "dist",
        "sort-packages": true,
        "allow-plugins": {
            "pestphp/pest-plugin": true
        }
    },
    "minimum-stability": "dev",
    "prefer-stable": true
}
s4muel's avatar

@mstdmstd have you run vagrant reload --provision after adding the mongodb feature to Homestead.yaml

mstdmstd's avatar

@johnDoe220 In my Homestead.yaml I have mongo options :

features:
    - mysql: true
    - mongodb: true

...

ports:
    - send: 33060 # MySQL/MariaDB
      to: 3306

    - send: 27017 # MongoDB
      to: 27017

I removed vendor directory and run composer again with errors:

$ composer install
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Info from https://repo.packagist.org: #StandWithUkraine
Updating dependencies
Your requirements could not be resolved to an installable set of packages.

Problem 1
- mongodb/mongodb dev-master requires ext-mongodb ^1.16.0 -> it is missing from your system. Install or enable PHP's mongodb extension.
- mongodb/mongodb[1.15.0, ..., v1.15.x-dev] require ext-mongodb ^1.15.0 -> it is missing from your system. Install or enable PHP's mongodb extension.
- mongodb/mongodb 1.16.x-dev is an alias of mongodb/mongodb dev-master and thus requires it to be installed too.
- Root composer.json requires mongodb/mongodb ^1.15 -> satisfiable by mongodb/mongodb[1.15.0, v1.15.x-dev, 1.16.x-dev (alias of dev-master)].

To enable extensions, verify that they are enabled in your .ini files:
- /etc/php/8.1/cli/php.ini
- /etc/php/8.1/cli/conf.d/10-mysqlnd.ini
- /etc/php/8.1/cli/conf.d/10-opcache.ini
- /etc/php/8.1/cli/conf.d/10-pdo.ini
- /etc/php/8.1/cli/conf.d/15-xml.ini
- /etc/php/8.1/cli/conf.d/20-bcmath.ini
- /etc/php/8.1/cli/conf.d/20-bz2.ini
- /etc/php/8.1/cli/conf.d/20-calendar.ini
- /etc/php/8.1/cli/conf.d/20-ctype.ini
- /etc/php/8.1/cli/conf.d/20-curl.ini
- /etc/php/8.1/cli/conf.d/20-dba.ini
- /etc/php/8.1/cli/conf.d/20-dom.ini
- /etc/php/8.1/cli/conf.d/20-enchant.ini
- /etc/php/8.1/cli/conf.d/20-exif.ini
- /etc/php/8.1/cli/conf.d/20-ffi.ini
- /etc/php/8.1/cli/conf.d/20-fileinfo.ini
- /etc/php/8.1/cli/conf.d/20-ftp.ini
- /etc/php/8.1/cli/conf.d/20-gd.ini
- /etc/php/8.1/cli/conf.d/20-gettext.ini
- /etc/php/8.1/cli/conf.d/20-gmp.ini
- /etc/php/8.1/cli/conf.d/20-iconv.ini
- /etc/php/8.1/cli/conf.d/20-igbinary.ini
- /etc/php/8.1/cli/conf.d/20-imagick.ini
- /etc/php/8.1/cli/conf.d/20-imap.ini
- /etc/php/8.1/cli/conf.d/20-intl.ini
- /etc/php/8.1/cli/conf.d/20-ldap.ini
- /etc/php/8.1/cli/conf.d/20-mbstring.ini
- /etc/php/8.1/cli/conf.d/20-msgpack.ini
- /etc/php/8.1/cli/conf.d/20-mysqli.ini
- /etc/php/8.1/cli/conf.d/20-odbc.ini
- /etc/php/8.1/cli/conf.d/20-pdo_dblib.ini
- /etc/php/8.1/cli/conf.d/20-pdo_firebird.ini
- /etc/php/8.1/cli/conf.d/20-pdo_mysql.ini
- /etc/php/8.1/cli/conf.d/20-pdo_odbc.ini
- /etc/php/8.1/cli/conf.d/20-pdo_pgsql.ini
- /etc/php/8.1/cli/conf.d/20-pdo_sqlite.ini
- /etc/php/8.1/cli/conf.d/20-pgsql.ini
- /etc/php/8.1/cli/conf.d/20-phar.ini
- /etc/php/8.1/cli/conf.d/20-posix.ini
- /etc/php/8.1/cli/conf.d/20-pspell.ini
- /etc/php/8.1/cli/conf.d/20-readline.ini
- /etc/php/8.1/cli/conf.d/20-redis.ini
- /etc/php/8.1/cli/conf.d/20-shmop.ini
- /etc/php/8.1/cli/conf.d/20-simplexml.ini
- /etc/php/8.1/cli/conf.d/20-snmp.ini
- /etc/php/8.1/cli/conf.d/20-soap.ini
- /etc/php/8.1/cli/conf.d/20-sockets.ini
- /etc/php/8.1/cli/conf.d/20-sqlite3.ini
- /etc/php/8.1/cli/conf.d/20-sysvmsg.ini
- /etc/php/8.1/cli/conf.d/20-sysvsem.ini
- /etc/php/8.1/cli/conf.d/20-sysvshm.ini
- /etc/php/8.1/cli/conf.d/20-tidy.ini
- /etc/php/8.1/cli/conf.d/20-tokenizer.ini
- /etc/php/8.1/cli/conf.d/20-xmlreader.ini
- /etc/php/8.1/cli/conf.d/20-xmlrpc.ini
- /etc/php/8.1/cli/conf.d/20-xmlwriter.ini
- /etc/php/8.1/cli/conf.d/20-xsl.ini
- /etc/php/8.1/cli/conf.d/20-zip.ini
- /etc/php/8.1/cli/conf.d/25-memcached.ini
You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
Alternatively, you can run Composer with `--ignore-platform-req=ext-mongodb` to temporarily ignore these required extensions.

I do not see any mongo in extentions list. Priorly I run composer with option :

--ignore-platform-req=ext-mongodb

Looks like that is my I passed it priorly...

Do I have invalid format/place for

mongodb: true

?

mstdmstd's avatar

Running provision command one more time I noticed messages in the end of output :

    homestead: We didn't find MariaDB ($mariadb), skipping $DB creation
==> homestead: Running provisioner: shell...
    homestead: Running: script: Creating Postgres Database: homestead
    homestead: We didn't find a PID for postgres, skipping $DB creation
==> homestead: Running provisioner: shell...
    homestead: Running: script: Creating Mongo Database: homestead
    homestead: Current Mongosh Log ID:  6409d90a95aec57a0c7cb184
    homestead: Connecting to:           mongodb://127.0.0.1:27017/homestead?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.7.1
    homestead: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
The SSH command responded with a non-zero exit status. Vagrant
assumes that this means the command failed. The output for this command
should be in the log above. Please read the output to determine what
went wrong.

I stopped mongo server under hosting kubuntu 20.04 with command :

    sudo systemctl stop mongod

can it be the issue that both hosting os and homestead use the same port ?

Bapquad's avatar

If you use 'php -m', it list mongodb. That you have a right mongdb extension version for your PHP. Otherwise, you have a wrong mongoDB extension version. If you have a right mongodb extension, but still meet the error. I think your server software (ex: apache) have a permission problem. You can try with Administrator right for it. Run As Adminstrator with windows or sudo when use *nix. Apache run PHP with php path, I set PHP path on environment path. It's solved my issue.

Please or to participate in this conversation.