Ubuntu Out of Memory: Add Swap the Safe Way

Symptom

Processes killed by oom-killer; no swap configured.

Root cause

Physical RAM exhausted with no swap buffer.

Fix, in order

1. Confirm zero swapfree -h # swap row all zeros
2. Create a 2G swapfile with safe permsfallocate -l 2G /swapfile && chmod 600 /swapfile && mkswap /swapfile && swapon /swapfile
3. Make it survive rebootecho '/swapfile none swap sw 0 0' >> /etc/fstab

Prevent it coming back

Swap is a pressure valve, not extra RAM: alert before it is used.

Deep-dive article

Full walkthrough: Ubuntu Out of Memory: Add Swap the Safe Way on the NinjaOps blog.

Run it on clean infra

Spinning up a fresh box to reproduce or escape this error?

Servers with headroom

Related

Cloud & DevOps tool comparisons · Hardware build guides

NinjaOps publishes free engineering guides. Some outbound links are affiliate links: they cost you nothing and support the site.

Last reviewed 2026-10-02 · NinjaOps SEO grid · DevOps blog