How to Add Swap Space on Ubuntu 25.04

This guide is applicable only for KVM-based VPSes and dedicated servers. If you wish to increase swap on your OpenVZ-based VPS, please contact us via a Support ticket from the client area.

Swap space, also known as "swap," is a reserved area on a server's storage device that the operating system can use as virtual memory when allocated memory is fully utilized. When your server's memory is exhausted due to running applications and processes, the operating system moves less frequently used data from memory to the swap space. This frees up memory for more active tasks and helps prevent the system from slowing down or crashing due to memory exhaustion.

Check Swap Information

To check the swap size:

swapon --show

Output:

root@server:~# swapon --show
NAME      TYPE SIZE USED PRIO
/swap.img file 2.2G   0B   -2

Or you can check using the free command:

free -m

Output:

root@server:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            3403         532        1659           4        1469        2871
Swap:           2229           0        2229

Note: If the output is empty, it means that swap space is not enabled yet on your system.

Add Swap Space

To create a swap file:

Note: This example adds 1GB. You can change 1G to your preferred size.

fallocate -l 1G /swapfile

or

dd if=/dev/zero of=/swapfile bs=1024 count=1048576

Set the correct permissions:

chmod 600 /swapfile

Set up a swap area on the file:

mkswap /swapfile

Output:

root@server:~# mkswap /swapfile
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=964c948f-9a19-4dad-aa31-845411ce2a67

Activate the swap file:

swapon /swapfile

Make the swap permanent by adding it to the /etc/fstab file:

nano /etc/fstab

Add this line at the end of the file:

/swapfile swap swap defaults 0 0

Check the new swap space:

swapon --show

or

free -m

Output:

root@server:~# swapon --show
NAME      TYPE  SIZE USED PRIO
/swap.img file  2.2G   0B   -2
/swapfile file 1024M   0B   -3

or

root@server:~# free -m
               total        used        free      shared  buff/cache   available
Mem:            3403         548         604           4        2522        2854
Swap:           3253           0        3253

Note: You can now see the added swap space.

Remove Swap Space

Deactivate the swap space:

swapoff -v /swapfile

Output:

root@server:~# swapoff -v /swapfile
swapoff /swapfile

Edit /etc/fstab to remove the swap entry:

nano /etc/fstab

Remove the line:

/swapfile swap swap defaults 0 0

Delete the swap file:

rm /swapfile

If you cannot configure swap or face technical difficulties, kindly reach out to us via a Support Ticket.


CrownCloud - Get a SSD powered KVM VPS at $4.5/month!
Use the code WELCOME for 10% off!

1 GB RAM / 25 GB SSD / 1 CPU Core / 1 TB Bandwidth per month

Available Locations: LAX | MIA | ATL | FRA | AMS