Guide: Installing MEAN on Ubuntu 16.04
Introduction
This guide will give you an introduction on how to run a MEAN site, the article is focused on Ubuntu 16.04 and will give you the basic concepts for installing it on similar distros.
Prerequisites
You will need a server with Ubuntu 16.04 (x64) I recommend at least 2GB ram, or 1GB ram and more than 2GB swap available, MEAN can be quite resource demanding at times.
You will need to install git before you can create your first MEAN application (Step four) simply type
sudo apt install git
Step one – MongoDB
First, we will install the database for the MEAN stack, our MongoDB
sudo apt install mongodb
This command will install a MongoDB with no data in it.
Step two – NodeJS and required packages for MEAN
Next, we need to install NodeJS and the package manager npm, this can be done in just one command.
sudo apt install nodejs npm
This is a large package so expect it to take a little while.
npm will be installed with some compatibility issues because it has NodeJS binary hardcoded as /usr/bin/node, however its actually in /usr/bin/node on our system, we can easily fix this with a symbolic link.
sudo ln -s /usr/bin/nodejs /usr/bin/node
The above command will not show any completion text in your terminal window. If you start on a new line without errors the symbolic link was established correctly
Now that we have our package manager tool (npm) we need to install gulp (a build tool) and bower (a frontend package manager)
sudo npm install -g gulp bower
npm installs gulp and bower globally, to confirm we have it installed correctly go to their folder
cd /usr/local/lib/node_modules/
Now do the following command to see the available packages
npm list
This will give you a long treelist of all installed packages, however if your top input is similar to:
/usr/local/lib
+-- bower@1.8.0
`-- gulp@3.9.1
Then you have successfully installed the packages.
Before continuing jump down to your home folder in your terminal, or where you wish to install your MEAN application, I will be using my home folder
cd
Will put me back to /home/kenneth
Step Three – MEAN’s command line interface
To install the cli we will use npm
sudo npm install -g mean-cli
Step Four – Creating our first MEAN application
To get started simple use the command
mean init example
This will give you 2 prompts as show in the picture below with ? beforehand.
The first is a prompt for the name of your application, this can be anything but I have named mine example
The second is if you want to set up an admin user, for simplicity let’s pick no as this will most likely be your first application it shouldn’t be needed.
Now you have installed your first MEAN application, lets head into its directory and finish installing some dependencies before running the application.
cd example
sudo npm link gulp
npm install
We are linking gulp into the application since we did not install it globally, we are also installing our npm into the folder of our MEAN application before running it.
note npm can throw some warnings here (seen below), but no need to worry it’s not something we need to worry about for our application, it will still finish the installation.
Step Five – Starting our application!
Now that we have everything installed we can fire up our application, we will be using gulp. this will start the application in the terminal window, so if you exit the application will close. this is good however for easy debugging.
gulp
You should now see the application start up and it should be available on your server, I have installed it on a Virtual Machine with the IP 192.168.1.110 (replace this IP with whatever IP is on the server/client you installed the application on) so in order to see if it works I will head to the following website address to test
http://192.168.1.110:3000
Success! our terminal should output the following to show we have made a succesful request to the application.
Step Six – running the application at startup automatically
To configure it automatically starting we will need to enable rc-local and edit the file
sudo systemctl enable rc-local.service
Enables the execution of rc.local on startup, now edit the file
sudo nano /etc/rc.local
Add the following to the file before the exit line:
sudo -H -u USERNAME bash -c 'cd /home/USERNAME/example && nohup node server &'
Remember to replace USERNAME with the username on your server/client with the application installed.
The file should look like this: