How to Add Swap Memory on Ubuntu 20.04 with Swapfile and Block Storage

Swap memory is a storage segment used as virtual memory when your server’s RAM capacity is exhausted. It prevents system crashes by relocating less frequently used processes from RAM into swap space, ensuring stability during high load.

This guide explains how to add swap memory on Ubuntu 20.04 using both a swap file and Block Storage to expand available memory resources.

Prerequisites

Before starting, ensure that you have:

  • An Ubuntu 20.04 server
  • An existing block storage device
  • SSH access as a non-root user with sudo privileges

Check Existing Swap Memory

First, confirm the current swap usage before adding more. Use the free command to display memory information including RAM and swap.

Example output:

              total        used        free      shared  buff/cache   available
Mem:          955Mi       317Mi       201Mi       1.2Mi       598Mi       638Mi
Swap:         2.3Gi       268Ki       2.3Gi

From this, the server already has a 2GB swap memory volume.

Create Swap Memory

Swap memory can reside either on your server’s disk or on an attached block storage device. The following sections describe how to create swap space using both methods.

Create Swap with a Swapfile

Create a new swapfile of 2GB in the root directory using fallocate:

$ sudo fallocate -l 2G /swapfile.img

Adjust file permissions so only root can read and write:

$ sudo chmod 0600 /swapfile.img

Format the file as swap:

$ sudo mkswap /swapfile.img

Output example:

Setting up swapspace version 1, size = 2 GiB (2147479552 bytes)
no label, UUID=2ed3e083-fac2-4571-bbdf-e9967aa1fc03

Create Swap with Block Storage

List available storage devices with lsblk. The new disk will appear as /dev/vdb without partitions:

Example output:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1 1024M  0 rom
vda    253:0    0   25G  0 disk
├─vda1 253:1    0  512M  0 part /boot/efi
└─vda2 253:2    0 24.5G  0 part /
vdb    253:16   0   40G  0 disk

Initialize the block storage volume with a GPT label:

$ sudo parted -s /dev/vdb mklabel gpt

Create a new partition using the full capacity:

$ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%

Recheck devices:

Example output:

NAME   MAJ:MIN RM  SIZE RO TYPE MOUNTPOINTS
sr0     11:0    1 1024M  0 rom  
vda    253:0    0   25G  0 disk 
├─vda1 253:1    0  512M  0 part /boot/efi
└─vda2 253:2    0 24.5G  0 part /
vdb    253:16   0   40G  0 disk 
└─vdb1 253:17   0   40G  0 part 

Format the partition as swap:

Example output:

Setting up swapspace version 1, size = 40 GiB (42947571712 bytes)
no label, UUID=7b3b6fa6-b344-41bd-b25b-f8657caa36b4

Check block devices with blkid and note the UUID:

Example output:

/dev/vda2: UUID="95e88749-c308-4c15-aca0-f47049d0c699" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="48874572-7e1f-4766-93e7-431038bd78f3"
/dev/vda1: UUID="D587-7645" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="96ca28e5-4696-482c-9359-24b87f2ea53e"
/dev/vdb1: UUID="ca230c16-b5a4-44e6-b5a8-930bb1f33fcf" TYPE="swap" PARTLABEL="primary" PARTUUID="7897c216-dc99-4e91-be17-abc1a0dba849"

Enable Swap Memory

Activate the swapfile using swapon:

$ sudo swapon /swapfile.img

Verify active swap:

Example output:

Filename                                Type            Size            Used            Priority
/swapfile                               file            2457596         268             -2
/swapfile.img                           file            2097148         0               -3

Enable the block storage partition as swap:

Recheck swap devices:

Example output:

Filename                                Type            Size            Used            Priority
/swapfile                               file            2457596         268             -2
/swapfile.img                           file            2097148         0               -3
/dev/vdb1                               partition       41940988        0               -4

Configure fstab for Automatic Swap Mounting

The fstab file defines how file systems are mounted at boot. Follow these steps to configure automatic swap mounting during startup.

First, back up the original configuration:

$ sudo cp /etc/fstab /etc/fstab.bak

Open the configuration file in a text editor such as nano:

Add this swapfile configuration at the end of the file:

/swapfile.img swap swap defaults 0 0

This entry enables swapfile mounting at boot. Breakdown:

  • swap: Identifies the swap file system type
  • swap: Sets the swap mount point
  • 0: Disables backups for this file system
  • 0: Disables file system checks for swap

Next, display block device information and copy the UUID of /dev/vdb1:

Example output:

/dev/vda2: UUID="95e88749-c308-4c15-aca0-f47049d0c699" BLOCK_SIZE="4096" TYPE="ext4" PARTUUID="48874572-7e1f-4766-93e7-431038bd78f3"
/dev/vda1: UUID="D587-7645" BLOCK_SIZE="512" TYPE="vfat" PARTUUID="96ca28e5-4696-482c-9359-24b87f2ea53e"
/dev/vdb1: UUID="ca230c16-b5a4-44e6-b5a8-930bb1f33fcf" TYPE="swap" PARTLABEL="primary" PARTUUID="7897c216-dc99-4e91-be17-abc1a0dba849"

Add the swap partition entry using the UUID:

UUID=ca230c16-b5a4-44e6-b5a8-930bb1f33fcf swap swap sw 0 0

Replace the UUID with your server’s actual value. After this step, swap memory is automatically mounted and enabled at every boot.

Configure Swappiness

Swappiness controls how aggressively the system uses swap compared to RAM. Behavior by value:

  • 0: Swap is avoided unless absolutely necessary
  • 1–49: Swap is used sparingly, RAM is prioritized
  • 50: Balanced usage between RAM and swap
  • 51–99: More aggressive use of swap
  • 100: Prefers swap heavily, RAM is freed quickly

Set a swappiness value by adding it to /etc/sysctl.conf:

$ echo "vm.swappiness = 50" | sudo tee -a /etc/sysctl.conf

Output:

vm.swappiness = 50

Reload the configuration to apply changes:

Test the Swap Memory

List active swap volumes:

Example output:

Filename                                Type            Size            Used            Priority
/swapfile                               file            2457596         268             -2
/swapfile.img                           file            2097148         0               -3
/dev/vdb1                               partition       41940988        0               -4

Enable all inactive swap volumes defined in /etc/fstab:

View memory usage and verify swap activity:

Example output:

              total        used        free      shared  buff/cache   available
Mem:          955Mi       348Mi       155Mi       1.2Mi       613Mi       607Mi
Swap:          44Gi       268Ki        44Gi

Remove Swap Memory

To disable swap, use swapoff followed by the file or partition path. For example, disabling the default swap file:

Verify that the file is disabled by listing swap devices:

Example output:

Filename                                Type            Size            Used            Priority
/swapfile.img                           file            2097148         0               -2
/dev/vdb1                               partition       41940988        0               -3

Conclusion

You have successfully configured swap memory on Ubuntu 20.04, expanding your system’s available resources. Although swap is slower than RAM, it improves performance stability by moving less-used or heavy processes away from primary memory.

Source: vultr.com

Create a Free Account

Register now and get access to our Cloud Services.

Posts you might be interested in: