Installing MongoDB on Linux

Mandi Haase
3 min readNov 11, 2020

I just started my journey to the land of Linux (I use Ubuntu) after finding out that the combination of the Windows operating system and Cypress testing can result in unpleasant issues. Today was my first real battle (and victory) with installing packages. I would like to share my journey to getting the right version of MongoDB on my computer in hopes of saving others the pain associated with not installing MongoDB correctly.

So here is how I spent my morning — when running sudo apt autoremove to clean up unneeded packages on my computer, I was greeted with the following message:

You might want to run ‘apt — fix-broken install’ to correct these.
The following packages have unmet dependencies: mongodb-org-tools : Depends: mongodb-database-tools but it is not installed E: Unmet dependencies. Try ‘apt — fix-broken install’ with no packages (or specify a solution).

So, being a good girl, I did as prompted and ran apt fix-broken install only to get:

mandi@mandi:~$ apt — fix-broken install E: Could not open lock file /var/lib/dpkg/lock-frontend — open (13: Permission denied) E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?

Yes, I am in the root directory. Oh no….😒

Fixing the issue

After a lot of googling (thank you Google!!) I finally realized that I had made a terrible error in originally installing MongoDB using sudo apt-get install mongodb. MongoDB explicitly states the following on their Install on Ubuntu page:

IMPORTANT

The mongodb package provided by Ubuntu is not maintained by MongoDB Inc. and conflicts with the official mongodb-org package. If you have already installed the mongodb package on your Ubuntu system, you must first uninstall the mongodb package before proceeding with these instructions.

Note to self: ➡️ ALWAYS check the official documentation for a package before installing.⬅️

After reading numerous stack overflow articles, I finally decided the best option was to, in the words of one of my colleagues, “nuke and pave.” 😅 I completely uninstalled MongoDB with the help of this great article:

These are the commands I used:

sudo apt list — installed | grep mongo (check what I’ve got on my computer)

Which shows:

mongo-tools/focal,now 3.6.3–0ubuntu1 amd64 [installed,auto-removable]
mongodb-org-database-tools-extra/focal,now 4.4.1 amd64 [installed,automatic]
mongodb-org-mongos/focal,now 4.4.1 amd64 [installed,automatic]
mongodb-org-server/focal,now 4.4.1 amd64 [installed,automatic]
mongodb-org-shell/focal,now 4.4.1 amd64 [installed,automatic]
mongodb-org-tools/focal,now 4.4.1 amd64 [installed,automatic]
mongodb-org/focal,now 4.4.1 amd64 [installed]

Then:

sudo apt-get purge mongodb-org*

Then:

sudo rm -r /var/log/mongodb
sudo rm -r /var/lib/mongodb
sudo apt list — installed | grep mongo
sudo apt remove mongodb
(not sure if needed but did as a sanity check)

Then:

sudo apt purge mongodb (also not sure if needed)
sudo apt autoremove (this time it worked!!)🌟
sudo apt list — installed | grep mongo (showed nothing. Yahoo!!)🌟

Let’s try this again

Then I did what I should have done in the very beginning, which is go to MongoDB docs for installing on Ubuntu.

Now everything seems to be working properly and I can go on with my programming day~🏃‍♀️

--

--