How to upgrade MongoDB from 2.6 to 3.4 on Ubuntu 16.04

You can do as following

$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 0C49F3730359A14518585931BC711F9BA15703C6

$ echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

$ sudo apt-get update
$ sudo apt-get install -y mongodb-org

after upgrading your service will be masked. To unmask it

sudo systemctl unmask mongodb

for more detail see:

  • How to Upgrade MongoDB to Latest Stable Version – Mongoaudit — the mongoaudit guides – Medium -> https://medium.com/mongoaudit/how-to-upgrade-mongodb-to-latest-stable-version-9607266834cf

  • error: Unit mongodb.service is masked when starting mongodb - Ask Ubuntu -> error: Unit mongodb.service is masked when starting mongodb

update:

If you use different ubuntu version, you should update <UBUNTU VERSION> in this command:

$ echo "deb http://repo.mongodb.org/apt/ubuntu <UBUNTU VERSION>/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list

For example, if you use 18.10, change it to cosmic You can find your version with this command

$ lsb_release -a