The MEAN stack, a powerful combination of MongoDB, Express.js, Angular, and Node.js, is a popular choice for developing modern web applications. This technology stack allows developers to build full-stack JavaScript applications, enabling seamless data flow between the front end and back end. By effectively leveraging the strengths of each component, MEAN consequently provides a robust and scalable foundation for dynamic web apps.
In this guide, we’ll walk you through the step-by-step process of installing and setting up the MEAN stack on an Ubuntu server. Whether you’re an experienced developer looking to streamline your workflow or a newcomer eager to explore full-stack development, this tutorial will equip you with the knowledge to get your MEAN application up and running.
Prerequisites
Before you begin, ensure you have the following:
- A server running Ubuntu 20.04 or later.
- A non-root user with sudo privileges.
- Basic knowledge of the command line.
How to Install the MEAN Stack on Ubuntu
-> MongoDB: MongoDB is a NoSQL document-oriented database that stores data in JSON-like format. It’s designed for high availability and scalability, making it a perfect fit for modern applications.
-> Express.js: Express.js is a flexible Node.js web application framework that provides a robust set of features for building single and multi-page web applications. It’s designed to make the development process simpler and more intuitive.
-> Angular: Angular is a front-end framework, expertly developed by Google, which allows developers to efficiently build dynamic, single-page applications with a clean and maintainable code structure.
-> Node.js:Node.js is a powerful JavaScript runtime environment that effectively enables the execution of JavaScript code server-side. It’s specifically built on Chrome’s V8 engine, thereby making it both fast and efficient.
Update Your System
Start by updating your package list and upgrading existing packages:
apt update
apt upgrade -y
Install Node.js and npm
Node.js is a runtime environment for executing JavaScript code server-side, and npm is its package manager. We have already discussed details about Node.js in our previous article.
- Add the NodeSource repository for the latest version of Node.js.
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
2. Install Node.js and npm.
apt install -y nodejs
3. Verify the installation.
node -v
npm -v
Install MongoDB
MongoDB is a NoSQL database that stores data in flexible, JSON-like documents.
- Import the MongoDB GPG key:
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
2. Add the MongoDB repository to your sources list.
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu focal/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
3. Install MongoDB:
apt update
apt install -y mongodb-org
4. Start and enable the MongoDB service:
systemctl start mongod
systemctl enable mongod
5. Verify MongoDB is running
systemctl status mongod
Install Express.js
Express.js is a web application framework for Node.js, simplifying the process of building web apps.
- Create a directory for your project
mkdir my-mean-app
cd my-mean-app
2. Initialize a new Node.js project
npm init -y
3. Install Express.js
npm install express --save
4. Create a simple Express server
touch app.js
Add the following code to app.js
:
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello, MEAN Stack!');
});
app.listen(port, () => {
console.log(`App listening at http://localhost:${port}`);
});
5. Run the server
node app.js
Visit http://localhost:3000
to see your server in action.
Install Angular
Angular is a front-end framework used for building dynamic web applications. We have already discussed details about Angular in our previous article.
- Install the Angular CLI globally:
npm install -g @angular/cli
2. Create a new Angular application:
ng new frontend
Follow the prompts to set up your Angular project.
Now, navigate into your project directory and start the development server:
Conclusion
By following these steps, you’ve successfully installed and set up the MEAN stack on your Ubuntu server. This powerful stack enables you to build full-stack applications with a consistent JavaScript environment, from the client-side to the database. With MongoDB efficiently handling your data, and as Express.js seamlessly manages your server logic, while Angular meticulously crafts your user interface, and Node.js serves as the robust backbone, you’re fully equipped to develop dynamic and responsive web applications.
Also Reads: