How To

Add Swap Space on Ubuntu 20.04: Create, Enable, and Tune

Swap space is an area on disk that Linux uses when it runs out of physical RAM. When the system needs more memory than is available, it moves inactive pages from RAM to swap to free up space for active processes.

Swap can be a dedicated partition or a swap file. On virtual machines, a swap partition is usually not present, so a swap file is the go-to solution.

This guide shows you how to add swap space on Ubuntu 20.04, how to make it persist across reboots, and how to tune and remove it.

What to Know Before You Start

Swap is slower than RAM because it uses your hard drive. It helps prevent crashes when memory runs out, but it is not a replacement for more RAM. If your server constantly hits its memory limit, the real fix is to upgrade the RAM.

How much swap you need depends on how much RAM you have:

  • Less than 2 GB RAM: use twice the amount of RAM
  • 2 to 8 GB RAM: use the same size as your RAM
  • More than 8 GB RAM: use at least 4 GB of swap

You need root or sudo access to create and activate a swap file.

Add Swap Space on Ubuntu 20.04

This example creates a 2 GB swap file. Replace 2G with the size you need.

Create the swap file using fallocate:

bashsudo fallocate -l 2G /swapfile

If fallocate is not available or returns an error on your system, use this alternative:

bashsudo dd if=/dev/zero of=/swapfile bs=1024 count=2097152

Set permissions to 600 so only root can read and write the file:

bashsudo chmod 600 /swapfile

Format the file as a Linux swap area:

bashsudo mkswap /swapfile

Activate the swap:

bashsudo swapon /swapfile

The swap is now active, but it will not survive a reboot yet. To make it permanent, open /etc/fstab:

bashsudo nano /etc/fstab

Add this line at the end of the file:

/swapfile swap swap defaults 0 0

Save and close the file. The swap file will now enable itself automatically on every boot.

Verify the swap is active:

bashsudo swapon --show

Or check with free:

bashsudo free -h

You should see swap listed in the output with its size and usage.

Adjust the Swappiness Value

Swappiness controls how often Linux moves data from RAM to swap. It ranges from 0 to 100:

  • A low value (like 10) tells the kernel to keep data in RAM and only swap when it has to
  • A high value (like 80) tells the kernel to swap more freely
  • The default on Ubuntu is 60

For most desktop systems, 60 is fine. For production servers, a lower value like 10 is often better because it keeps more working data in RAM and reduces disk activity.

Check the current swappiness value:

bashcat /proc/sys/vm/swappiness

Set a new value that takes effect right away but resets on reboot:

bashsudo sysctl vm.swappiness=10

To make the change permanent, open /etc/sysctl.conf and add:

vm.swappiness=10

The right swappiness value depends on your workload. Start at 10 for servers and adjust in small steps based on how your system behaves.

Remove the Swap File

To fully remove the swap file, follow these steps in order.

Turn off the swap:

bashsudo swapoff -v /swapfile

Open /etc/fstab and delete the line:

/swapfile swap swap defaults 0 0

Then remove the swap file itself:

bashsudo rm /swapfile

Swap space is now set up and running on your Ubuntu server. It gives the system breathing room when RAM gets tight and helps prevent out-of-memory crashes. Keep the swappiness value tuned to match how your server actually uses memory. Got questions? Leave a comment below.

Cyber Defence

Recent Posts

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

15 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

15 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

15 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

15 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago