For enquiries call:

Phone

+1-469-442-0620

April flash sale-mobile

HomeBlogWeb DevelopmentHow to Install MongoDB on Ubuntu [Step-by-Step]

How to Install MongoDB on Ubuntu [Step-by-Step]

Published
24th Jan, 2024
Views
view count loader
Read it in
6 Mins
In this article
    How to Install MongoDB on Ubuntu [Step-by-Step]

    MongoDB: An Overview 

    Setting up MongoDB on Ubuntu turned out to be more challenging than I expected. If you're like me and still searching for a detailed guide on installing MongoDB on Ubuntu, you're in the right spot.

    In this blog, I'll share my experience and guide you through the process of installing MongoDB on Ubuntu. I aim to make the installation as smooth as possible by providing an easy, step-by-step approach.
    . 

    Let us start with the basics before we dive deeper to understand the tools and techniques involved. If you are looking forward to becoming a Certified MongoDB practitioner, please check out the MongoDB certification price. 

    MongoDB Version

    In this guide, we will install MongoDB 6.0 Community Edition. In case you want to install a different version, you can use the version drop-down menu in the upper-left corner of the MongoDB community page to select the documentation for the required community version. 

    MongoDB Refresher

    To get started, let us refresh our understanding of MongoDB first. MongoDB is a popular NoSQL database that is open-source and document-oriented. 'NoSQL' here implies that it is a non-relational database, i.e., it stores the data in a different format other than the relational tables and therefore does not require a fixed schema. The storage format in which the data is stored is known as BSON (Binary JavaScript Object Notation). This binary structure encodes the information allowing it to be parsed much more quickly, thus allowing a highly scalable and flexible document structure. 

    Though there is more to it than meets the eye, if you want to learn more about this powerful database you can check out MongoDB DBA online training. 

    Advantages of MongoDB Database

    • It is a cloud-based developer data platform. 
    • It has flexible document schemas. 
    • Provides powerful querying and analytics. 
    • Easy to scale horizontally with sharding. 
    • Simple installation on all platforms. 
    • Cost-effective and efficient. 

    Introduction to Ubuntu

    Ubuntu is a Linux-based operating system that is designed for computers, smartphones, and network servers.  

    It is developed by a UK-based company called Canonical Ltd. All the principles used to develop the Ubuntu software are based on Open-Source software development. 

    Features of Ubuntu

    The following are some of the significant features of Ubuntu −  

    • The desktop version of Ubuntu supports all software associated with Windows, such as Firefox, Chrome, VLC, etc.  
    • Ubuntu provides an in-built email software called Thunderbird, which gives access to emails such as Exchange, Gmail, Hotmail, etc.  
    • It also provides a host of complimentary applications to view and edit photos.  
    • Moreover, it provides applications to manage videos and allows its users to share videos. 

    Advantages of Ubuntu

    Some Advantages of using Ubuntu in your system are as follows: 

    1. Ubuntu is open-Source and free. 
    2. Ubuntu updates without restarting. 
    3. Ubuntu is thoroughly customizable. 
    4. Ubuntu is more secure than alternatives. 
    5. Ubuntu is backed by a huge open-source community. 

    Considerations

    Some considerations before getting into the installation of MongoDB on Ubuntu. 

    • MongoDB 5.0 Community Edition has removed support for Ubuntu 16.04 on x86_64 
    • MongoDB 5.0 Community Edition has also removed support for Ubuntu 18.04 on s390x 

    MongoDB 6.0 Community Edition supports the following 64-bit Ubuntu LTS releases: 

    • 20.04 LTS ("Focal") 
    • 18.04 LTS ("Bionic") 
    • 16.04 LTS ("Xenial") 

    MongoDB only supports the 64-bit versions of these platforms. 

    MongoDB 6.0 Community Edition also supports the ARM64 architecture on Ubuntu. 

    Production Notes

    It is highly recommended to consider the Production Notes document before deploying MongoDB in a production environment; they provide performance considerations and configuration recommendations for production deployments. 

    Official MongoDB Packages

    These instructions will use the official MongoDB-org package to install MongoDB Community on your Ubuntu system, which is maintained and supported by MongoDB Inc. The most recent version of MongoDB is always included in the official MongoDB-org package, which is accessible from a dedicated repository. 

    Prerequisites

    Some Prerequisite knowledge is needed for you to easily carry out the installation, like, 

    • Working knowledge of MongoDB is necessary. 
    • MongoDB is installed on the host workstation. 
    • A broad idea of operating command line/shell commands. 
    • Ubuntu is installed as the Operating System on the host Machine. 

    How to Install and Configure MongoDB on Ubuntu [Step-by-Step]

    Now let us dive into the step-by-step process of installing MongoDB on Ubuntu, we will try to analyze every step to understand what it is used for. MongoDB certification can give you a deeper understanding on the workings of MongoDB, check out here KnowledgeHut’s MongoDB certification price.  

    Step 1: Import the public key used by the package management system

    The first step is to import the MongoDB public GPG Key from mongoDB 

    On a terminal, issue the following command for the same: 

    > wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - 

    The operation should respond with an OK. 

    However, if you get an error implying that GnuPG is not installed, you can follow the below steps: 

    1. Use the following command to Install GnuPG and its required libraries: 

    sudo apt-get install gnupg 

    2. Once installation is done, retry importing the key: 

    > wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - 

    Step 2: Create a list file for MongoDB

    Step two is to create the list file /etc/apt/sources.list.d/mongodb-org-6.0.list for your version of Ubuntu. 

    Click on the appropriate tab for your version of Ubuntu. In case you are unsure of what Ubuntu version is running on the host, open a terminal or shell on the host machine and execute the command to check mongodb version ubuntu: 

    > lsb_release -dc. 

    The following set of instructions is to install MongoDB Ubuntu 20.04 (Focal) as an example 

    Create the /etc/apt/sources.list.d/mongodb-org-6.0.list file for Ubuntu 20.04 (Focal): 

    > echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list 

    Step 3: Reload the local package database

    Here we will try to reload the local package database using the following command on a terminal: 

    > sudo apt-get update 

    Step 4: Install the MongoDB packages

    You can install any specific version of MongoDB, though for now, we will be installing the latest stable version of MongoDB. To install the latest stable version, issue the following command: 

    > sudo apt-get install -y mongodb-org 

    Although you can specify any available version of MongoDB, the `apt-get` command will upgrade the packages when a newer version becomes available. 

    Install a specific release of MongoDB

    Optionally, if you wish to install a certain release, you will have to specify each component package individually along with the version number, as can be seen in the below example: 

    > sudo apt-get install -y mongodb-org=6.0.1 mongodb-org-database=6.0.1 mongodb-org-server=6.0.1 mongodb-mongosh=6.0.1 mongodb-org-mongos=6.0.1 mongodb-org-tools=6.0.1 

    If you exclusively install MongoDB-org=6.0.1 without the component packages, the latest version of each MongoDB package will be installed irrespective of the version specified by you.  

    The above commands will install several packages containing a stable version of MongoDB and some helpful management tools for the MongoDB server.   

    The installation process will start the MongoDB server automatically, but it's good to verify that the service is started and that the database is working. 

    First, check the service's status by using the below command 

    > sudo systemctl status mongodb 

    You should see the below output: 

    According to systemd, the MongoDB server is up and running. 

    We can verify this practically by connecting to the database server and executing a diagnostic command as below: 

    > mongo --eval 'db.runCommand({ connectionStatus: 1 })' 

    The output of this command will be the server address and port, the current database version, and the output of the status command:

    Next, let us check out how to manage the server instance. 

    How to Check MongoDB is Installed? 

    MongoDB is installed as a systemd service; therefore, you can manage it using standard systemd commands side-by-side with all other system services in Ubuntu. 

    We can also verify the status of the service by using the below command: 

    > sudo systemctl status mongodb 

    You can stop the server anytime by typing: 

    > sudo systemctl stop mongodb 

    To start MongoDB ubuntu server when it is stopped, type: 

    > sudo systemctl start mongodb 

    Create MongoDB Database Root User and Password

    Once you've set up MongoDB as a service, you now need to launch your MongoDB installation.  

    After you've configured MongoDB as a service, you'll need to launch your MongoDB installation. Let us do this by opening up the Mongo Shell and switching to the database admin mode using the following two commands: 

    > mongo  
    > use admin 

    Now here we will create a root user for your MongoDB installation and simultaneously exit the Mongo Shell using the below command: 

    > db.createUser({user:"admin", pwd:"password", roles:[{role:"root", db:"admin"}]}) 

    Let us now connect with the MongoDB server using our newly created credentials, by first restarting the server and then using the following line of code: 

    > mongo -u admin -p admin123 --authenticationDatabase admin 

    And if everything works fine, you should be able to see the successful connection and to check the same we can use the "show dbs" command to see all the databases. 

    > show dbs 

    Run MongoDB Community Edition

    To run MongoDB Community Edition on your machine, follow the below instructions. These instructions assume that you are utilizing the official mongodb-org package and the default settings rather than the unofficial MongoDB package given by Ubuntu. 

    Init System 

    Here we will use our operating system's built-in init system to run and control the mongod process on the host machine. The recent Linux versions utilize systemd (through the systemctl command), but previous Linux versions use System V init (which uses the service command). 

    If you're not sure which init system your platform uses, try the below command can help you out: 

    > ps --no-headers -o comm 1 

    Then select the appropriate tab below based on the result: 

    • systemd - select the systemd (systemctl) tab below. 
    • init - select the System V Init (service) tab below. 

    Uninstall MongoDB Community Edition

    Warning: All databases and their respective configurations will be removed after this process completes. Before initiating this step, create a backup of all your data and configuration information, considering it is irreversible.  

    To uninstall MongoDB Ubuntu, you first need to remove the MongoDB packages and to do so, and you need to stop any running instance of MongoDB. The following section will guide you through the steps to remove MongoDB from ubuntu. 

    1. Stop MongoDB

    Stop the MongoDB process by issuing the following command: 

    > sudo service mongod stop 

    2. Remove Packages

    Remove any MongoDB packages that you had previously installed using the following command. 

    > sudo apt-get purge mongodb-org* 

    3. Remove Data Directories 

    Remove MongoDB databases and log files. 

    > sudo rm -r /var/log/mongodb 
    > sudo rm -r /var/lib/mongodb 

    This is how you can uninstall MongoDB on Ubuntu. 

    Unlock your coding potential with the best Python programming certification. Master the language that powers AI, web development, and more. Start your journey today!

    Conclusion

    In this article, I have described the steps on how to install MongoDB on Ubuntu. I also provided information on how to manage the server instance, create a root user and password, run MongoDB Community Edition, and uninstall MongoDB Community Edition. I hope this guide has been helpful. If you have any questions, please feel free to leave a comment below. 

    Frequently Asked Questions (FAQs)

    1Does Ubuntu come with MongoDB?

    Yes, but this MongoDB package provided by Ubuntu isn't maintained by MongoDB Inc. and conflicts with the official MongoDB-org package.

    2Where does MongoDB install on Ubuntu?

    If you have installed via the package manager, the data directory /var/lib/mongodb and the log directory /var/log/mongodb are created during the installation. MongoDB runs using the MongoDB user account by default.

    3How do I know if MongoDB is running on Ubuntu?

    MongoDB is installed as a systemd service, therefore, you can manage it using standard systemd commands side-by-side all other system services in Ubuntu. 

    To verify the status of the service, type: 

    sudo systemctl status mongodb  

    4Where is MongoDB located?

    MongoDB stores data on the host disk as BSON, the path directory usually is /data/db. There should be two files per collection, collection. 0, which stores the data (and that integer is then incremented as per need) and collection.

    5How do I start MongoDB in the terminal?

    To Start MongoDB command Ubuntu, open up your command prompt and type mongod to start the MongoDB server.

    Profile

    Nikhilesh Pandey

    Author

    "Experienced Lead Developer oriented towards problem-solving and analyzing impacts, both technically and functionally, Enjoy's working with web technologies with a strong focus on quality code and robust system architecture."

    Share This Article
    Ready to Master the Skills that Drive Your Career?

    Avail your free 1:1 mentorship session.

    Select
    Your Message (Optional)

    Upcoming Web Development Batches & Dates

    NameDateFeeKnow more
    Course advisor icon
    Course Advisor
    Whatsapp/Chat icon