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
    1. What is a swap file?
    1. Why create a swap file?
    1. Step-by-step setup
    1. How much swap should you create?
    1. Common use cases
    1. Troubleshooting
    1. Frequently asked questions

What is a swap file?

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:
swapon --show

Step 2: Create a 2 GB swap file

sudo fallocate -l 2G /swapfile
If your system doesn’t support fallocate, use:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Step 3: Secure the swap file

Linux requires the swap file to have restricted permissions. Run:
sudo chmod 600 /swapfile

Step 4: Format the swap file

Initialize the file as swap space:
sudo mkswap /swapfile
Example output:
Setting up swapspace version 1, size = 2 GiB

Step 5: Enable swap

sudo swapon /swapfile

Step 6: Verify it is working

free -h
Example:
               total        used        free
Mem:           2.0Gi       1.3Gi       450Mi
Swap:          2.0Gi         0B        2.0Gi
You can also check:
swapon --show

Step 7: Make swap permanent

Without this step, the swap file is disabled after a reboot. Run:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Now the swap file will be enabled automatically every time the server starts.

Step 8: Optimize swappiness

Linux controls how aggressively it uses swap through the swappiness value. Check the current value:
cat /proc/sys/vm/swappiness
For most web servers, a value of 10 is a good balance. Set it temporarily:
sudo sysctl vm.swappiness=10
Make it permanent:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf

How much swap should you create?

RAM Recommended swap
1 GB 1–2 GB
2 GB 2 GB
4 GB 2–4 GB
8 GB 2–4 GB
More swap isn’t always better. Excessive swapping can slow your server down, because disk storage is far slower than RAM.

Common use cases

Creating a swap file is especially helpful when:
    • Building Next.js applications
    • Running React production builds
    • Compiling large Node.js projects
    • Running Docker containers
    • Hosting multiple websites
    • Running Nginx, PM2 and MySQL together on a small VPS

Troubleshooting

“fallocate: Operation not supported”

Use the dd command instead:
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048

Verify swap is active

swapon --show
or
free -h

Disable swap

If you ever need to turn it off:
sudo swapoff /swapfile

Final thoughts

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.