How to Install and Setup a MongoDB Server on MacOS

Have a Database Problem? Speak with an Expert for Free
Get Started >>


One of the benefits of having a MongoDB server on your macOS is that it resembles a Unix-like system. That being said, you can do a lot with a Linux distro and MongoDB’s latest version. The best news is that you can use a variety of methods to install MongoDB on the different distros of Linux. So let’s proceed with this tutorial on how to install and setup a MongoDB server on macOS.


  • Localhost web server – It must be running.


  • Linux server remote SSH access – You must have sudo privileges and a private key to gain access.

Debian-based distros: Install and Run MongoDB

  • Use the APT (Advanced Packaging Tool) repository manager to install and run the MongoDB server.

  • The sources.list.d file must have the MongoDB repository added to it.

echo ""deb [ arch=amd64 ] bionic/mongodb-org/4.0 multiverse"" | sudo tee /etc/apt/sources.list.d/mongodb-org-4.0.list
  • Access a terminal in Linux before you add MongoDB or anything else that is new. Prepare your OS by updating all repositories.
sudo apt-get update
  • Next, go ahead and install MongoDB. Include dependencies that go with it.
sudo apt-get install -y mongodb-org

or specify a particular version

sudo apt-get install -y mongodb-org=4.0.9 mongodb-org-server=4.0.9 mongodb-org-shell=4.0.9 mongodb-org-mongos=4.0.9 mongodb-org-tools=4.0.9
  • If you experience problems locating mongodb-org, try the sudo apt-get install method below:
sudo apt-get install php-mongodb

>NOTE: The repository file sources.list.d has to be complete and all packages have to be intact, nothing broken. If you come across an incomplete repository or some other error occurs, don’t stress. Just remove MongoDB entirely. Yes, it’s best to purge instead of trying to figure out the error because it could be anywhere and you don’t want to waste time. The sudo apt-get purge mongodb-org* command will get you to a place where you can start the installation process again.

Red Hat distros: Install and Run MongoDB

  • The YUM (Yellowdog Updater Modified) package manager works well with Red Hat Linux distros. Fedora and CentOS use YUM for installations. The good news is that within the directory of the YUM repository, a .repo file for MongoDB is created.

  • Use cd to create the MongoDB repository .repo file.

cd /etc/yum.repos.d/
  • The sudo command touch creates the .repo file. You’ll do this when you’re in the yum.repos.d folder. Check that you downloaded MongoDB’s current installation because you want to base the .repo file on that.

  • Next, the nano command line editor allows you to do some file editing.

sudo touch mongodb-org-4.0.repo

sudo nano mongodb-org-4.0.repo
  • Edit the file by pasting the repository installation’s configuration settings information.

name=MongoDB Repository




  • Now that the YUM directory contains the .repo file, you’re ready to install MongoDB.
sudo yum install mongodb-org
  • Update repositories with the YUM command This is the simplest way for those that use the RPM (Red Hat Package Manager):

  • CentOS distros

  • Fedora distros

  • Red Hat distros

sudo yum update

# use 'upgrade' to install the updated packages:

sudo yum upgrade

Use PHP and PEAR: Install and Run MongoDB

The PHP Extension and Application Repository is another way to install MongoDB. Before you begin though, use php --version to verify your current version of PHP on your OS.

php -v

php --version
  • Check that your PHP 7 version is complete. Verify that you have the necessary development libraries for it.
# replace `7.x` with your version of PHP 7

sudo apt-get install php7.x-dev
  • PEAR has a pecl library. Install MongoDB with it.
sudo pecl install mongodb

The version is identified on a macOS terminal by using the pear version command.

Screenshot of a macOS terminal checking the version of PHP PEAR installed

Be sure to verify your installation of PEAR .

Use a Tarball archive: Install and Run MongoDB

  • You can install and run MongoDB using a tarball archive. After you download, you can unarchive it.

Use cURL to download Tarball file

sudo curl -O

Use tar to unpack the Tarbarll file

