Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Install & Setup MEAN Stack on Ubuntu (MongoDB, Express.JS, Angular.JS, Node.JS)
The MEAN stack is a popular web development framework consisting of MongoDB, Express.js, Angular.js, and Node.js. It is an open-source platform that allows developers to create robust web applications quickly and efficiently. In this article, we will guide you through the installation and setup process of the MEAN stack on Ubuntu.
Step 1: Install Node.js and NPM
Node.js is the runtime environment that allows developers to run JavaScript code outside of the browser. It is the backbone of the MEAN stack. To install Node.js on Ubuntu, follow these steps
Open the terminal on Ubuntu by pressing Ctrl+Alt+T.
Type the following command to add the Node.js PPA (Personal Package Archive) to your system
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
Once the PPA is added, run the following command to install Node.js and npm
sudo apt-get install -y nodejs
To verify the installation, type the following command to check the version of Node.js and npm
node -v npm -v
If the installation is successful, the version numbers of Node.js and npm will be displayed on the terminal.
Step 2: Install MongoDB
MongoDB is a NoSQL database that stores data in JSON-like documents. It is a popular choice among developers because of its flexibility and scalability. To install MongoDB on Ubuntu, follow these steps
Open the terminal and type the following command to import the MongoDB public GPG key
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add -
Next, add the MongoDB repository to the list of sources on your system by running the following command
echo "deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu $(lsb_release -sc)/mongodb-org/6.0 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list
Once the repository is added, update the package list and install MongoDB by running the following commands
sudo apt-get update sudo apt-get install -y mongodb-org
To start the MongoDB service, run the following command
sudo systemctl start mongod
To enable MongoDB to start at boot, run the following command
sudo systemctl enable mongod
To verify the installation, type the following command to check the status of the MongoDB service
sudo systemctl status mongod
If the installation is successful, the MongoDB service status should be displayed as "active (running)".
Step 3: Install Express.js and Angular.js
Express.js is a web application framework for Node.js that provides a set of tools and features for building web applications. Angular.js is a JavaScript framework for building dynamic web applications. To install Express.js and Angular.js on Ubuntu, follow these steps
Open the terminal and navigate to your project directory
Install Express.js by running the following command
npm install express --save
Install Angular.js by running the following command
npm install angular --save
Step 4: Create a MEAN Stack Application
Now that we have installed all the required components, let's create a simple MEAN stack application to test our installation.
Open the terminal and navigate to your project directory.
Create a new directory for your MEAN stack application by running the following command
mkdir mean-app
Navigate to the new directory by running the following command
cd mean-app
Initialize a new Node.js project
npm init -y
Create a new file called server.js by running the following command
touch server.js
Open the server.js file in your preferred text editor and add the following code
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World from MEAN Stack!');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
Save the server.js file.
Start the server by running the following command
node server.js
Open a web browser and navigate to http://localhost:3000. You should see the message "Hello World from MEAN Stack!" displayed on the page.
Test MongoDB Connection
To verify that MongoDB is working properly, open a new terminal and run the MongoDB shell
mongosh
Create a test database and insert a document
use testdb
db.users.insertOne({name: "John", age: 30})
{
acknowledged: true,
insertedId: ObjectId("...")
}
Query the inserted document
db.users.find()
[
{
_id: ObjectId("..."),
name: "John",
age: 30
}
]
Conclusion
The MEAN stack is a powerful web development framework that allows developers to create robust web applications quickly and efficiently. By following the steps outlined in this article, you can easily install and set up the MEAN stack on Ubuntu and create your own applications with MongoDB for data storage, Express.js for server-side logic, Angular.js for frontend, and Node.js as the runtime environment.
