I recently had to deploy a react app on a Digital Ocean Droplet and when I run npm install it got "killed":

Command line running command npm install, ultimately failing with the term "killed"

After some google searches I found that this had to do with Swap Memory.

Swap is an designated area on a hard drive where the operating system temporary stores data when it runs out of RAM.

Here are the exact commands I used to solve the problem (Works on Ubuntu14.04, Ubuntu16.04 and Ubuntu18.04):

Step 1:

sudo fallocate -l 1G /swapfile

Step 2:

sudo chmod 600 /swapfile

Step 3:

sudo mkswap /swapfile

Step 4:

sudo swapon /swapfile

Step 5:

sudo swapon --show

Step 6:

sudo cp /etc/fstab /etc/fstab.bak

Step 7:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Step 8:

sudo sysctl vm.swappiness=10

Step 9:

echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

Step 10:

sudo sysctl vm.vfs_cache_pressure=50

Step 11:

echo 'vm.vfs_cache_pressure=50' | sudo tee -a /etc/sysctl.conf

The commands have created a 1GB swap file.
Run npm install again and everything should be working fine.

Further reading / sources

  1. What is swap memory?
  2. Swap File
  3. How To Add Swap Space on Ubuntu 18.04
  4. NPM gets killed no matter what