Show/Hide Toolbars

MongoDB Notes

Navigation: MongoDB Notes Overview

Setting Up a MongoDB Virtual Machine

Scroll Prev Top Next More


In this procedure, you will create a virtual machine running Ubuntu and MongoDB Enterprise, Version 3.6.  Employing Vagrant to set up a virtual machine with MongoDB is useful for developers.  A developer can destroy and then rapidly recreate an MongoDB environment.  If a project employs multiple developers or testers, each person can rapidly set up an environment that is consistent for each member of the staff.




This procedure assumes:


VirtualBox is installed

Vagrant is installed


Procedure (Host: Windows 10, Guest: Ubuntu)


1.Create a directory for the Vagrant configuration.  Call it \vmmongo.

2.Open a Command Prompt window.

3.Change directories to \vmmongo.

4.Create a subdirectory shared: mkdir shared

5.Execute these Vagrant command.


vagrant init bento/ubuntu-16.04

vagrant up


4.Edit the file Vagrantfile so it looks like the "Example Vagrantfile for Ubuntu".  Set the appropriate values for the network IP address and host name.

5.Create a provisioning file like the "Example MongoDB Provisioning File".  Name the file provision-mongodb.

6.Using Vagrant, provision the virtual machine using this command:


vagrant provision


7.Log into the virtual machine using this command:


vagrant ssh


Updating the Virtual Machine (Guest: Ubuntu)


In some cases, you may wish to replace an existing virtual machine with a new instance.  For example, you may wish to change the version of MongoDB in the virtual machine.  Use this procedure.  You can reuse the existing Vagrantfile.  Prior to performing this procedure, you should update the provisioning file to reflect any changes in the configuration.


1.Change directories to \vmmongo.

2.Open a Command Prompt window.

3.Enter the following command:


vagrant up --provision


4.Log into the virtual machine using this command:


vagrant ssh


5.In the Linux shell, update the /etc/hosts file to add the IP address "" and host name


Example Vagrantfile for Ubuntu


# -*- mode: ruby -*-


Vagrant.configure("2") do |config|


  # Ubuntu Version 16.04 LTS = "bento/ubuntu-16.04"


  # Create a private network, which allows host-only access to the machine

  # using a specific IP address. "private_network", ip: ""

  config.vm.hostname = ""

  config.vm.synced_folder "shared/", "/home/vagrant/shared", create: false


  config.vm.provider "virtualbox" do |vb|

     # Do not display the VirtualBox GUI when booting the machine

     vb.gui = false = "MongoDB"

     # Customize the amount of memory on the VM:

     vb.memory = 2048



   config.vm.provision "shell", path: "provision-mongodb"  




Example MongoDB Provisioning File


This file installs the latest version of MongoDB Enterprise edition version 3.6.


#!/usr/bin/env bash


echo "Import MongoDB Public Key - Updated for MongoDB Version 3.6"

sudo apt-key adv --keyserver hkp:// --recv 2930ADAE8CAF5059EE73BB4B58712A2291FA4AD5

echo "Create list file for MongoDB"

echo "deb [ arch=amd64,arm64,ppc64el,s390x ] xenial/mongodb-enterprise/3.6 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-enterprise.list

sudo apt-get update -y

sudo apt-get install -y mongodb-enterprise

sudo apt-get upgrade -y

echo "Virtual machine provisioned"


Additional Information


After the virtual machine is provisioned, Vagrant has set up a user named vagrant with a password vagrant.  

Vagrant sets up access to the sudo command so that no password is required.

This default configuration is obviously insecure, hence the need to configure the network access as host-only.

Production environments require changes to these settings.





Vagrant Documentation

Vagrant Up and Running