# 🚀 How to Increase Swap Space on Ubuntu (DigitalOcean Droplet Friendly)

Shasa ThuoShasa Thuo
2 min read

If you're running a server with limited RAM — like a 2GB DigitalOcean droplet — you might run into memory issues when building large applications (hello oom-kill). A quick and effective solution is to increase your swap space.

This guide walks you through adding or resizing swap on Ubuntu in a safe and permanent way.


🧠 What is Swap?

Swap space is disk-based memory that your system uses when RAM is full. It’s slower than RAM but can prevent your apps from crashing due to memory exhaustion.


🛠️ Step-by-Step Guide

1. Check Existing Swap

Open your terminal and check if swap is currently enabled:

swapon --show
free -h

If swapon returns nothing and Swap: shows 0B, you currently have no active swap.

Turn Off and Remove Old Swap (If Exists)

sudo swapoff /swapfile
sudo rm /swapfile

Create a New Swap File

Let’s create a 4GB swap file. You can adjust the size (4G) as needed.

sudo fallocate -l 4G /swapfile

If fallocate fails, use this instead:

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096

Secure the swap file

sudo chmod 600 /swapfile

Format and Enable the Swap File

sudo mkswap /swapfile
sudo swapon /swapfile

Now Check it

swapon --show

or

free -h

Make It Persistent Across Reboots

Edit the /etc/fstab file

sudo nano /etc/fstab

Add this line at the bottom:

/swapfile none swap sw 0 0

You now have a bigger swap space ready to handle memory spikes on your server.

Want to Clear Swap Manually?

sudo swapoff -a
sudo swapon -a

This disables and re-enables all swap, effectively clearing it.

0
Subscribe to my newsletter

Read articles from Shasa Thuo directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Shasa Thuo
Shasa Thuo

I am a developer from Nairobi, interested in Python, Django, Javascript and React