If you run Node.js, Next.js, React, Laravel or Docker on a small VPS with 1 GB or 2 GB of RAM, you’ve probably watched your server freeze or crash in the middle of a build. One of the simplest ways to make it more stable is to add a swap file. This guide explains what swap memory is, why it helps, and exactly how to set it up on Ubuntu.
On this page
A swap file is disk space that Linux uses as extra virtual memory when your physical RAM is full. Think of it as an emergency backup for your RAM.
For example, with:
RAM: 2 GB
Swap: 2 GB
When the 2 GB of RAM is exhausted, Linux can temporarily use the swap file instead of immediately killing running processes.
Note: Swap is much slower than RAM because it lives on your storage device. It improves stability but does not replace adding more physical memory.
Why create a swap file?
A swap file can help you:
Prevent server crashes caused by low memory
Reduce “Out of Memory” (OOM) errors
Keep Node.js builds from failing
Improve stability during software updates
Let small VPS instances ride out temporary memory spikes
This is especially useful on 1 GB and 2 GB RAM VPS plans, budget cloud servers and development servers.
Step 1: Check existing memory
Run:
free -h
Example output:
total used free
Mem: 2.0Gi 1.7Gi 120Mi
Swap: 0B 0B 0B
If Swap shows 0B, your server has no swap configured. You can also verify with:
A swap file is one of the simplest ways to improve the reliability of a small Linux VPS. It won’t replace physical RAM, but it gives you a safety net during memory spikes and can stop your server crashing during demanding tasks.
If you regularly build large applications or host several services, enabling swap is a worthwhile first step. For consistently heavy workloads, upgrading to a VPS with more RAM will give the best long-term performance.
Frequently asked questions
Does a swap file replace RAM?
No. Swap is disk space used as overflow when RAM runs out. It is much slower than RAM, so it improves stability during spikes but is not a substitute for more physical memory.
How much swap should I add on a 2 GB VPS?
About 2 GB is a sensible default for a 2 GB RAM server. More isn’t automatically better, because heavy swapping slows things down.
Why does my Node.js or Next.js build crash on a small server?
Builds are memory-hungry. When RAM is exhausted the Linux OOM killer terminates the process. A swap file gives the build room to finish instead of being killed.
What is swappiness and what value should I use?
Swappiness controls how eagerly Linux moves data to swap. For most web servers a value of 10 is a good balance, keeping data in RAM until memory is genuinely tight.
Will the swap file survive a reboot?
Only if you add it to /etc/fstab (Step 7). Without that entry the swap file is disabled the next time the server restarts.
Server giving you trouble?
Kondipress builds, deploys and keeps web apps running for Nigerian businesses, from VPS setup to going live. Chat with us on WhatsApp.
Recent Comments