How to Install & Set up the MEAN Stack on Ubuntu (MongoDB, Express.js, Angular, Node.js)

Introduction

The MEAN stack is an open-source JavaScript (JS) stack for developing web applications. It includes several software tools for building dynamic websites and web applications.

As it uses JS in each layer of the stack, it significantly simplifies web development.

This article provides step-by-step instructions for installing the MEAN stack on Ubuntu 20.04 and 22.04.

How to Install & Set up MEAN Stack on Ubuntu

Prerequisites

How to Install the MEAN Stack on Ubuntu

MEAN is an acronym, and it consists of the following programs:

MEAN is an alternative to the widely known LAMP stack, which uses JS or a JS framework on the client side and PHP on the server side.

Note: See our in-depth comparison between MEAN vs. LAMP.

Installing the MEAN stack requires installing each component separately. To do so, follow the steps in the sections below.

Step 1: Update Package Repository Cache

Before installing the MEAN stack, update Ubuntu's package repository:

sudo apt update

If prompted, press y to accept installing suggested updates. Wait for the updates to be completed before proceeding to the next step.

Step 2: Install MongoDB

MongoDB does not maintain the MongoDB package in the APT package manager. The official community edition requires manual installation and supports 64-bit Ubuntu LTS releases.

To install the latest stable version of MongoDB, do the following:

1. Install cURL and GnuPG with the apt-get command:

sudo apt-get install gnupg curl
sudo apt-get install gnupg curl terminal output

The two tools help import the MongoDB public GPG key. Press y to confirm the package installation.

2. Import the GPG key with the following command:

curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor

The command does not produce an output.

2. Next, create a list file for MongoDB so the command knows where to look for the packages:

echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -cs)/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list

The command determines the operating system codename. It then creates the MongoDB repo list file in the APT sources list using the appropriate name and the imported key from the previous step.

3. Update the local package database:

sudo apt-get update

4. Install the latest MongoDB version with:

sudo apt-get install mongodb-org
sudo apt-get install mongodb-org terminal output

Wait for the installation to complete.

5. To start MongoDB, run the following systemctl command to start the service:

sudo systemctl start mongod

This command starts the database service.

6. Check the mongod service status with:

sudo systemctl status mongod
sudo systemctl status mongod active running terminal output

The output shows the service as active (running), which indicates the installation was successful.

6. The final step is to enable MongoDB:

sudo systemctl enable mongod
sudo systemctl enable mongod terminal output

The command makes MongoDB start automatically when the system launches.

Step 3: Install Node.js and npm

Node.js is the runtime environment in the MEAN stack, enabling server-side JS execution. npm (the Node Package Manager) handles Node.js packages and dependencies and is used to install Express and Angular. Although Node.js is available in the APT repositories, it's not the latest stable version.

Follow the steps below to install Node.js and npm:

1. Add the NodeSource PPA (Personal Package Archive) and configure the repository with:

curl -sL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
curl nodejs setup ppa script terminal output

We recommend using the LTS version. Replace lts.x with the preferred version number for a specific version.

2. Install Node.js and npm by typing the command:

sudo apt install nodejs
sudo apt-get install nodejs terminal output

3. Verify the installation with the following two commands:

nodejs --version
npm --version
nodejs and npm version terminal output

The first command outputs the Node.js version, while the second shows the npm version.

Step 4: Install Angular CLI

Angular CLI is a command-line tool for making and managing Angular applications. Install it with the following command:

sudo npm install -g @angular/cli
sudo npm install angular cli terminal output

The command installs Angular CLI globally and enables installing Angular for local projects. When creating a new project, the following Angular CLI command installs Angular locally:

ng new [application_name]
ng new frontend terminal output mean frontend

The installation asks several questions, and answers depend on the project's requirements.

Step 5: Install Express.js

Express.js is a backend framework for Node.js. It facilitates the backend part of the MEAN stack. When creating the project's backend, first initialize the local project directory:

npm init -y
npm init -y terminal output MEAN backend

Then, install Express.js locally in a project folder with the following command:

npm install express
npm install express terminal output

The npm command automatically adds it as a project dependency. A global installation is not recommended due to dependency issues. 

Conclusion

After reading this article, you know how to install all the necessary dependencies and software packages for the MEAN stack on Ubuntu. The stack enables the development of fast and robust web applications.

Next, read more about software dependencies.

Was this article helpful?
YesNo
Milica Dancuk
Milica Dancuk is a technical writer at phoenixNAP with a passion for programming. With a background in Electrical Engineering and Computing, coupled with her teaching experience, she excels at simplifying complex technical concepts in her writing.
Next you should read
How to Install Ubuntu 18.04 Bionic Beaver
July 5, 2019

Ubuntu 18.04 (codenamed Bionic Beaver) is a free, open-source distribution of Linux, based on the Debian...
Read more
How to Install XAMPP on Ubuntu 18.04
February 22, 2024

The XAMPP stack is an open-source Apache distribution of a PHP development environment consisting of...
Read more
How To Install Node.js & NPM on Ubuntu 18.04
March 18, 2019

Node.js is an open-source cross-platform JavaScript (JS) runtime environment. It is used for building fast...
Read more
How to Install Pip on Ubuntu 18.04
August 20, 2024

Pip is a software utility that downloads packages from PyPI - the Python Package Index. Using PIP is like...
Read more