sudo tar -zxvf mongodb-linux-x86_64-4.0.9.tgz
  • A new directory will contain the unpacked installation files for MongoDB.

  • Move it to the $PATH environment variable


  • Link it symbolically to point to the directory folder

Option 1: MongoDB Directory – Use MV for Path Exporting

  • The mv command will move the MongoDB archive (unzipped) to a new location where it can stay for good.

  • If you do that, the next thing you’ll want to do is use cd to gain access into the directory.

  • The pwd command will show you the directory’s path.

  • For the ability to gain access to the folder wherever, export the directory’s name and its path to $PATH in Linux.

export PATH=/some/path/mongodb-linux-x86_64-4.0.9/bin:$PATH
  • Another way to export the path is to copy and paste the following code below in the ~/.bashrc file:
export PATH=$PATH:/some/path/mongodb-linux-x86_64-4.0.9
  • To look at every environmental path, use the echo $PATH command.

  • To view every environmental variable, use the echo $VARIABLE command.

Option 2: MongoDB Directory – Move it to Linux

For this option, there are two ways to move the MongoDB directory:

  • Put it in a system directory of Linux


  • Put it in an established environment in Linux such as:

  • /usr/bin

  • /usr/local/bin
  • another similar path based on your OS and Linux system
sudo cp ....
  • View the environment path directory listing by inputting the terminal command: echo $PATH

Screenshot of terminal getting environmental paths

Optional Validation Task: Use a Public Key

  • If you want to validate the installer’s character, you can download a GPG public key and the TAR archive at the same time. Start with this script:
sudo curl -LO
  • To get the public signature file, use cURL.
sudo curl -LO
  • The last step in this process is to use cURL again to import the server.
curl -LO
gpg --import server-3.4.asc

Use Composer: Install and Run MongoDB

MongoDB: Setup and Configure the Server

  • If you don’t see the file mongodb.service in the cd /etc/systemd/system/ system directory, then in the terminal, paste the following code:
  • It’s likely that you won’t find MongoDB files in the system folder, but you can try ls -A to find out after you accessed the system folder.

  • Go to the directory /etc/systemd/system/ and use the command touch to create the mongodb.servicefile.

sudo touch mongodb.service
  • Edit the mongodb.service file you just created with the command line terminal editor nano or another one.
sudo nano mongodb.service
  • Export the file’s changes you just made by pressing Ctrl+O

  • Next, close the command line terminal editor nano or whichever one you used to edit by pressing Ctrl+X.

  • Press CTRL+O to output the changes to the file, and then press CTRL+X to close the nano editor running in terminal.

  • Try these sudo commands:

  • sudo systemctl start mongodb

  • sudo systemctl status mongodb

NOTE: To stop MongoDB service in Linux while in the terminal, press Ctrl+Z.

MongoDB: Test it. Start it up

mongod --port 42424

Time to Create a MongoDB Database Directory

  • You might already have a directory for MongoDB. Find out by going into a terminal and pasting the data directory path /data/db/. Press Return when finished.

  • If you receive a “error: dbpath (/data/db) does not exist” or similar error message, create the `/data/db/ by entering the following code:

sudo mkdir -p /data/db/
  • Start up the MongoDB service
sudo service mongod start

*Restart the MongoDB service

restart: sudo service mongod restart

*Stop MongoDB service

stop: sudo service mongod stop
  • Type Ctrl+Z to stop running the MongoDB service in the terminal.


This tutorial explained the various Linux distros and how you can setup MongoDB server on a macOS with them. Among the ways discussed, we talked about Debian-based, Red Hat, PHP and PEAR, Tarball archive, Composer, plus some more tips and tricks. With so many choices, you can select the method that suits you best.

Pilot the ObjectRocket Platform Free!

Try Fully-Managed CockroachDB, Elasticsearch, MongoDB, PostgreSQL (Beta) or Redis.

Get Started

Keep in the know!

Subscribe to our emails and we’ll let you know what’s going on at ObjectRocket. We hate spam and make it easy to unsubscribe.