How to Add Swap Memory in Ubuntu 24.04
Introduction
Swap memory is dedicated storage space used as virtual memory when the Random Access Memory (RAM) on your server is used up or fully utilized by actively running process. Swap memory enables the server to migrate less-used processes from RAM to the dedicated server space to free up your memory. As a result, when your server runs out of memory space, instead of crashing, additional processes are stored in your swap memory to keep the system healthy.
This article explains how to add swap memory on Ubuntu 24.04. You will set up swap memory using a dedicated file on your server storage and use Vultr Block Storage as a dedicated swap volume to increase the available server memory.
Prerequisites
Before you begin:
Deploy a Debian server on Vultr.
Deploy a Vultr Block Storage volume and attach it to the server.
Access the server using SSH as a non-root user with sudo privileges.
View Existing Swap Memory
Swap memory is available on Ubuntu 24.04 servers by default but with low dedicated space on the server. Follow the steps below to view the existing swap memory before adding new memory to the server.
View the available server memory including RAM and Swap using the free
utility.
$ sudo free -h
Verify the Swap memory available on your server similar to the output below.
total used free shared buff/cache available
1. Mem: 955Mi 317Mi 201Mi 1.2Mi 598Mi 638Mi
1. Swap: 2.3Gi 268Ki 2.3Gi
Based on the above output, the server includes a 2GB
Swap memory volume.
Create Swap Memory
Swap memory is dedicated space on a storage device such as your server storage or a block storage volume. Follow the sections below to create swap memory using either a Swapfile or a dedicated Vultr Block Storage volume on your server.
Create Swap Memory using a Swapfile
Create a new Swapfile with a specific size such as
2GB
using thefallocate
utility in your root directory/
.console$ sudo fallocate -l 2G /swapfile.img
Modify the Swapfile permissions to allow only the root user to read and write changes on the file.
console$ sudo chmod 0600 /swapfile.img
Format the file as swap using
mkswap
.console$ sudo mkswap /swapfile.img
Verify the new swam memory size, label information and UUID similar to the following output.
Setting up swapspace version 1, size = 2 GiB (2147479552 bytes) no label, UUID=2ed3e083-fac2-4571-bbdf-e9967aa1fc03
Create Swap Memory using Vultr Block Storage
List the storage devices available on your server using the
lsblk
utility. The new disk is attached as/dev/vdb
and it has no partitions.console$ lsblk
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 2G 0 disk
The Vultr Block Storage device is attached as
vdb
based on the above output with a40GB
size.Initialize the volume using the GPT partition table.
console$ sudo parted -s /dev/vdb mklabel gpt
Create a new partition using the entire block storage volume.
console$ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%
List the server storage devices again and verify that the new block storage partitiion is available.
console$ lsblk
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
Convert the new block storage partition to swap.
console$ sudo mkswap /dev/vdb1
Output:
Setting up swapspace version 1, size = 40 GiB (42947571712 bytes) no label, UUID=7b3b6fa6-b344-41bd-b25b-f8657caa36b4
View your block devices information and verify that the new swap partition is available.
console$ blkid
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"
Note the swap partition
UUID
value to use when mounting the volume automatically at boot time.
Enable Swap Memory
Enable the Swapfile as swap memory using the
swapon
utility.console$ sudo swapon /swapfile.img
View the server swap memory and verify that the new Swapfile is active.
console$ sudo swapon -s
Output:
Filename Type Size Used Priority /swapfile file 2457596 268 -2 /swapfile.img file 2097148 0 -3
Run the following command to enable the Vultr Block Storage swap partition as swap memory. Replace
/dev/vdb1
with your actual partition path.console$ sudo swapon /dev/vdb1
View the server swap memory and verify that the swap partition is active.
console$ sudo swapon -s
Output:
Filename Type Size Used Priority /swapfile file 2457596 268 -2 /swapfile.img file 2097148 0 -3 /dev/vdb1 partition 41940988 0 -4
Configure the File System Table (fstab) to enable Automatic Mounting
The File System Table (fstab) configuration defines how file systems are mounted on a server. Follow the steps below to configure fstab
to enable the automatic mounting of swap memory volumes on your server at boot time.
Backup the original
fstab
configuration on your server.console$ sudo cp /etc/fstab /etc/fstab.bak
Open the
fstab
configuration file using a text editor such asnano
.console$ sudo nano /etc/fstab
Add the following Swapfile configuration at the end of the file.
ini/swapfile.img swap swap defaults 0 0
Save and close the file.
The above configuration enables automatic mounting using the
/swapfile.img
file. Within the configuration:swap
: Enables the swap file system type.swap
: Sets the swap mount point.0
: Disables backups on the swap file system.0
: Disables file system checks on the swap memory.Add the block storage swap partition using its
UUID
value to enable automatic mounting on the volume.
iniUUID=ca230c16-b5a4-44e6-b5a8-930bb1f33fcf swap swap sw 0 0
You have enabled automatic mounting of swap memory on your server. The server automatically mounts and enables the swap memory at boot time.
Configure Swappiness
A Swappiness value controls how the system switches between the main memory (RAM) and swap memory on your server based on the usage percentage. A low value minimizes swapping to disk while a higher value enables the server to use swap memory based on the following ratings:
0
: Enables the system to avoid swapping processes out of physical memory.1-49
: Enables swapping with reduced usage unless the main memory (RAM) is at capacity.50
: Balances swapping and memory caching on the server.51-99
: Increases swapping from the main memory to swap memory.100
: Prioritizes swapping on the server and processes are continuously moved from RAM to swap.
Follow the steps below to configure the swappiness value on your server.
Run the following command to modify the
/etc/sysctl.conf
with your swappiness value. Replace50
with your desired swapping level.console$ echo "vm.swappiness = 50" | sudo tee -a /etc/sysctl.conf
Output:
vm.swappiness = 50
Reload the
systctl
configuration to apply the configuration changes.console$ sudo sysctl -p
Test the Swap Memory
View all available swap volumes on your server.
console$ sudo swapon -s
Output:
Filename Type Size Used Priority /swapfile file 2457596 268 -2 /swapfile.img file 2097148 0 -3 /dev/vdb1 partition 41940988 0 -4
Run the following command to activate all inactive swap volumes in your
/etc/fstab
configuration.console$ sudo swapon -a
View your server memory and verify the amount of swap memory actively in use.
console$ sudo free -h
Output:
total used free shared buff/cache available Mem: 955Mi 348Mi 155Mi 1.2Mi 613Mi 607Mi Swap: 44Gi 268Ki 44Gi
Remove Swap Memory
Use the
swapoff
utility with the Swapfile or partition path to disable on your server. For example, run the following command to disable the default Swapfile/swapfile
.console$ sudo swapoff /swapfile
View the swap volumes on your server and verify that the default Swapfile is disabled.
console$ sudo swapon -s
Output:
Filename Type Size Used Priority /swapfile.img file 2097148 0 -2 /dev/vdb1 partition 41940988 0 -3
Conclusion
You have added swap memory on an Ubuntu 24.04 server and extended the default system memory to enable swapping. Swap memory may be slower as compared to RAM but improves your server performance by offloading resource-intensive processes from the main memory. For more configuration options, run $ man swapon
to view the swap commands manual.