MongoDB is included in Ubuntu package repositories, but official MongoDB repository provides most upto date version.
We use official version of MongoDB
$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
The output should look like this
ubuntu@ip-192-41-15-154:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
Executing: /tmp/tmp.u96cXK2VEb/gpg.1.sh --keyserver
hkp://keyserver.ubuntu.com:80
--recv
EA312927
gpg: requesting key EA312927 from hkp server keyserver.ubuntu.com
gpg: key EA312927: public key "MongoDB 3.2 Release Signing Key <packaging@mongodb.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
We add MongoDB repository to Apt, so that apt know where to download the repository
echo "deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.4.list
Output:
deb [ arch=amd64,arm64 ] http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 multiverse
Update the Apt
$ sudo apt-get update
Output:
Hit:1 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial InRelease
Get:2 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]
Ign:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 InRelease
Get:4 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-backports InRelease [102 kB]
Get:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release [3,457 B]
Get:6 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release.gpg [801 B]
Get:7 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main Sources [258 kB]
Get:8 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages [573 kB]
Get:9 http://ap-southeast-1.ec2.archive.ubuntu.com/ubuntu xenial-updates/universe amd64 Packages [504 kB]
Ign:6 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release.gpg
Hit:10 http://ppa.launchpad.net/webupd8team/atom/ubuntu xenial InRelease
Get:11 http://security.ubuntu.com/ubuntu xenial-security InRelease [102 kB]
Get:12 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 Packages [5,870 B]
Get:13 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse arm64 Packages [5,872 B]
Fetched 1,658 kB in 1s (938 kB/s)
Reading package lists... Done
W: GPG error: http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY AC711F9BA35703V6
W: The repository 'http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4 Release' is not signed.
N: Data from such a repository can't be authenticated and is therefore potentially dangerous to use.
N: See apt-secure(8) manpage for repository creation and user configuration details.
Install MongoDB
$ sudo apt-get install mongodb-org
Output:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
The following NEW packages will be installed:
mongodb-org mongodb-org-mongos mongodb-org-server mongodb-org-shell mongodb-org-tools
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 66.8 MB of archives.
After this operation, 270 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
WARNING: The following packages cannot be authenticated!
mongodb-org-shell mongodb-org-server mongodb-org-mongos mongodb-org-tools mongodb-org
Install these packages without verification? [y/N] Y
Get:1 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-shell amd64 3.4.6 [7,981 kB]
Get:2 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-server amd64 3.4.6 [14.2 MB]
Get:3 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-mongos amd64 3.4.6 [8,110 kB]
Get:4 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org-tools amd64 3.4.6 [36.5 MB]
Get:5 http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.4/multiverse amd64 mongodb-org amd64 3.4.6 [3,556 B]
Fetched 66.8 MB in 3s (20.3 MB/s)
Selecting previously unselected package mongodb-org-shell.
(Reading database ... 108006 files and directories currently installed.)
Preparing to unpack .../mongodb-org-shell_3.4.6_amd64.deb ...
Unpacking mongodb-org-shell (3.4.6) ...
Selecting previously unselected package mongodb-org-server.
Preparing to unpack .../mongodb-org-server_3.4.6_amd64.deb ...
Unpacking mongodb-org-server (3.4.6) ...
Selecting previously unselected package mongodb-org-mongos.
Preparing to unpack .../mongodb-org-mongos_3.4.6_amd64.deb ...
Unpacking mongodb-org-mongos (3.4.6) ...
Selecting previously unselected package mongodb-org-tools.
Preparing to unpack .../mongodb-org-tools_3.4.6_amd64.deb ...
Unpacking mongodb-org-tools (3.4.6) ...
Selecting previously unselected package mongodb-org.
Preparing to unpack .../mongodb-org_3.4.6_amd64.deb ...
Unpacking mongodb-org (3.4.6) ...
Processing triggers for man-db (2.7.5-1) ...
Setting up mongodb-org-shell (3.4.6) ...
Setting up mongodb-org-server (3.4.6) ...
Adding system user `mongodb' (UID 113) ...
Adding new user `mongodb' (UID 113) with group `nogroup' ...
Not creating home directory `/home/mongodb'.
Adding group `mongodb' (GID 118) ...
Done.
Adding user `mongodb' to group `mongodb' ...
Adding user mongodb to group mongodb
Done.
Setting up mongodb-org-mongos (3.4.6) ...
Setting up mongodb-org-tools (3.4.6) ...
Setting up mongodb-org (3.4.6) ...
Create mongodb.service configuration file
$ sudo nano /etc/systemd/system/mongodb.service
Start MongoDB as Daemon
$ sudo systemctl start mongod
Check status of MongoDB
$ sudo systemctl status mongod
Output:
mongodb.service - High-performance, schema-free document-oriented database
Loaded: loaded (/etc/systemd/system/mongodb.service; disabled; vendor preset: enabled)
Active: active (running) since Thu 2019-07-16 03:25:42 UTC; 1min 43s ago
Main PID: 13102 (mongod)
Tasks: 20
Memory: 34.8M
CPU: 408ms
CGroup: /system.slice/mongodb.service
└─13102 /usr/bin/mongod --quiet --config /etc/mongod.conf
Jul 13 03:25:42 ip-192-31-13-143 systemd[1]: Started High-performance, schema-free document-oriented database.
Enable process start for MongoDB when system reboot
$ sudo systemctl enable mongodb
Output:
Created symlink from /etc/systemd/system/multi-user.target.wants/mongodb.service to /etc/systemd/system/mongodb.service.