Flash Sale:75% Off Hosting + Free DomainEnds in13h47m14sView Plans
Hostvento logoHostvento

Stop "npm install" from being killed: add a swap file

On a small server, npm install can stop with the word "Killed". This guide shows how to add swap space so it can finish. It needs root access, so it is meant for VPS or dedicated server users.

Web Hosting2 min read11 steps

Why does npm get killed?

npm is a tool that downloads code packages for Node.js projects. It can use a lot of memory. When the server runs out of memory (RAM), Linux protects itself and kills the biggest program. You see Killed.

Swap is a spare area on the disk that Linux uses like extra memory. It is slower than real RAM, but it stops crashes.

Step 1: Check current swap

  1. Log in to your server with SSH.
  2. Run this command:
free -h

It shows memory and swap in easy units. If the Swap line says 0, you have none.

Step 2: Create a swap file

Check you have free disk space first with df -h. These steps do not delete data, but a full disk causes other problems.

  1. Create a 2 GB file:
fallocate -l 2G /swapfile

This reserves 2 GB of disk space for swap.

  1. Lock the file so only root can read it:
chmod 600 /swapfile
  1. Format it as swap:
mkswap /swapfile
  1. Turn it on:
swapon /swapfile
  1. Run free -h again. The Swap line should now show 2.0G.

Step 3: Keep it after a reboot

  1. Back up the file first:
cp /etc/fstab /etc/fstab.bak
  1. Add the swap file to the list of disks:
echo '/swapfile none swap sw 0 0' >> /etc/fstab

This line tells Linux to turn swap on at every start.

Step 4: Run npm again

  1. Go to your project folder.
  2. Run npm install again.
Tip: If fallocate fails on your system, use dd if=/dev/zero of=/swapfile bs=1M count=2048 instead. Swap is a safety net. If you often run out of memory, a bigger plan may help.

Quick recap

  • "Killed" means the server ran out of memory.
  • Check swap with free -h.
  • Create, lock, format and enable a swap file.
  • Add it to /etc/fstab to keep it after reboot.