Instalando a extensão MongoDB PHP com PECL

Informações para instalar esta extensão PECL podem ser encontradas no capítulo Instalação de Extensões PECL do manual. Informações adicionais, como novas versões, downloads, arquivos fontes, informações do mantenedor e um CHANGELOG, podem ser encontradas aqui: » https://pecl.php.net/package/mongodb

Usuários de Linux, Unix e macOS podem executar o seguinte comando para instalar a extensão:

$ sudo pecl install mongodb

Em sistemas com múltiplas versões de PHP instaladas (por exemplo, macOS padrão, Homebrew, » XAMPP), cada versão do PHP terá seu próprio comando pecl e arquivo php.ini. Além disso, cada ambiente PHP (por exemplo, CLI, web) podem usar arquivos php.ini separados.

A partir da versão de extensão 1.17.0, o PECL solicitará várias opções de configure. Para instalar a extensão com opções padrão em um script não interativo, a entrada de string vazia pode ser canalizada para pecl install usando o comando yes:

$ yes '' | sudo pecl install mongodb

Uma lista completa de opções configure suportadas pode ser encontrada no arquivo package.xml incluído no pacote PECL. Para instalar a extensão com opções configure específicas em um script não interativo, a opção --configureoptions para pecl install pode ser usada:

$ sudo pecl install --configureoptions='with-mongodb-system-libs="yes" enable-mongodb-developer-flags="no"' mongodb

Por padrão, a instalação da extensão via PECL usará versões agrupadas de » libbson, » libmongoc e » libmongocrypt e tentará configurá-los automaticamente.

Nota: Se o processo de construção não conseguir encontrar uma biblioteca SSL, verifique se os pacotes de desenvolvimento (por exemplo, libssl-dev) e » pkg-config estão ambos instalados. Se isso não resolver o problema, considere usar o processo de instalação manual.

Finalmente, adicione a seguinte linha ao arquivo php.ini para cada ambiente que precisará usar a extensão:

extension=mongodb.so

add a note

User Contributed Notes 9 notes

up
39
mohammadhoseinmazalahi at gmail dot com
4 years ago
As of Ubuntu 20.04, php-mongodb in the database is outdated and you'll need the PECL method to install latest version of MongoDB.

To do this in Ubuntu, you'll need the php-pear plugin and php-dev:
sudo apt install php-dev php-pear

then you can run:
sudo pecl install mongodb
up
27
contact ? automatix : info
8 years ago
An additional requirement might be pkg-config (on Ubuntu 14.04).

$ pecl install mongodb
...
configure: error: Cannot find OpenSSL's libraries
ERROR: `/tmp/pear/temp/mongodb/configure --with-php-config=/usr/bin/php-config' failed

But:

$ apt-get install pkg-config
...
Setting up pkg-config (0.26-1ubuntu4) ...
$ pecl install mongodb
...
Build process completed successfully
Installing '/usr/lib/php/20151012/mongodb.so'
install ok: channel://pecl.php.net/mongodb-1.1.7
configuration option "php_ini" is not set to php.ini location
You should add "extension=mongodb.so" to php.ini
up
14
asologor at gmail dot com
6 years ago
On Ubuntu you can just do:

sudo apt-get install php-mongodb

or the same for specific PHP version:

sudo apt-get install php5.6-mongo

or

sudo apt-get install php7.0-mongodb
up
16
rafael_xuvisco at yahoo dot com dot br
7 years ago
Ubuntu 16.04

sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev

sudo pecl install mongodb

add extension=mongodb.so in fpm and cli:

sudo vim /etc/php/7.0/fpm/conf.d/30-mongodb.ini

sudo vim /etc/php/7.0/cli/conf.d/30-mongodb.ini

Restart service:

sudo systemctl restart php7.0-fpm

sudo systemctl reload nginx
up
6
Tim Greiser
8 years ago
[Editor's note: fixed typo]

There are some additional requirements you need to build the pecl package.

sudo apt-get install libcurl4-openssl-dev pkg-config libssl-dev libsslcommon2-dev
up
6
Anonymous
7 years ago
I got an error "fatal error: pcre.h: No such file or directory" and had to also had to install another dependency

sudo apt-get install libpcre3-dev
up
-1
Unmesh
6 years ago
The following steps worked for me on Centos7

sudo yum install -y openssl-devel
sudo yum install pecl
sudo yum install gcc
sudo pecl install mongodb
up
-13
maneesh dot singh at hof-university dot com
7 years ago
Hi,
I am looking into installation of php mongo db driver.
I have checked with phpinfo it shows mongodb module have been loaded but when I use in my code it give error "Fatal error: Uncaught Error: Class 'MongoDB' not found "

i have another module for redis and it works perfectly . Initially I was getting same error but phpinfo was showing module is not loaded .

I guess there is some problem with driver. I have tried all the documents and website links which mention to include extenstion=mongodb.so which i have already done.

I am using open suse 42.2 leap with apache server.

Could you please investigate this issue.
If you need more info please let me know.

Thanks
Maneeshg
up
-12
ovidius at email dot it
7 years ago
after installed and added extension=mongodb to php.ini, when i run php -v i get this error:

bus error php -v

what could it be? the build process completed successfully.
To Top