Deploy Node.js on ubuntu with GIT

Install Git on your server

ssh into your VM, then install Git:

apt-get update
apt-get install git

Clone your private/public repo

#For public repo

Remove old node and add new node

sudo apt-get purge --auto-remove nodejs npm

Install pm2

npm -g install pm2

Kill other PM2 Process

pm2 kill

Create an environment variable file

//cd to the git repo
nano .env
//paste all variables by right clicking

Update the local repo

git pull origin master

If you get permission denied error which might happen if the repo is private follow the following steps

# Make sure ssh agent is working
eval "$(ssh-agent -s)"

Run PM2

pm2 start index.js --name "GingerCream"

For automatic deployment, u can follow the following methods

  • Github actions (CI & CD)
  • post-receive Git hook

Written by

Finding Magic.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store