{"id":4977,"date":"2021-03-11T02:36:05","date_gmt":"2021-03-11T02:36:05","guid":{"rendered":"https:\/\/linuxways.net\/?p=4977"},"modified":"2021-03-13T06:05:08","modified_gmt":"2021-03-13T06:05:08","slug":"how-to-install-mongodb-on-ubuntu-20-04-lts","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-mongodb-on-ubuntu-20-04-lts\/","title":{"rendered":"How to Install MongoDB on Ubuntu 20.04 LTS"},"content":{"rendered":"<p>MongoDB (known as Mongo) is a document-oriented database management system. It is categorized as a NoSQL database as it is a non-relational database and does not work with the conventional table-based relational database structure. It efficiently works will large-scale data. Some of the well-known companies which use MongoDB are Facebook, Cisco, Forbes, Adobe, Nokia, etc.<\/p>\n<p>This article describes how to install MongoDB on the Ubuntu 20.04 LTS system. You can follow the same procedure on previous Ubuntu releases: Ubuntu 18.04 LTS and 16.04 LTS.<\/p>\n<p>We can install MongoDB on Ubuntu through following two ways:<\/p>\n<ul>\n<li>Using package manager<\/li>\n<li>Using archive<\/li>\n<\/ul>\n<p><strong>Note<\/strong>: You will require sudo access to install the MongoDB on the Ubuntu OS.<\/p>\n<h2>Installing MongoDB Using Package Manager<\/h2>\n<p>The MongoDB package available in Ubuntu is not managed by MongoDB Inc. Here, we will show you the installation of the official MongoDB package version <strong>4.4<\/strong>. which is managed by MongoDB Inc. Follow the below instructions to install MongoDB using the Ubuntu package manager.<\/p>\n<h3>Step 1: Import MongoDB Public Key<\/h3>\n<p>First, you will need to add the MongoDB repository key to your apt keyring. By doing this, your system will trust the added repository. Run this command to add the MongoDB key.<\/p>\n<pre>\r\n$ wget -qO - https:\/\/www.mongodb.org\/static\/pgp\/server-4.4.asc | sudo apt-key add -\r\n<\/pre>\n<p>If it is added successfully, you will see <strong>OK<\/strong> in the output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"810\" height=\"88\" class=\"wp-image-4978\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-93.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-93.png 810w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-93-300x33.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-93-768x83.png 768w\" sizes=\"auto, (max-width: 810px) 100vw, 810px\" \/><\/p>\n<h3>Step 2: Add MongoDB Repository<\/h3>\n<p>Add MongoDB repository to your system\u2019s list of sources. To do so, edit the <strong>sources.list<\/strong> file using the command below:<\/p>\n<pre>\r\n$ sudo nano \/etc\/apt\/sources.list\r\n<\/pre>\n<p>Append the below line in the file:<\/p>\n<pre>\r\ndeb [ arch=amd64,arm64 ] https:\/\/repo.mongodb.org\/apt\/ubuntu focal\/mongodb-org\/4.4 multiverse\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"949\" height=\"218\" class=\"wp-image-4979\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-94.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-94.png 949w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-94-300x69.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-94-768x176.png 768w\" sizes=\"auto, (max-width: 949px) 100vw, 949px\" \/><\/p>\n<p>Now save the file and close it.<\/p>\n<h3>Step 3: Update Repository Index<\/h3>\n<p>Now use the below command to update your system\u2019s repository index:<\/p>\n<pre>\r\n$ sudo apt update\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"738\" height=\"133\" class=\"wp-image-4980\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-95.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-95.png 738w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-95-300x54.png 300w\" sizes=\"auto, (max-width: 738px) 100vw, 738px\" \/><\/p>\n<h3>Step 4: Install MongoDB<\/h3>\n<p>Now the MongoDB repository has been added to our system\u2019s list of sources, we can install it as follows:<\/p>\n<pre>\r\n$ sudo apt install mongodb-org\r\n<\/pre>\n<p>When prompted with the <strong>y\/n<\/strong> option, press <strong>y<\/strong> to continue.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"958\" height=\"385\" class=\"wp-image-4981\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-96.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-96.png 958w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-96-300x121.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-96-768x309.png 768w\" sizes=\"auto, (max-width: 958px) 100vw, 958px\" \/><\/p>\n<h3>Step 5: Run MongoDB<\/h3>\n<p>To run MongoDB, start the mongod service (daemon for MongoDB) using the command below:<\/p>\n<pre>\r\n$ sudo systemctl start mongod\r\n<\/pre>\n<p>If the service does not start or you encounter an error like \u201cservice not found\u201d, issue the command below:<\/p>\n<pre>\r\n$ sudo systemctl daemon-reload\r\n<\/pre>\n<p>After starting the mongod service, check its status to verify if it is running fine. Use the command below to do so:<\/p>\n<pre>\r\n$ sudo systemctl status mongod\r\n<\/pre>\n<p>If it is running fine, you will see the below output.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"907\" height=\"94\" class=\"wp-image-4982\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-97.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-97.png 907w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-97-300x31.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-97-768x80.png 768w\" sizes=\"auto, (max-width: 907px) 100vw, 907px\" \/><\/p>\n<p>To start the MongoDB automatically at each boot, the command is:<\/p>\n<pre>\r\n$ sudo systemctl enable mongod\r\n<\/pre>\n<p>Now to start the mongo shell from the same system running the mongod process, the command is as follows:<\/p>\n<pre>\r\n$ mongo\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"882\" height=\"134\" class=\"wp-image-4983\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-98.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-98.png 882w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-98-300x46.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-98-768x117.png 768w\" sizes=\"auto, (max-width: 882px) 100vw, 882px\" \/><\/p>\n<h2>Installing MongoDB Using Archive<\/h2>\n<p>Although, the recommended way of installing MongoDB on Ubuntu is by using apt package manager as it installs the required dependencies during the installation. However, you can use archive (.tgz tarball) for manually installing it in Ubuntu. Follow the below instructions to install MongoDB using the using official MongoDB archive.<\/p>\n<h3>Step 1: Install Dependencies<\/h3>\n<p>Use the command below to install the dependencies needed for the installation of MongoDB:<\/p>\n<pre>\r\n$ sudo apt install libcurl4 openssl liblzma5\r\n<\/pre>\n<p>When prompted with the <strong>y\/n<\/strong> option, press <strong>y<\/strong> to continue.<\/p>\n<h3>Step 2: Download MongoDB archive<\/h3>\n<p>Now you will need to download the MongoDB archive (.tgz tarball). Use the below command to download the current latest version of MongoDB to your system:<\/p>\n<pre>\r\n$ wget https:\/\/fastdl.mongodb.org\/linux\/mongodb-linux-x86_64-ubuntu2004-4.4.4.tgz\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1266\" height=\"293\" class=\"wp-image-4984\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-99.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-99.png 1266w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-99-300x69.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-99-1024x237.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-99-768x178.png 768w\" sizes=\"auto, (max-width: 1266px) 100vw, 1266px\" \/><\/p>\n<p>Alternatively, you can also download MongoDB by accessing their official <a href=\"https:\/\/www.mongodb.com\/try\/download\/community?tck=docs_server\">Downloads<\/a> page. Access the page and choose the MongoDB version you want to download, your OS platform, and the package (tgz). Then click <strong>Download<\/strong> to download the package.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"771\" height=\"326\" class=\"wp-image-4985\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-100.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-100.png 771w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-100-300x127.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-100-768x325.png 768w\" sizes=\"auto, (max-width: 771px) 100vw, 771px\" \/><\/p>\n<h3>Step 3: Extract MongoDB Archive<\/h3>\n<p>Extract the MongoDB archive using the command below:<\/p>\n<pre>\r\n$ tar -xvzf mongodb-linux-x86_64-ubuntu2004-4.4.4.tgz\r\n<\/pre>\n<p>This extracts the files from the archive as clear in the below screenshot: <img loading=\"lazy\" decoding=\"async\" width=\"843\" height=\"241\" class=\"wp-image-4986\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-101.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-101.png 843w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-101-300x86.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-101-768x220.png 768w\" sizes=\"auto, (max-width: 843px) 100vw, 843px\" \/><\/p>\n<h3>Step 4: Copy Binaries to a Directory in PATH environment Variable<\/h3>\n<p>Now copy the binaries located in the <strong>linux-x86_64-ubuntu2004-4.4.4\/bin <\/strong>to one of the directory in the <strong>$PATH <\/strong>like<strong> \/usr\/local\/bin. <\/strong><\/p>\n<pre>\r\n$ sudo cp -v mongodb-linux-x86_64-ubuntu2004-4.4.4\/bin \/usr\/local\/bin\/\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1143\" height=\"126\" class=\"wp-image-4987\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-102.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-102.png 1143w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-102-300x33.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-102-1024x113.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-102-768x85.png 768w\" sizes=\"auto, (max-width: 1143px) 100vw, 1143px\" \/><\/p>\n<h3>Step 5: Run MongoDB<\/h3>\n<p>To run MongoDB, follow the below steps:<\/p>\n<p>1. Create directories for storing data and logs. Issue the below commands to do so:<\/p>\n<pre>\r\n$ sudo mkdir -p \/var\/lib\/mongo\r\n\r\n$ sudo mkdir -p \/var\/log\/mongodb\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"55\" class=\"wp-image-4988\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-103.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-103.png 592w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-103-300x28.png 300w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<p>2. Use the below command to assign your user account read\/write permissions to the MongoDB data directory:<\/p>\n<pre>\r\n$ sudo chown `whoami` \/var\/lib\/mongo\r\n<\/pre>\n<p>Then assign permission to log directory:<\/p>\n<pre>\r\n$ sudo chown `whoami` \/var\/log\/mongodb\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"670\" height=\"54\" class=\"wp-image-4989\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-104.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-104.png 670w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-104-300x24.png 300w\" sizes=\"auto, (max-width: 670px) 100vw, 670px\" \/><\/p>\n<p>3. Run the mongod process (daemon for MongoDB) using the command below:<\/p>\n<pre>\r\n$ mongod --dbpath \/var\/lib\/mongo --logpath \/var\/log\/mongodb\/mongod.log --fork\r\n<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1118\" height=\"105\" class=\"wp-image-4990\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-105.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-105.png 1118w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-105-300x28.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-105-1024x96.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/03\/word-image-105-768x72.png 768w\" sizes=\"auto, (max-width: 1118px) 100vw, 1118px\" \/><\/p>\n<p>4. Now to start the mongo shell from the same system running the mongod process, the command is as follows:<\/p>\n<pre>\r\n$ mongo\r\n<\/pre>\n<h2>Uninstall MongoDB<\/h2>\n<p>If you no longer need MongoDB, you can completely uninstall it from your Ubuntu system. To do so, first stop the mongod process:<\/p>\n<pre>\r\n$ sudo service mongod stop\r\n<\/pre>\n<p>To remove MongoDB that you have installed using package manager, the command is as follows:<\/p>\n<pre>\r\n$ sudo apt purge mongodb-org\r\n<\/pre>\n<p>If you want to remove the logs and data directories too, use the commands below:<\/p>\n<pre>\r\n$ sudo rm -r \/var\/lib\/mongodb\r\n\r\n$ sudo rm -r \/var\/log\/mongodb\r\n<\/pre>\n<p>If you have installed MongoDB through the archive, you can uninstall it by removing the MongoDB binaries from <strong>\/usr\/local\/bin<\/strong>.<\/p>\n<p>This is how you can install MongoDB on Ubuntu 20.04 LTS using either the apt package manager or by downloading and installing through the archive. For more information, visit MongoDB&#8217;s official <a href=\"https:\/\/docs.mongodb.com\/manual\/installation\/\">documentation<\/a>.<\/p>","protected":false},"excerpt":{"rendered":"<p>MongoDB (known as Mongo) is a document-oriented database management system. It is categorized as a NoSQL database as it is a non-relational database and does not work with&hellip;<\/p>","protected":false},"author":1,"featured_media":5015,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[121],"class_list":["post-4977","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-mongodb-2"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4977","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=4977"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/4977\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/5015"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=4977"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=4977"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=4977"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}