MongoDB is a document database available for Linux, MacOS, and Windows operating systems. It offers a free, self-managed Community Edition, as well as a paid version. In this tutorial, we will see how to install the free MongoDB Community Edition on an Ubuntu Linux system.
Installing MongoDB Community Edition on Ubuntu
Open the terminal application (ctrl+alt+t). If you don’t have gnupg and curl, the first install using below command.
sudo apt-get install gnupg curl
Now import MongoDB public GPG key, using below command.
curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | \ sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg \ --dearmor
Now create a list file for MongoDB using below command.
echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
Now update the local package database.
sudo apt update
And finally install the latest version MongoDB using this command.
sudo apt-get install -y mongodb-org
Run MongoDB Community Edition on Ubuntu
You can start the mongod process using below command.
sudo systemctl start mongod
And enable using below command.
sudo systemctl enable mongod
You can check the status using below command.
sudo systemctl status mongod
Now start a mongosh session as the mongod. You can run mongosh without any command-line options to connect to a mongod that is running on your localhost with default port 27017.
mongosh
If you want to stop and restart MongoDB, the use below commands.
sudo systemctl stop mongod
sudo systemctl restart mongod