Steps: Upgrade Mongodb version 3.2 to 4.2 on Ubuntu 16.04

Updating MongoDB servers are pretty straight forward but require you to update version to version (3.2 -> 3.4 -> 3.6 -> 4.0 -> 4.2). This is a very easy process and we will go step by step.

NOTE: The MongoDB data will not be affected by this process, avoid removing the /data folder which contains all mongo records.

SSH into your MongoDB instance.

MongoDB 3.2 -> 3.4

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-3.4.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.2.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.2.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “3.4”})

MongoDB 3.4 -> 3.6

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-3.6.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.6 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-3.6.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.4.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.4.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “3.6”})

MongoDB 3.6 -> 4.0

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-4.0.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.6.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-3.6.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “4.0”})

MongoDB 4.0 -> 4.2

1) Run the following command to import the public key.

wget -qO – https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add –

2) Run the following command to create a source list file for MongoDB 3.4.

echo “deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/4.2 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list

3) Run the following command:

sudo apt-get update

Note: If you receive an error when updating the packages, there is most likely an issue with mongob-org.3.6.list in the sources.list.d folder. Remove it with the following command.

sudo rm /etc/apt/sources.list.d/mongodb-org-4.0.list

4) Restart the mongo service:

sudo service system restart

5) Verify the mongo service is running:

sudo service system status

6) Run mongo and set the compatibility version:

sudo mongo

db.adminCommand({setFeatureCompatibilityVersion: “4.2”})


That’s it! Simple steps to update to each version of Mongo on any environment.