Arch Linux BasedManjaro

How To Install MEAN Stack on Manjaro

Install MEAN Stack on Manjaro

Manjaro, an Arch-based Linux distribution, offers a versatile environment for developers who want a cutting-edge and user-friendly operating system. One of the most powerful combinations for modern web development is the MEAN stack. MEAN stands for MongoDB, Express.js, Angular, and Node.js—four technologies that use JavaScript throughout the entire development process. This setup lets you create high-performing, scalable applications for various purposes, ranging from simple prototypes to sophisticated production projects.

Below is a comprehensive, step-by-step guide on how to install and configure the MEAN stack on Manjaro Linux. By the end of this tutorial, you’ll have a functional environment ready for development and deployment. You will also learn about best practices, optimization tips, and troubleshooting techniques to ensure a smooth workflow with this popular technology bundle.

Prerequisites

Before you begin, ensure you meet the following requirements:

  • An up-to-date Manjaro system installed on your machine (preferably the latest release).
  • Basic knowledge of the Linux command line, including how to use sudo privileges.
  • A stable internet connection to fetch packages from official and community repositories.
  • At least 2 GB of RAM and ample free disk space (preferably 10 GB or more) for data and logs.

Understanding the MEAN Stack Architecture

To fully appreciate the power of MEAN, it is helpful to break down each component:

  1. MongoDB: A NoSQL document database used to store and manage data in a flexible JSON-like format. Its ability to scale horizontally and handle large volumes of data makes it essential for modern web applications.
  2. Express.js: A lightweight Node.js framework that simplifies server-side coding. It provides numerous features for building web applications, APIs, and dynamic websites using JavaScript.
  3. Angular: A TypeScript-based front-end framework maintained by Google. Angular handles client-side logic, user interfaces, and data binding, resulting in a dynamic, highly responsive user experience.
  4. Node.js: A server-side JavaScript runtime environment built on Google’s V8 engine. Node.js allows developers to run JavaScript code outside of a browser and is typically used to power network applications and servers.

All these components work seamlessly to form a robust platform. Because they rely on JavaScript, data can flow easily between front-end and back-end.

Step 1: Update the System

Manjaro is a rolling-release distribution, meaning packages update frequently. Ensuring your system is fully updated before proceeding wards off unexpected conflicts or issues:

sudo pacman -Syu

If prompted to confirm installation of packages or updates, press Y. Once the process completes, reboot your machine if the system suggests it, ensuring all new changes are properly applied.

Step 2: Install Node.js and npm on Manjaro

Node.js and its package manager (npm) are the foundation for both server-side development and for installing other necessary components. Manjaro’s official repositories often include multiple Node.js versions. You can choose between the latest stable and LTS (Long-Term Support) versions. For most projects, LTS is a safe bet:

sudo pacman -S nodejs npm

After installation, verify successful setup:

node -v
npm -v

You should see the current Node.js and npm versions displayed. If you prefer a specific version of Node.js that is not available by default, you could explore a community repository or use an AUR (Arch User Repository) helper like yay or pamac. However, the official repository version is generally sufficient for most MEAN stack projects on Manjaro.

Step 3: Install MongoDB on Manjaro

MongoDB is a core part of the MEAN architecture. While official MongoDB installation documentation often focuses on distributions like Debian or Ubuntu, Manjaro provides a simple approach to get MongoDB up and running. Use the Manjaro repositories in the command below:

sudo pacman -S mongodb-bin

This package name may vary depending on the repository. If the mongodb-bin package is not found, you can install mongodb or community/mongodb.

Once MongoDB is installed, create a data directory (if the installation process did not do so automatically) for local usage:

sudo mkdir -p /var/lib/mongodb
sudo chown -R mongodb:mongodb /var/lib/mongodb

Then enable and start the MongoDB service:

sudo systemctl enable --now mongodb

Check for the status to ensure everything is working:

systemctl status mongodb

If you see it running without errors, MongoDB has started successfully on Manjaro.

Step 4: Install Express.js

Express.js simplifies the process of routing in your Node.js applications. You will build server logic with Express. Although you can install it globally, using it in a local project folder is typically recommended to avoid version conflicts. However, to confirm that Express is accessible via the command line, you can do a global install:

sudo npm install -g express-generator

Alternatively, you can initialize a project directory:

mkdir my-mean-project
cd my-mean-project
npm init -y
npm install express

This approach stores Express locally in the node_modules folder of your project, capturing the version in the package.json. You will later import this local Express module in your Node.js files to create servers, define routes, and handle middleware.

Step 5: Install Angular CLI

Angular is one of the most popular front-end frameworks for building dynamic single-page applications. The Angular CLI (Command Line Interface) greatly simplifies project scaffolding and development tasks. To install it globally, run:

sudo npm install -g @angular/cli

Confirm the installation:

ng version

You should see Angular CLI details, along with associated environment information. This global installation allows you to create, build, and serve Angular projects seamlessly on your system.

Step 6: Testing the Installation

Now that Node.js, npm, MongoDB, Express.js, and Angular are set up, test to ensure everything is integrated:

  • Node.js and npm: Already validated by running node -v and npm -v. Both versions should be displayed properly.
  • MongoDB: Check if the mongodb service is running. Use systemctl status mongodb or attempt a MongoDB connection:
mongo --eval "db.runCommand({ connectionStatus: 1 })"

A successful status object means MongoDB is operational.

  • Angular CLI: Confirm by running ng version. This ensures the CLI is recognized.
  • Express.js: To verify Express functionality, create a quick JavaScript file that uses Express, or run express --version if installed globally via Express Generator.

