Create a swap file on Ubuntu

Small VPS, no swap, and npm install gets killed. Give it some room.

SNP 041 #369 Filed cmd ubuntu linux

shell
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Check it with free -h. To keep it after a reboot, add it to /etc/fstab:

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

If you only want swap used when memory is really gone, lower the swappiness:

shell
echo 'vm.swappiness=10' | sudo tee /etc/sysctl.d/99-swappiness.conf
sudo sysctl --system