sodcalculator

10 Tech How-To

Install MEAN Stack on Ubuntu: Step-by-Step

Guide 10-52682 · Updated · Step by step

To install the MEAN stack (MongoDB, Express.js, Angular, Node.js) on Ubuntu, you install Node.js, MongoDB, and the Angular CLI, then create a project that uses Express and Angular together. This guide walks through each component on a current Ubuntu LTS release, using official repositories and npm packages.

What Is the MEAN Stack?

MEAN is an acronym for four open-source technologies that together form a full-stack JavaScript environment:

  • MongoDB – a NoSQL document database that stores data in JSON-like documents.
  • Express.js – a minimal web framework for Node.js that handles routing and middleware.
  • Angular – a front-end framework (TypeScript-based) for building single-page applications.
  • Node.js – the JavaScript runtime that runs the server-side code.

Because all layers use JavaScript (or TypeScript for Angular), developers can work in one language across the entire stack. The alternative MERN stack replaces Angular with React, and MEVN uses Vue.js.

Prerequisites

Before starting, ensure your Ubuntu system is updated and you have sudo access. You will also need a terminal and an internet connection. The steps below assume Ubuntu 22.04 or 24.04 LTS, but they generally work on other recent versions as well.

  1. Update your package list: sudo apt update
  2. Upgrade existing packages: sudo apt upgrade -y
  3. Install curl and build tools (needed for some npm packages): sudo apt install -y curl build-essential

Step 1: Install Node.js and npm

Ubuntu’s default repositories contain an older version of Node.js. For the MEAN stack, you want a current LTS release. The recommended way is to use NodeSource, a third-party repository that provides up-to-date binaries.

  1. Add the NodeSource repository (replace 22.x with the current LTS major version if needed):
    curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
  2. Install Node.js and npm: sudo apt install -y nodejs
  3. Verify the installation: node -v and npm -v

Node.js includes npm (Node Package Manager) by default. The nodejs package from NodeSource also installs the node binary, which is what you run.

Step 2: Install MongoDB

MongoDB is not included in Ubuntu’s default repositories because of licensing. You must add MongoDB’s official repository manually.

  1. Import the MongoDB public GPG key:
    curl -fsSL https://www.mongodb.org/static/pgp/server-7.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg --dearmor
  2. Create the source list file:
    echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-7.0.gpg ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/7.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-7.0.list
    Note: For Ubuntu 24.04, replace jammy with noble if the repository supports it; otherwise use the LTS version that matches your Ubuntu.
  3. Update packages and install MongoDB: sudo apt update then sudo apt install -y mongodb-org
  4. Start MongoDB and enable it on boot:
    sudo systemctl start mongod
    sudo systemctl enable mongod
  5. Check the service status: sudo systemctl status mongod

MongoDB runs as a background service on port 27017. The default configuration binds to localhost, which is fine for development.

Step 3: Install Angular CLI

Angular CLI is a command-line tool that scaffolds and builds Angular applications. It is installed globally via npm.

  1. Install the CLI: sudo npm install -g @angular/cli
  2. Verify: ng version

The ng command lets you create new projects, generate components, and serve the app locally. The CLI installs the latest stable Angular version by default.

Step 4: Create the Project Structure

A typical MEAN project has two parts: a backend (Node/Express) and a frontend (Angular). You can keep them in separate folders or combine them. A common layout:

my-mean-app/
  backend/
  frontend/
  1. Create the backend folder: mkdir backend && cd backend
  2. Initialize a Node project: npm init -y
  3. Install Express and Mongoose (Mongoose is an ODM for MongoDB): npm install express mongoose cors
  4. Create a simple server.js file with Express and connect to MongoDB.
  5. Create the frontend: ng new frontend (choose routing and stylesheet as desired).

You now have a backend that can serve APIs and a frontend that can consume them. To run both in development, you can use two terminals: node server.js in the backend folder and ng serve in the frontend folder.

Common Problems and Fixes

Here are issues people often hit during installation:

  • MongoDB fails to start after install – Check the log file /var/log/mongodb/mongod.log. Often the data directory /var/lib/mongodb has wrong permissions. Fix with sudo chown -R mongodb:mongodb /var/lib/mongodb.
  • Node.js version too old – If you installed from Ubuntu’s default repo, you may get an old version. Remove it (sudo apt remove nodejs) and use the NodeSource method above.
  • npm permission errors – When installing global packages without sudo, you may get EACCES errors. Either use sudo or configure npm’s global directory to a user-writable location.
  • Angular CLI not found after install – Ensure the npm global bin directory is in your PATH. Usually it is /usr/local/bin or /usr/bin. Run npm prefix -g to check.

Frequently Asked Questions

Do I need to install MongoDB separately?

Yes, MongoDB is a separate database server. It does not come bundled with Node.js or Angular. You install it via its official repository, as shown above.

Can I use a different version of Angular?

Yes, the Angular CLI installs the latest stable version by default. You can specify a version with npx @angular/cli@17 new my-app for example. The MEAN stack works with any modern Angular version.

Is the MEAN stack suitable for production?

Yes, all four technologies are widely used in production. However, you should configure MongoDB with authentication, use environment variables for secrets, and set up a process manager like PM2 for Node.js. Also, build the Angular app with ng build --prod and serve the static files from Express.

What is the difference between MEAN and MERN?

The only difference is the front-end framework: Angular vs React. MEAN uses Angular (a full framework with TypeScript), while MERN uses React (a library with more flexibility). The backend and database are the same.

How do I uninstall the MEAN stack?

To remove components: sudo apt remove --purge mongodb-org, sudo apt remove nodejs, and sudo npm uninstall -g @angular/cli. Also remove the repository files if you added them. Be careful, as this will delete your projects if they are in the same directories.

Alternatives and Final Thoughts

If you prefer a different database, you can replace MongoDB with PostgreSQL or MySQL (then it becomes a PENN stack). Similarly, you can swap Angular for React or Vue. The installation steps for Node.js and Express remain the same. The MEAN stack is a solid choice for developers who want a unified JavaScript environment, but it requires understanding of both front-end and back-end concepts. Follow the steps carefully, and you will have a working development environment in under an hour.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →