MongoDB Install on Ubuntu

MongoDB is a great NoSQL database - and if you use ubuntu you may want to install it! Let's go over how to install MongoDB on Ubuntu. πŸš€

1. Pre-installation Checklist:

Always Back Up: Before we begin, let me hit you with a golden rule of tech - Always back up your data! Ensure you've got a safe copy of your essential data. Awesome, moving on!

Update System Packages: It's always wise to start with the latest updates. It can save tons of headaches down the road.

sudo apt update && sudo apt upgrade -y
Or at the very least update your package list (sudo apt update)

2. Import MongoDB Public Key:

MongoDB is signed with a public key to ensure software consistency and authenticity. Let's import this key using the following:

wget -qO - https://www.mongodb.org/static/pgp/server-5.0.asc | sudo apt-key add -
Get Mongo Signing Key into Ubuntu Package Manager

3. Add MongoDB Source List:

Ubuntu repositories don't always house the latest MongoDB version. So, let's make sure we get the most updated one directly from MongoDB itself.
NOTE: The command will depend on the flavor of Ubuntu (pay attention to the label on the command, you only run one of these).

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
MongoDB for Ubuntu 22.04
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
MongoDB for Ubuntu 20.04
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
MongoDB for Ubuntu 18.04
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-5.0.gpg ] https://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/5.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-5.0.list
MongoDB for Ubuntu 16.04

4. Installing MongoDB:

We're about to bring MongoDB to life on your system!

sudo apt update
sudo apt install -y mongodb-org
update and install MongoDB on Ubuntu

That's it! MongoDB is now dancing on your machine. πŸ˜„

5. Starting and Enabling MongoDB:

Just installing isn’t enough. Time to set it as a service so it keeps running:

sudo systemctl start mongod
Start Mongo on Linux

And hey, if you want MongoDB to run every time your system does, simply use:

sudo systemctl enable mongod
Enable MongoDB on Linux

6. Verify Your Installation:

Let's confirm MongoDB is running smoothly:

sudo systemctl status mongod

If you see an β€œactive” status, give yourself a pat on the back! You did it!