Installing MongoDB on Ubuntu is only a few steps of procedure.
Let’s install MongoDB on Linux-Ubuntu using the native Ubuntu package manager
Note: This has been tested on Ubuntu 18.04
So let’s get started…
Install MongoDB on Ubuntu
Step 1. Add and Import the public key
wget -qO - https://www.mongodb.org/static/pgp/server-4.2.asc | sudo apt-key add -
Step 2. Create a Sources list file for MongoDB
echo "deb [ arch=amd64 ] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.2.list
Step 3. Reload local package database
sudo apt-get update
Step 4. Install the MongoDB packages
sudo apt-get install -y mongodb-org
And this will install MongoDB on our Ubuntu system. We will see how we can start & stop Mongo, but before that a few things to check .
Things to Check
Now, If you installed via the package manager, the data directory
/var/lib/mongodb and the log directory
/var/log/mongodb are created during the installation.
The official MongoDB package includes a configuration file which is located at
Check MongoDB version on Ubuntu
Mongo – Database Version
// Below is the output db version v4.0.9 git version: fc525e2d9b0e4bceff5c2201457e564362909765 OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017 allocator: tcmalloc modules: none build environment: distmod: ubuntu1804 distarch: x86_64 target_arch: x86_64
Mongo – Client Version
// Below is the output MongoDB shell version v4.0.9 git version: fc525e2d9b0e4bceff5c2201457e564362909765 OpenSSL version: OpenSSL 1.1.0g 2 Nov 2017 allocator: tcmalloc modules: none build environment: distmod: ubuntu1804 distarch: x86_64 target_arch: x86_64
Starting and Stopping MongoDB
There are a few methods we can start and stop MongoDB database. Let’s have a look at 2 of those methods.
Method 1: Using the `service` command
sudo service mongod start
sudo service mongod stop
sudo service mongod restart
Method 2: Using the `mongod` command
We will start mongo with a custom path for our database files. Mongo accepts a custom directory where we can choose to store our database at. So let’s start Mongo with a custom
Let’s first create a custom directory named
db1 where we can store our database.
Step 1. Create a custom folder
mkdir -p ~/projects/db/mongo/tutorials/db1
Step 2. Start Mongo with the custom folder as dbpath
And now start MongoDB with this directory where our database will be stored.
sudo mongod --dbpath ~/projects/db/mongo/tutorials/db1
Step 3. Verify
Verify that the `mongod` process has started successfully by checking our ports with the following command in a different terminal tab.
sudo netstat -plnt
Step 4. Stop
Press CTRL+C to end the MongoDB process that was started in Step 2 in that same terminal tab where it was started.
Begin using MongoDB.
Start a mongo shell on the same host machine as the
mongod. You can run the
mongo shell without any command-line options to connect to a
mongod that is running on your localhost with default port 27017:
Let’s uninstall the MongoDB server, client and all the related packages from our Ubuntu system.
Step 1. Stop MongoDB.
mongod process by issuing the following command.
sudo service mongod stop
Step 2. Remove Packages.
Remove any MongoDB packages that you had previously installed.
sudo apt-get purge mongodb-org*
Step 3. Remove Data Directories.
Remove the default MongoDB databases and log files, or any other custom database and log files that you created. We created 1 database directory (`~/projects/db/mongo/tutorials/db1`) here in our blog so we can include that too. But, we’ll just use the default directories for the moment.
sudo rm -r /var/log/mongodb sudo rm -r /var/lib/mongodb