How to increase memory (RAM) on DigitalOcean Droplets for free - kdaisho/Blog GitHub Wiki
Troubleshoot
npm install
won't complete. It tries, but ending up throwing errors as follows;
npm ERR! code ENOMEM
npm ERR! syscall spawn
npm ERR! errno -12
npm ERR! spawn ENOMEN
This is because the machine doesn't have enough memory. Happened to me with a VPS from DigitalOcean which was provisioned with 1GB of RAM. 1GB may not enough for a Gatsby app. High memory usage was observed for a build process too.
Solution
A swap file can be a solution. I wondered if the droplet had it set up?
Check
free -m
And the output suggested that there is no swap file provisioned. This is how the output looked;
total used free shared buff/cache available
Mem: 492 109 280 4 102 366
Swap: 0 0 0
So we need to set up a swapfile.
Allocate 1GB to swap file
sudo fallocate -l 1G /swapfile
I could have done with 2GB, but I allocated 1GB for my droplet.
Set appropriate permissions for the swap area file
sudo chmod 600 /swapfile
Setup the swap area using the space allocated above
sudo mkswap /swapfile
Enable the swap area
sudo swapon /swapfile
Veify that swap area has been enabled
sudo swapon -s
Output
Filename Type Size Used Priority
/swapfile file 2097148 44088 -1
/etc/fstab
file. Add the following line to the /etc/fstab
file.
To make the swap file permanent, we need to add it to /swapfile none swap sw 0 0
After doing all the above, npm install
start to be completed successfully.
Gatsby's build process completes a lot faster now 🤗