Step 7: Creating a Simple MEAN Application

Let’s walk through creating a basic test application that demonstrates the synergy of MongoDB, Express.js, Angular, and Node.js.

7.1 – Directory Setup

In your home directory, create a dedicated folder for your MEAN test project:

mkdir mean-demo
cd mean-demo

7.2 – Express Backend Setup

Initialize a Node.js project:

npm init -y

Install Express and MongoDB driver:

npm install express mongoose

Create a file named server.js in the mean-demo folder:

touch server.js
nano server.js

Open it with an editor (for instance, nano), then add the following content:

const express = require('express');
const mongoose = require('mongoose');
const app = express();
const port = 3000;

// Middleware to parse JSON
app.use(express.json());

// Connect to MongoDB
mongoose.connect('mongodb://localhost:27017/mean_demo', {
  useNewUrlParser: true,
  useUnifiedTopology: true
}).then(() => {
  console.log('Connected to MongoDB');
}).catch((err) => {
  console.error('MongoDB connection error:', err);
});

// Sample route
app.get('/', (req, res) => {
  res.send('Hello from Express and MongoDB on Manjaro!');
});

// Start the server
app.listen(port, () => {
  console.log(`Server running on port ${port}`);
});

Exit and save the file. This file sets up an Express.js server listening on port 3000, connects to a local MongoDB database named mean_demo, and defines a basic route.

Run the server to test your back end:

node server.js

Open a web browser or use a tool such as curl, then navigate to http://localhost:3000. The “Hello from Express and MongoDB on Manjaro!” message should appear.

7.3 – Angular Frontend Setup

In the same mean-demo directory (or a separate directory if you prefer a structured approach), create your Angular application using the CLI:

ng new frontend

When prompted, choose the features you want (e.g., routing, stylesheet format). After generation, switch into the created frontend directory:

cd frontend

Test the Angular application:

ng serve --open

This starts a local development server, typically on http://localhost:4200. Your browser should open automatically.

Once you see the default Angular welcome page, confirm the server is running properly. Then, you can customize the AppComponent or create new components to communicate with your Express/Node back end. For cross-origin resource sharing, set up CORS in the Node.js server if needed:

npm install cors

// In server.js
const cors = require('cors');
app.use(cors());

Optimize Performance

While the MEAN stack is already quite efficient, there are a few ways to further optimize performance:

  • Enable Production Mode in Angular: Before deployment, build Angular in production mode using ng build --prod. This step minifies and optimizes JavaScript and CSS for faster load times.
  • Use Build Tools: Consider bundlers and optimization tools (e.g., Webpack, Rollup) for advanced improvements in your front-end modules.
  • Efficient Database Queries: Use MongoDB indexes to accelerate queries. Carefully design schemas to reduce unnecessary complexity.
  • Server-Side Caching: Implement caching layers or use memory stores such as Redis for frequently accessed data, reducing the load on MongoDB.
  • Load Balancing: For high-traffic scenarios, deploy multiple Node.js instances behind a load balancer. Also, consider cluster mode to leverage multi-core CPU usage.

Security Best Practices

Securing your MEAN-based applications should be taken seriously. Consider the following:

  • MongoDB Authentication: Enable authentication in the MongoDB configuration file, set up authorized users, and disallow anonymous connections.
  • HTTPS Everywhere: Use SSL/TLS certificates for any application in production. Tools like Let’s Encrypt automate the process of acquiring and renewing certificates.
  • Environment Variables: Store sensitive data such as connection strings and API keys in environment variables or configuration files that are not publicly exposed.
  • Regular Patching: Keep system packages, Node.js, and all dependencies updated to reduce vulnerabilities.
  • Express.js Middleware: Use middleware like helmet to set secure HTTP headers. Rate limiting is also important for mitigating brute-force attacks.

Troubleshooting Tips

Encountering issues on Manjaro or in your MEAN environment? Here are some common problems and solutions:

1. Package Not Found

If you receive “package not found” errors, especially for mongodb-bin or nodejs variants, it may mean that your mirrors are out of sync or the package name differs in your region. Check the official community or update repositories with:

sudo pacman-mirrors --fasttrack
sudo pacman -Syy

Then re-attempt:

sudo pacman -S mongodb-bin

2. MongoDB Fails to Start

This issue often arises from access or directory-related problems. Confirm ownership of /var/lib/mongodb is correctly set and your file paths exist:

sudo chown -R mongodb:mongodb /var/lib/mongodb
sudo systemctl restart mongodb

3. Port Conflicts

If node server.js fails to run, you might see an error about port 3000 already in use. You can:

  • Terminate existing processes blocking the port with sudo kill -9 <PID>.
  • Modify your application to use a different port (e.g., 4000 or 8080).

4. Angular Build Issues

If your Angular application fails to compile, check Node.js versions or conflicts among dependencies in package.json. Try removing node_modules and reinstalling:

rm -rf node_modules
npm install

Also verify you are using a compatible Node.js version. LTS versions typically ensure better stability.

5. CORS Errors

If your Angular front end cannot communicate with your Express/Node back end, you may see CORS-related console messages. Install and use the CORS package:

npm install cors

// Example usage in Express
const cors = require('cors');
app.use(cors());

Remember to restart your Node.js server for changes to take effect.

Congratulations! You have successfully installed MEAN Stack. Thanks for using this tutorial for installing MEAN Stack on Manjaro system. For additional help or useful information, we recommend you check the Debian website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button