Ubuntu's official package repositories include a stable version of MongoDB. However, as of this writing, the version of MongoDB available in the default Ubuntu repositories is 3.6, while the latest stable version is 4.4.
To get the latest version of this software, you must add MongoDB's dedicated package repository to your APT sources. You will then be able to install the mongodb-org meta-package, which always points to the latest version of MongoDB.
To begin, import the public GPG key for the latest stable version of MongoDB by running the following command. If you plan to use a version of MongoDB other than 4.4, make sure to change the URL portion of this command to align with the version you want to install:
curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -
cURL is a command-line tool, available on many operating systems, used for data transfer. It reads the data stored at the URL passed to it and prints the content to the system's output. In the example below, cURL prints the contents of the GPG key file and then pipes it to the sudo apt-key add - command that follows, so that the GPG key is added to your list of trusted keys.
Also note that this curl command uses the -fsSL options, which together essentially tell cURL to fail silently. This means that if cURL is unable to contact the GPG server for any reason, or if the GPG server is not working, it will not accidentally add the resulting error code to your list of trusted keys.
This command will return OK if the key was added successfully:
OutputOK
If you want to double-check that the key was added correctly, you can do so with the following command:
apt-key list
This will return the MongoDB key somewhere in the output:
Output/etc/apt/trusted.gpg
--------------------
pub rsa4096 2019-05-28 [SC] [expires: 2024-05-26]
2069 1EEC 3521 6C63 CAF6 6CE1 6564 08E3 90CF B1F5
uid [ unknown] MongoDB 4.4 Release Signing Key <packaging@mongodb.com>
. . .
At this point, your APT installation still does not know where to find the mongodb-org package you need to install the latest version of MongoDB.
There are two places on your server where APT looks for online package sources to download and install from: the sources.list file and the sources.list.d directory. sources.list is a file that lists the active sources of APT data, with one source per line and the most preferred sources listed first. The sources.list.d directory allows you to add such sources.list entries as separate files.
Run the following command, which creates a file named mongodb-org-4.4.list in the sources.list.d directory. The only content in this file is a single line reading deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/4.4 multiverse:
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
This single line tells APT everything it needs to know about what the source is and where to find it:
deb: This means the source entry refers to a regular Debian architecture. In other cases, this part of the line may readdeb-src, which means the source entry represents the source code of a Debian distribution.[ arch=amd64,arm64 ]: This specifies which architectures the APT data should be downloaded for. In this case, it specifies theamd64andarm64architectures.https://repo.mongodb.org/apt/ubuntu: This is a URI representing the location where the APT data can be found. In this case, the URI points to the HTTPS address of the official MongoDB repository.focal/mongodb-org/4.4: Ubuntu repositories can contain several different releases. This4.4specifies that you want only themongodb-orgpackage version to be available for thefocalrelease of Ubuntu ("Focal Fossa" is the codename for Ubuntu 20.04).multiverse: This section points APT to one of the four main Ubuntu repositories. In this case, it points to themultiverserepository .
After running this command, update your server's local package index so that APT knows where to find the mongodb-org package:
sudo apt update
After that, you can install MongoDB:
sudo apt install mongodb-org
When prompted, press Y and then ENTER to confirm that you want to install the package.
When the command finishes, MongoDB will be installed on your system. However, it is not ready to use yet. Next, you will start MongoDB and confirm that it works properly.
Step 2 — Starting the MongoDB Service and Testing the Database
The installation process described in the previous step automatically configures MongoDB to run as a daemon controlled by systemd, which means you can manage MongoDB using various systemctl commands. However, this installation procedure does not start the service automatically.
To start the MongoDB service, run the following systemctl command:
sudo systemctl start mongod.service
Then check the status of the service. Note that this command does not include .service in the service file definition. systemctl automatically appends this suffix to the argument you pass if it is not already present, so you do not need to include it:
sudo systemctl status mongod
This command returns output like the following, indicating that the service is up and running:
Output● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Tue 2020-06-09 12:57:06 UTC; 2s ago
Docs: https://docs.mongodb.org/manual
Main PID: 37128 (mongod)
Memory: 64.8M
CGroup: /system.slice/mongod.service
└─37128 /usr/bin/mongod --config /etc/mongod.conf
After confirming that the service is working as expected, enable the MongoDB service to start at boot:
sudo systemctl enable mongod
You can also verify that the database is up and running by connecting to the database server and running a diagnostic command. The following command will connect to the database and display its current version, the server address and the port. It also returns the result of MongoDB's internal connectionStatus command:
mongo --eval 'db.runCommand({ connectionStatus: 1 })'
connectionStatus will check and return the status of the database connection. A value of 1 for the ok field in the response indicates that the server is working as expected:
OutputMongoDB shell version v4.4.0
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("1dc7d67a-0af5-4394-b9c4-8a6db3ff7e64") }
MongoDB server version: 4.4.0
{
"authInfo" : {
"authenticatedUsers" : [ ],
"authenticatedUserRoles" : [ ]
},
"ok" : 1
}
Also note that the database is running on port 27017 of localhost, the local loopback address 127.0.0.1. This is MongoDB's default port number.
Next, you will confirm that the MongoDB server instance is managed by systemd.
Step 3 — Managing the MongoDB Service
As mentioned earlier, the installation process described in Step 1 configures MongoDB to run as a systemd service. This means you can manage it using standard systemctl commands, just like other Ubuntu system services.
As mentioned earlier, the systemctl status command checks the status of the MongoDB service:
sudo systemctl status mongod
You can stop the service at any time by typing:
sudo systemctl stop mongod
To start the service when it is stopped, run:
sudo systemctl start mongod
You can also restart the server while it is already running:
sudo systemctl restart mongod
In Step 2, you enabled MongoDB to start automatically with the server. If you want to disable this automatic start, type:
sudo systemctl disable mongod
Then, to re-enable starting at boot, run the enable command again:
sudo systemctl enable mongod