How to install MongoDB on Ubuntu? [SOLVED]


Written By - Omer Cakmak
Advertisement

What is MongoDB?

MongoDB, it is a scalable, document-based, open source, NoSQL (Not Only SQL) database application developed with C++. It is free and open source under the GNU Affero General Public License. MongoDB has a distributed database structure, so it is highly available, scaling horizontally and vertically.

 

In this article, MongoDB installation will be explained in two different ways. The first way will be to install packages from the MongoDB repository, and the second way will be with tarball files.

 

Method-1: Installation from MongoDB Repository

MongoDB only supports 64-bit versions. The official package always includes the latest version of MongoDB and is available from its own repository. First install the repository key:

foc@ubuntu22:~$ sudo wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - 

Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
OK

Then define the repository addresses by creating a new file under the sources.list.d directory:

foc@ubuntu22:~$ sudo 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 
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse

Update the package list:

foc@ubuntu22:~$ sudo apt-get update -y

Ubuntu 22.04 uses the libssl package, version 3. It is necessary to install the libssl.1.1 package manually:

foc@ubuntu22:~$ sudo wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
--2022-12-10 14:33:25--  http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb
Resolving archive.ubuntu.com (archive.ubuntu.com)... 185.125.190.39, 185.125.190.36, 91.189.91.39, ...
Connecting to archive.ubuntu.com (archive.ubuntu.com)|185.125.190.39|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1318204 (1.3M) [application/x-debian-package]
Saving to: ‘libssl1.1_1.1.1f-1ubuntu2_amd64.deb’

libssl1.1_1.1.1f- 100%[=============>]   1.26M  2.60MB/s    in 0.5s    

2022-12-10 14:33:26 (2.60 MB/s) - ‘libssl1.1_1.1.1f-1ubuntu2_amd64.deb’ saved [1318204/1318204]

Install the downloaded file:

foc@ubuntu22:~$ sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
Selecting previously unselected package libssl1.1:amd64.
(Reading database ... 151123 files and directories currently installed.)
Preparing to unpack libssl1.1_1.1.1f-1ubuntu2_amd64.deb ...
Unpacking libssl1.1:amd64 (1.1.1f-1ubuntu2) ...
Setting up libssl1.1:amd64 (1.1.1f-1ubuntu2) ...
Processing triggers for libc-bin (2.35-0ubuntu3) ...

Install the mongodb-org package:

Advertisement
foc@ubuntu22:~$ sudo apt-get install -y mongodb-org

MongoDB version 6.0 installation completed:

foc@ubuntu22:~$ dpkg -l | grep mongodb
ii  mongodb-database-tools                100.6.1                                 amd64        mongodb-database-tools package provides tools for working with the MongoDB server: 
ii  mongodb-mongosh                       1.6.1                                   amd64        MongoDB Shell CLI REPL Package
ii  mongodb-org                           6.0.3                                   amd64        MongoDB open source document-oriented database system (metapackage)
ii  mongodb-org-database                  6.0.3                                   amd64        MongoDB open source document-oriented database system (metapackage)
ii  mongodb-org-database-tools-extra      6.0.3                                   amd64        Extra MongoDB database tools
ii  mongodb-org-mongos                    6.0.3                                   amd64        MongoDB sharded cluster query router
ii  mongodb-org-server                    6.0.3                                   amd64        MongoDB database server
ii  mongodb-org-shell                     6.0.3                                   amd64        MongoDB shell client
ii  mongodb-org-tools                     6.0.3                                   amd64        MongoDB tools

Status of mongod service:

foc@ubuntu22:~$ sudo systemctl status mongod.service 
○ mongod.service - MongoDB Database Server
     Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vend>
     Active: inactive (dead)
       Docs: https://docs.mongodb.org/manual

Enable mongodb service at startup:

foc@ubuntu22:~$ sudo systemctl enable mongod.service 
Created symlink /etc/systemd/system/multi-user.target.wants/mongod.service → /lib/systemd/system/mongod.service.

Connect to MongoDB:

foc@ubuntu22:~$ sudo mongosh
Current Mongosh Log ID:	639599a97ea7003185be5a75
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1
Using MongoDB:		6.0.3
Using Mongosh:		1.6.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

Installation completed successfully. You can start database operations.

 

Method-2: MongoDB Manual Installation (With Tarball)

You can manually install MongoDB 6.0 Community Edition by downloading a tarball file. First, install the following requirements:

foc@ubuntu22:~$ sudo apt-get install libcurl4 openssl liblzma5 -y

Download the compressed package from the official download address:

install MongoDB

Download the compressed package from the official download address or copy the link. Then download it with wget in terminal:

Advertisement
foc@ubuntu22:~$ sudo wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.3.tgz
--2022-12-10 14:51:41--  https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-ubuntu2004-6.0.3.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 52.84.106.103, 52.84.106.43, 52.84.106.124, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|52.84.106.103|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 71836258 (69M) [application/gzip]
Saving to: ‘mongodb-linux-x86_64-ubuntu2004-6.0.3.tgz’

mongodb-linux-x86 100%[=============>]  68.51M  1.83MB/s    in 46s     

2022-12-10 14:52:28 (1.49 MB/s) - ‘mongodb-linux-x86_64-ubuntu2004-6.0.3.tgz’ saved [71836258/71836258]

Unzip the compressed file:

foc@ubuntu22:~$ tar -zxvf mongodb-linux-*-6.0.3.tgz
mongodb-linux-x86_64-ubuntu2004-6.0.3/LICENSE-Community.txt
mongodb-linux-x86_64-ubuntu2004-6.0.3/MPL-2
mongodb-linux-x86_64-ubuntu2004-6.0.3/README
mongodb-linux-x86_64-ubuntu2004-6.0.3/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-ubuntu2004-6.0.3/bin/install_compass
mongodb-linux-x86_64-ubuntu2004-6.0.3/bin/mongod
mongodb-linux-x86_64-ubuntu2004-6.0.3/bin/mongos

Copy the files to the paths:

foc@ubuntu22:~$ sudo cp mongodb-linux-x86_64-ubuntu2004-6.0.3/bin/* /usr/local/bin/

Create library and log directories:

foc@ubuntu22:~$ sudo mkdir -p /var/lib/mongo
foc@ubuntu22:~$ sudo mkdir -p /var/log/mongodb

The user who started the MongoDB process must have read and write permissions to these directories. Run MongoDB:

foc@ubuntu22:~$ sudo mongod --dbpath /var/lib/mongo --logpath /var/log/mongodb/mongod.log --fork
about to fork child process, waiting until server is ready for connections.
forked process: 1000
child process started successfully, parent exiting

Then you can watch the logs:

foc@ubuntu22:~$ sudo tail -f /var/log/mongodb/mongod.log

Download and install the mongosh file:

Advertisement
foc@ubuntu22:~$ wget https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/6.0/multiverse/binary-amd64/mongodb-mongosh_1.6.1_amd64.deb
--2022-12-10 16:20:10--  https://repo.mongodb.org/apt/ubuntu/dists/jammy/mongodb-org/6.0/multiverse/binary-amd64/mongodb-mongosh_1.6.1_amd64.deb
Resolving repo.mongodb.org (repo.mongodb.org)... 13.32.110.107, 13.32.110.5, 13.32.110.35, ...
Connecting to repo.mongodb.org (repo.mongodb.org)|13.32.110.107|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 37705384 (36M)
Saving to: ‘mongodb-mongosh_1.6.1_amd64.deb’

mongodb-mongosh_1 100%[=============>]  35.96M  1.84MB/s    in 20s     

2022-12-10 16:20:30 (1.83 MB/s) - ‘mongodb-mongosh_1.6.1_amd64.deb’ saved [37705384/37705384]

Install the downloaded deb package:

foc@ubuntu22:~$ sudo dpkg -i mongodb-mongosh_1.6.1_amd64.deb 

Selecting previously unselected package mongodb-mongosh.
(Reading database ... 151036 files and directories currently installed.)
Preparing to unpack mongodb-mongosh_1.6.1_amd64.deb ...
Unpacking mongodb-mongosh (1.6.1) ...
Setting up mongodb-mongosh (1.6.1) ...
Processing triggers for man-db (2.10.2-1) ...

You can start database operations by logging in with Mongosh:

foc@ubuntu22:~$ mongosh
Current Mongosh Log ID:	6394b1fd3d93654c1493dbb3
Connecting to:		mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.1
Using MongoDB:		6.0.3
Using Mongosh:		1.6.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/


To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting
   2022-12-10T15:09:36.591+00:00: Using the XFS filesystem is strongly recommended with the WiredTiger storage engine. See http://dochub.mongodb.org/core/prodnotes-filesystem
   2022-12-10T15:09:36.930+00:00: Access control is not enabled for the database. Read and write access to data and configuration is unrestricted
   2022-12-10T15:09:36.930+00:00: You are running this process as the root user, which is not recommended
   2022-12-10T15:09:36.930+00:00: This server is bound to localhost. Remote systems will be unable to connect to this server. Start the server with --bind_ip <address> to specify which IP addresses it should serve responses from, or with --bind_ip_all to bind to all interfaces. If this behavior is desired, start the server with --bind_ip 127.0.0.1 to disable this warning
   2022-12-10T15:09:36.930+00:00: Soft rlimits for open file descriptors too low
------

------
   Enable MongoDB's free cloud-based monitoring service, which will then receive and display
   metrics about your deployment (disk utilization, CPU, operation statistics, etc).
   
   The monitoring data will be available on a MongoDB website with a unique URL accessible to you
   and anyone you share the URL with. MongoDB may use this information to make product
   improvements and to suggest MongoDB products and deployment options to you.
   
   To enable free monitoring, run the following command: db.enableFreeMonitoring()
   To permanently disable this reminder, run the following command: db.disableFreeMonitoring()
------

test>

Example:

test> show dbs;
admin   40.00 KiB
config  60.00 KiB
local   72.00 KiB
test> use foc
switched to db foc
foc> db.foc.insert({"role":"admin","rank":"pro"})
DeprecationWarning: Collection.insert() is deprecated. Use insertOne, insertMany, or bulkWrite.
{
  acknowledged: true,
  insertedIds: { '0': ObjectId("6394b308b1aceb4666233aa8") }
}
foc> show collections
foc
foc> db.foc.find()
[
  {
    _id: ObjectId("6394b308b1aceb4666233aa8"),
    role: 'admin',
    rank: 'pro'
  }
]
foc>

 

Summary

MongoDB supports the following Ubuntu versions:

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

We hope you will have a smooth installation by following the steps in the article.

 

References

www.mongodb.com - Install mongosh

 

Didn't find what you were looking for? Perform a quick search across GoLinuxCloud

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

1 thought on “How to install MongoDB on Ubuntu? [SOLVED]”

Leave a Comment