Built with Tutify.io

    AIStudyLabs

    Install MongoDB On Ubuntu

    Install MongoDB On Ubuntu

    If we use Ubuntu 20.04 and want to leverage the power of MongoDB, this article will walk us through the installation process step by step. Following these instructions, we can quickly install MongoDB on Ubuntu 20.04 machine. It's usually a good idea to upgrade any current packages and update the package lists of the system before beginning the installation procedure.

    <a id="h-1-nbsp-import-the-mongodb-gpg-key-into-ubuntu"></a>

    1.  Import the MongoDB GPG Key into Ubuntu

    Importing the MongoDB GPG key is the first step in the installation process, which enables us to confirm the legitimacy of the MongoDB packages. To add a GPG key, enter the command described below.

    
    # Importing the MongoDB GPG key 
    wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
    

    The key is successfully added, as shown on the Ubuntu terminal.

    <a id="h-2-add-the-mongodb-repository"></a>

    2. Add the MongoDB Repository

    Installing MongoDB requires adding its repository to the system's package sources, which we must do next. To generate a list file for MongoDB, perform the following command.

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

    <a id="h-3-update-the-repository-of-mongodb"></a>

    3. Update the Repository of MongoDB

    Once the MongoDB repository has been added, update the package lists to include the newly added repository. Use the following command, which may take new minutes.

    
    # update the package lists
    sudo apt-get update
    

    Therefore, the MongoDB packages are all updated, as seen in the following image.

    <a id="h-4-installation-of-mongodb-in-ubuntu"></a>

    4. Installation of MongoDB in Ubuntu

    Now that the entire process is configured, we can install MongoDB on the Ubuntu system. To begin the installation procedure, use the following command.

    
    # install MongoDB
    sudo apt-get install mongodb-org=4.4.8 mongodb-org-server=4.4.8 mongodb-org-shell=4.4.8 mongodb-org-mongos=4.4.8 mongodb-org-tools=4.4.8
    

    <a id="h-5-verify-mongodb-status-in-ubuntu"></a>

    5. Verify MongoDB Status in Ubuntu

    Here, we have to examine the status of MongoDB, whether it is active on our system or not. The MongoDB service must first be started using the systemctl command to do this. Pass the following command.

    
    # examine the status of MongoDB
    sudo systemctl start mongod
    

    Then, use the following command to enable MongoDB and make sure it begins when the system boots up.

    
    # enable MongoDB
    sudo systemctl enable mongod
    

    Now, MongoDB is up and running, ready to serve the applications. Let's ensure that everything went without a hitch. To verify MongoDB's status, enter the following command.

    
    # verify MongoDB's status
    sudo systemctl status mongod
    

    If everything operates as it should, the output should read "active (running)," as shown below.

    <a id="h-6-check-mongodb-version-in-ubuntu"></a>

    6. Check MongoDB Version in Ubuntu

    As the MongoDB services are now active in the Ubuntu system. Hence, we can figure out the version of MongoDB installed with the system. For this, we passed the below command on the terminal, which displayed the current version of MongoDB.

    
    # figure out the version of MongoDB 
    mongod --version
    

    We can see the version of MongoDB below installed in our Ubuntu system.

    <a id="h-7-connect-to-mongodb"></a>

    7. Connect to MongoDB

    We have successfully installed MongoDB here on our Ubuntu 20.04 system. It's time to unleash creativity and build amazing applications powered by this powerful NoSQL database. Here, we interact with MongoDB by using the MongoDB shell, also known as the Mongo shell. To connect to the nearby MongoDB instance, merely type the following command:

    
    # Connect to MongoDB
    mongo
    

    The mongo shell is now connected with the following connection string on the console below.

    We can perform some of the basic queries in the mongo shell there. Firstly, we switch to the specific database and then insert a document using the insert() method.  Note that we can exist from the mongo shell in Ubuntu by just pressing the ctrl+c keys. Thus, following these instructions will have MongoDB up and running on our Ubuntu 20.04 system in no time.

    <a id="h-8-conclusion"></a>

    8. Conclusion

    In conclusion, a few easy steps can be taken to successfully install MongoDB on Ubuntu 20.04 straightforwardly. MongoDB is a great option for various applications because of its flexibility and scalability, which allow us to store and retrieve data quickly. Remember to check for updates and look through the MongoDB manual for additional configuration and optimization advice.

    More details about this topic can be found here.

    <a id="h-related-articles"></a>

    Related Articles