Published on

How to Install MongoDB on macOS and Ubuntu

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

How to Install MongoDB on macOS and Linux

MongoDB is a popular NoSQL database, and setting it up correctly is the first step to leveraging its capabilities. In this guide, we'll cover how to install MongoDB on macOS and Linux, ensuring a smooth setup process.

Installing MongoDB on macOS

Step 1: Install Homebrew

If you don’t already have Homebrew installed, you can install it using the following command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Step 2: Install MongoDB via Homebrew

Run the following commands to install MongoDB:

brew tap mongodb/brew
brew install mongodb-community

Step 3: Start the MongoDB Service

Start MongoDB with the command:

brew services start mongodb/brew/mongodb-community

Step 4: Verify Installation

Check if MongoDB is running:

mongosh --version

If the version is displayed, MongoDB is successfully installed.


Installing MongoDB on Ubuntu

Step 1: Import the Public Key

Import the MongoDB public GPG key to verify packages:

curl -fsSL https://www.mongodb.org/static/pgp/server-4.4.asc | sudo apt-key add -

Step 2: Add MongoDB Repository

Create a file for the MongoDB repository:

sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list <<EOF
deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse
EOF

Replace focal with your distribution codename if different.

Step 3: Update Package List

Update your system’s package list:

sudo apt update

Step 4: Install MongoDB

Install MongoDB using:

sudo apt install mongodb-org

Step 5: Start the MongoDB Service

Start MongoDB with:

sudo systemctl start mongod

Enable MongoDB to start on boot:

sudo systemctl enable mongod

Step 6: Verify Installation

Check MongoDB version:

mongo --version

If the version is displayed, MongoDB is successfully installed.


Conclusion

Installing MongoDB on macOS and Ubuntu is straightforward with the right steps. Whether you’re on macOS using Homebrew or on Ubuntu using package managers, MongoDB can be up and running in no time. Next, we’ll explore basic CRUD operations in MongoDB to help you get started with development.

Happy coding!