April 20, 2023

How To Install Mean Stack On Ubuntu 20.04

Abhishek Thakur

MEAN stack is a popular web development stack that includes MongoDB, Express, Angular, and Node.js. In this blog, we will show you how to install the MEAN stack on Ubuntu 20 Server.

Step 1: Update Ubuntu 20.04

Before we start, it’s always a good idea to update your Ubuntu server to the latest version. To do this, run the following commands:

sudo apt update
sudo apt upgrade

Step 2: Install Node.js and NPM

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. To install Node.js and NPM, run the following command:

sudo apt install nodejs npm

Step 3: Install MongoDB

MongoDB is a popular NoSQL database that is used by many web applications. To install MongoDB, run the following commands:

sudo apt update
sudo apt install mongodb

After installation, start the MongoDB service and enable it to start on boot:

sudo systemctl start mongod
sudo systemctl enable mongod

Step 4: Install Angular CLI

Angular is a popular front-end JavaScript framework for building web applications. To install the Angular CLI, run the following command:

sudo npm install -g @angular/cli

Step 5: Install Express

Express is a popular web application framework for Node.js. To install Express, run the following command:

sudo npm install -g express

Step 6: Create a New MEAN Stack Application

Now that you have installed all the necessary components, you can create a new MEAN stack application. To do this, follow these steps:

Create a new directory for your application:

mkdir mean-app
cd mean-app

Initialize your application with NPM:

npm init -y

Install the necessary packages for your application:

npm install express mongodb mongoose body-parser cors

Create a new file called server.js and add the following code:

const express = require(‘express’);
const bodyParser = require(‘body-parser’);
const cors = require(‘cors’);
const mongoose = require(‘mongoose’);

const app = express();

// Middleware
app.use(bodyParser.json());
app.use(cors());

// Connect to MongoDB
mongoose.connect(‘mongodb://localhost:27017/mean-app’, { useNewUrlParser: true });

const connection = mongoose.connection;

connection.once(‘open’, () => {
console.log(‘MongoDB database connection established successfully!’);
});

// Start the server
const port = process.env.PORT || 4000;
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});

Start the server:node server.js.

Open your web browser and navigate to http://localhost:4000. You should see a message indicating that the server is running.

Conclusion

In this blog, we have shown you how to install the MEAN stack on Ubuntu 20.04 . By following these steps, you can have a fully functional web development stack that includes MongoDB, Express, Angular, and Node.js. You can use this stack to build powerful web applications with ease.


Buy Linux VPS

Popular Blog Posts