Installing MongoDB and PyMongo on Ubuntu 14.04

Tags: , , , ,

Installing MongoDB and the associated Python API PyMongo to an existing Ubuntu 14.04 system with Python3 is straightforward, as Python3 is installed by default in Ubuntu 14.04.

Installing MongoDB

To install MongoDB’s latest stable version, add the MongoDB repository to the sources list for Ubuntu’s apt-get package manager, update the sources, and install:

This will install MongoDB, start the mongod daemon, and ensure that the daemon starts when the system is booted.

Installing PyMongo

As mentioned Python3 is installed by default, but you can confirm this with

$ python3 --version

from the command line–it should return “Python 3.4.0”. To install PyMongo, first install the Python package installer for Python3, pip3, and use this to get PyMongo:

Testing the installation

To test the installation, enter Python and create a MongoDB client that can connect to the ‘test’ database (installed by default):

$ python3
>>> from pymongo import MongoClient
>>> client = MongoClient()           # connects to the default database port on localhost
>>> db = client.test                 # connects to the 'test' database
>>> db.name
'test'