How to Install the MEAN Stack on Ubuntu Linux
The MEAN stack on Ubuntu Linux is a web development bundle made of MongoDB, Express.js, Angular, and Node.js. It lets you build dynamic websites using only JavaScript for both the front end and the back end.
Setting up this environment on Ubuntu 22.04 LTS takes about 20 minutes when you follow the right steps. Each tool has a clear job: MongoDB saves your data, Express handles web traffic, Angular builds what people see on screen, and Node runs your code on the server.
Before you start, make sure you are signed in to Ubuntu as a user with sudo rights and that the machine has internet access. Every command in this guide runs in a terminal window.
Install the MEAN stack on Ubuntu 22.04 by first adding MongoDB’s official repository, then installing MongoDB, Node.js, and the project files in that order. Run sudo apt update, then sudo apt install mongodb-org in your terminal. The whole setup takes about 20 minutes and needs a user with sudo rights and internet access.
Install and use the MEAN stack on Ubuntu Linux
As described above, the MEAN stack is a JavaScript-based framework for developing dynamic websites and applications. The stack consists of MongoDB, Express, Angular, and Node.
Only one language is involved. The same JavaScript skills cover the database queries, the server code and the browser interface, which is the main reason people pick this stack.
Below is how to install it on Ubuntu Linux. The order matters: MongoDB first, then Node.js, then the project files, and finally the optional reverse proxy.
Install MongoDB
Install MongoDB on Ubuntu by adding the official software repository to your system package lists. You need to update your system packages, install the required security dependencies, and run the standard package manager commands to download and set up the database server before building your application.
The repository is needed because the MongoDB packages in Ubuntu's own sources are not the current official builds. The next few steps prepare the system, add the repository key, add the repository itself, and then install the database.
First, add the packages that let APT work with third-party repositories over HTTPS. Run the commands below in your terminal.
If the terminal asks for your password, type it and press Enter. Nothing shows on screen while you type, and that is normal.
sudo apt update sudo apt install dirmngr gnupg apt-transport-https ca-certificates software-properties-common
To install MongoDB, add its repository's GPG key to the system. APT uses this key to check that the packages really come from MongoDB and were not changed on the way.
Run the commands below to do that. When they finish without errors, the key is stored and APT will trust packages signed with it.
wget -qO - https://www.mongodb.org/static/pgp/server-4.4.asc | sudo gpg --dearmor -o /usr/share/keyrings/mongodb-server-4.4.gpg
Next, run the commands below to create a repository file for MongoDB on Ubuntu Linux. This file tells APT where to download MongoDB packages from.
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
For example, the Ubuntu 20.04 code name is focal. On Ubuntu 22.04 LTS, which this guide targets, the code name is jammy.
A wrong code name is the most common reason for "repository does not have a Release file" errors in the next step. Also confirm that the MongoDB version in the repository line supports your Ubuntu release.
After adding the repository GPG key and file, run the commands below to update the Ubuntu package index and install MongoDB.
The update step should list the MongoDB repository among the sources it checks. If it reports a key or "Release" error instead, go back and recheck the key and the repository file before you continue.
sudo apt update sudo apt install mongodb-org
If you encounter problems installing MongoDB, run the commands below to install the missing dependencies. Then repeat the MongoDB installation.
wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.1f-1ubuntu2_amd64.deb sudo dpkg -i libssl1.1_1.1.1f-1ubuntu2_amd64.deb
Additional resources on installing and managing the MongoDB database can be found at the link below. It covers starting the database service and checking that it runs, which you should do before you move on to the application.
Install Node.js
Install Node.js on Ubuntu to provide the JavaScript runtime environment required to run your web server and application code. You can download the setup script from the official Node distribution sources and run terminal commands to put the correct version onto your system.
Angular is a set of tools and libraries for building the front end of web apps in JavaScript (or TypeScript). Its tooling depends on Node.js, so Node has to be in place first.
Node.js is the JavaScript runtime that runs your server code. Express runs on top of it.
Let's first install Node.js and then get the other packages installed after.
Node.js packages are included in Ubuntu's default repositories. However, the version included in Ubuntu might not necessarily be the latest.
To get a newer version, you have to add the Node.js repository first.
Run the commands below to download the Node.js version 18 repository script and run it. The script adds the repository to APT, so the next command can find the newer packages.
This guide uses version 18. That release is no longer actively supported, so for a new project, check the Node.js site for the current LTS release and use the setup script for that version instead.
sudo apt install curl curl -sL https://deb.nodesource.com/setup_18.x | sudo bash -
Once the script has downloaded and run, run the commands below to install Node.js version 18. The installer also brings in npm, the package manager you use for the rest of the setup.
sudo apt install nodejs
Additional packages recommended with the MEAN stack can be installed using the commands below. Some of them may need build tools, so if an install fails with a compiler error, install the build tools and run it again.
sudo npm install -g yarn sudo npm install -g gulp sudo npm install pm2 -g
Additional resources on installing and using Node.js on Ubuntu Linux can be found at the link below.
Install MEAN stack
Install MEAN stack components on Ubuntu by downloading the source code repository from GitHub using Git. You pull the project files to your local machine and use package managers to bring together MongoDB, Express, AngularJS, and Node.js for your development workspace.
Git must be installed for this step. If the terminal says the command is not found, install Git with APT first.
Run the commands below to clone the package from GitHub. The clone creates a new folder with the project files in your current directory.
sudo apt install git git clone https://github.com/meanjs/mean
Once downloaded, move into the project folder and run the command below to install all dependencies. This can take a minute or two, because npm downloads every package the project needs. When it ends, a node_modules folder appears in the project folder.
cd mean yarn install
Create a Node.js web app
Create a Node.js web app by editing the main server file named server.js inside your project folder. You open this configuration file in a terminal text editor, paste the required connection code for your database, and save the changes to launch your server.
Otherwise the editor creates a new, empty file in the wrong place.
nano ~/mean/server.js
- Wait for the editor to open the file.
- Copy the lines below and paste them into the file.
- Save the file and close the editor.
In a terminal editor such as nano, you save with Ctrl+O and then Enter, and exit with Ctrl+X.
const express = require('express');
const MongoClient = require('mongodb').MongoClient;
const app = express();
app.use('/', (req, res) => {
MongoClient.connect("mongodb://localhost:27017/test", function(err, db){
db.collection('Example', function(err, collection){
collection.insert({ pageHits: 'pageHits' });
db.collection('Example').count(function(err, count){
if(err) throw err;
res.status(200).send('Page Hits: ' + Math.floor(count/2));
});
});
});
});
app.listen(3000);
console.log('Server running at http://localhost:3000/');
module.exports = app;After saving the file, run the commands below to start the server. The terminal stays busy while the server runs, so don't close it yet.
pm2 start ~/mean/server.js
When the server starts, it should output something similar to the lines below. If you see a connection error instead, check that the MongoDB service is running.
-------------
__/\\\\\\____/\\____________/\\____/\\\\_____
_/\/////////\_/\\\________/\\\__/\///////\___
_/\_______/\_/\//\____/\//\_///______//\__
_/\\\\\\/__/\\///\/\/_/\___________/\/___
_/\/////////____/\__///\/___/\________/\//_____
_/\_____________/\____///_____/\_____/\//________
_/\_____________/\_____________/\___/\/___________
_/\_____________/\_____________/\__/\\\\\\\_
_///______________///______________///__///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
[PM2] Spawning PM2 daemon with pm2_home=/home/richard/.pm2
[PM2] PM2 Successfully daemonized
[PM2] Starting /home/richard/mean/server.js in fork_mode (1 instance)
[PM2] Done.
┌─────┬───────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├─────┼───────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ server │ default │ 0.6.0 │ fork │ 7377 │ 0s │ 0 │ online │ 0% │ 38.8mb │ richard │ disabled │
└─────┴───────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
Run the commands below to enable the web app to start automatically when the system boots. Without this, the app stops when you close the terminal or restart the machine.
pm2 startup ~/mean/server.js
The command above will output lines similar to the ones below.
[PM2] Init System found: systemd ----------------------------------------------------------- PM2 detected systemd but you precised /home/richard/mean/server.js Please verify that your choice is indeed your init system If you arent sure, just run : pm2 startup [PM2] To setup the Startup Script, copy/paste the following command: sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup /home/richard/mean/server.js -u richard --hp /home/richard
You will be prompted to run the highlighted commands above to enable the app to start up automatically every time you start the system. Copy the highlighted command exactly as it appears in your own output, paste it into the terminal and press Enter. The command is generated for your user account, so a copy from another machine will not work.
The application has started using port 3000.
- Open your web browser.
- Type the server's hostname or IP address in the address bar, followed by a colon and port number 3000. On the same machine, use localhost:3000.
- Press Enter. You should see the web page served by your app.
If the page does not load from another computer, a firewall is the usual cause. If UFW is enabled on the server, port 3000 must be allowed through it.
http://localhost:3000
Run MEAN app with reverse proxy
Run a MEAN app with a reverse proxy like Nginx or Apache to handle incoming web traffic and improve overall site performance. You configure the web server to forward public requests to your local Node.js port so visitors can reach your application securely.
With a proxy in place, visitors use a normal address on port 80 or 443 and never type :3000. It also lets you add HTTPS later. This step is optional on a test machine, but you should do it on a public server. The guides below cover both web servers.
That should do it!
Conclusion:
- The MEAN stack is a powerful JavaScript-based framework for developing dynamic web applications.
- By installing MongoDB, Express, Angular, and Node.js on Ubuntu Linux, developers can standardize on proven technologies and spend more time building applications.
- Adding a reverse proxy with Nginx or Apache HTTP server can efficiently run the MEAN stack app.
- Building and deploying web applications using the MEAN stack on Ubuntu Linux is now within reach and can lead to fast, dynamic, and robust results.
Have a question about this guide?
Answered by AI from this guide. Good questions may be added to this page — please don't include personal details.
Did this guide fix your problem?
About the Author
Richard
Tech Writer, IT Professional
Richard is a writer at Geek Rewind who turns complex IT tasks into clear, step-by-step guides. He draws on years of hands-on experience in system administration and enterprise IT operations, focusing on Windows, Linux and WordPress: the problems people actually run into, and the fixes that work. His server and WordPress guides come from systems he runs himself. Richard builds and maintains the platform behind Geek Rewind, from its Ubuntu servers and Nginx configuration to its custom WordPress plugins. Many new tutorials start with readers' questions, and he's always glad to answer them in the comments.
No comments yet — be the first to share your thoughts!