Installing / Upgrading

PyMongo is in the Python Package Index.

Warning

Do not install the “bson” package from pypi. PyMongo comeswith its own bson package; doing “pip install bson” or “easy_install bson”installs a third-party package that is incompatible with PyMongo.

Installing with pip

We recommend using pipto install pymongo on all platforms:

  1. $ python -m pip install pymongo

To get a specific version of pymongo:

  1. $ python -m pip install pymongo==3.5.1

To upgrade using pip:

  1. $ python -m pip install --upgrade pymongo

Note

pip does not support installing python packages in .egg format. If you wouldlike to install PyMongo from a .egg provided on pypi use easy_installinstead.

Installing with easy_install

To use easy_install fromsetuptools do:

  1. $ python -m easy_install pymongo

To upgrade do:

  1. $ python -m easy_install -U pymongo

Dependencies

PyMongo supports CPython 2.7, 3.4+, PyPy, and PyPy3.5+.

Optional dependencies:

GSSAPI authentication requires pykerberos on Unix or WinKerberos on Windows. The correctdependency can be installed automatically along with PyMongo:

  1. $ python -m pip install pymongo[gssapi]

Support for mongodb+srv:// URIs requires dnspython:

  1. $ python -m pip install pymongo[srv]

TLS / SSL support may require ipaddress and certifi or wincertstore depending on the Pythonversion in use. The necessary dependencies can be installed along withPyMongo:

  1. $ python -m pip install pymongo[tls]

Wire protocol compression with snappy requires python-snappy:

  1. $ python -m pip install pymongo[snappy]

Wire protocol compression with zstandard requires zstandard:

  1. $ python -m pip install pymongo[zstd]

You can install all dependencies automatically with the followingcommand:

  1. $ python -m pip install pymongo[snappy,gssapi,srv,tls,zstd]

Other optional packages:

  • backports.pbkdf2,improves authentication performance with SCRAM-SHA-1 and SCRAM-SHA-256.It especially improves performance on Python versions older than 2.7.8.
  • monotonic adds support fora monotonic clock, which improves reliability in environmentswhere clock adjustments are frequent. Not needed in Python 3.

Installing from source

If you’d rather install directly from the source (i.e. to stay on thebleeding edge), install the C extension dependencies then check out thelatest source from GitHub and install the driver from the resulting tree:

  1. $ git clone git://github.com/mongodb/mongo-python-driver.git pymongo
  2. $ cd pymongo/
  3. $ python setup.py install

Installing from source on Unix

To build the optional C extensions on Linux or another non-macOS Unix you musthave the GNU C compiler (gcc) installed. Depending on your flavor of Unix(or Linux distribution) you may also need a python development package thatprovides the necessary header files for your version of Python. The packagename may vary from distro to distro.

Debian and Ubuntu users should issue the following command:

  1. $ sudo apt-get install build-essential python-dev

Users of Red Hat based distributions (RHEL, CentOS, Amazon Linux, Oracle Linux,Fedora, etc.) should issue the following command:

  1. $ sudo yum install gcc python-devel

Installing from source on macOS / OSX

If you want to install PyMongo with C extensions from source you will needthe command line developer tools. On modern versions of macOS they can beinstalled by running the following in Terminal (found in/Applications/Utilities/):

  1. xcode-select --install

For older versions of OSX you may need Xcode. See the notes below for variousOSX and Xcode versions.

Snow Leopard (10.6) - Xcode 3 with ‘UNIX Development Support’.

Snow Leopard Xcode 4: The Python versions shipped with OSX 10.6.xare universal binaries. They support i386, PPC, and x86_64. Xcode 4 removedsupport for PPC, causing the distutils version shipped with Apple’s builds ofPython to fail to build the C extensions if you have Xcode 4 installed. Thereis a workaround:

  1. # For some Python builds from python.org
  2. $ env ARCHFLAGS='-arch i386 -arch x86_64' python -m easy_install pymongo

See http://bugs.python.org/issue11623for a more detailed explanation.

Lion (10.7) and newer - PyMongo’s C extensions can be built againstversions of Python 2.7 >= 2.7.4 or Python 3.4+ downloaded frompython.org. In all cases Xcode must be installed with ‘UNIX DevelopmentSupport’.

Xcode 5.1: Starting with version 5.1 the version of clang that ships withXcode throws an error when it encounters compiler flags it doesn’t recognize.This may cause C extension builds to fail with an error similar to:

  1. clang: error: unknown argument: '-mno-fused-madd' [-Wunused-command-line-argument-hard-error-in-future]

There are workarounds:

  1. # Apple specified workaround for Xcode 5.1
  2. # easy_install
  3. $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future easy_install pymongo
  4. # or pip
  5. $ ARCHFLAGS=-Wno-error=unused-command-line-argument-hard-error-in-future pip install pymongo
  6.  
  7. # Alternative workaround using CFLAGS
  8. # easy_install
  9. $ CFLAGS=-Qunused-arguments easy_install pymongo
  10. # or pip
  11. $ CFLAGS=-Qunused-arguments pip install pymongo

Installing from source on Windows

If you want to install PyMongo with C extensions from source the followingrequirements apply to both CPython and ActiveState’s ActivePython:

64-bit Windows

For Python 3.5 and newer install Visual Studio 2015. For Python 3.4install Visual Studio 2010. You must use the full version of Visual Studio2010 as Visual C++ Express does not provide 64-bit compilers. Make sure thatyou check the “x64 Compilers and Tools” option under Visual C++. For Python 2.7install the Microsoft Visual C++ Compiler for Python 2.7.

32-bit Windows

For Python 3.5 and newer install Visual Studio 2015.

For Python 3.4 install Visual C++ 2010 Express.

For Python 2.7 install the Microsoft Visual C++ Compiler for Python 2.7

Installing Without C Extensions

By default, the driver attempts to build and install optional Cextensions (used for increasing performance) when it is installed. Ifany extension fails to build the driver will be installed anyway but awarning will be printed.

If you wish to install PyMongo without the C extensions, even if theextensions build properly, it can be done using a command line option tosetup.py:

  1. $ python setup.py --no_ext install

Building PyMongo egg Packages

Some organizations do not allow compilers and other build tools on productionsystems. To install PyMongo on these systems with C extensions you may need tobuild custom egg packages. Make sure that you have installed the dependencieslisted above for your operating system then run the following command in thePyMongo source directory:

  1. $ python setup.py bdist_egg

The egg package can be found in the dist/ subdirectory. The file name willresemble “pymongo-3.6-py2.7-linux-x86_64.egg” but may have a different namedepending on your platform and the version of python you use to compile.

Warning

These “binary distributions,” will only work on systems that resemble theenvironment on which you built the package. In other words, ensure thatoperating systems and versions of Python and architecture (i.e. “32” or “64”bit) match.

Copy this file to the target system and issue the following command to install thepackage:

  1. $ sudo python -m easy_install pymongo-3.6-py2.7-linux-x86_64.egg

Installing a beta or release candidate

MongoDB, Inc. may occasionally tag a beta or release candidate for testing bythe community before final release. These releases will not be uploaded to pypibut can be found on theGitHub tags page.They can be installed by passing the full URL for the tag to pip:

  1. $ python -m pip install https://github.com/mongodb/mongo-python-driver/archive/3.9.0b1.tar.gz

or easy_install:

  1. $ python -m easy_install https://github.com/mongodb/mongo-python-driver/archive/3.9.0b1.tar.gz