How to Add Swap Memory in Ubuntu 22.04

Updated on 29 April, 2025
How to Add Swap Memory in Ubuntu 22.04 header image

Swap memory is extra storage used when your server's RAM is full. It moves less active processes from RAM to a reserved space on disk, freeing up memory and preventing system crashes.

In this article, you are to add swap memory on Ubuntu 22.04. You are to use a block storage to increase the available server memory.

Prerequisites

Before you begin:

  • Have an Ubuntu 22.04 server.

  • Have an existing block storage.

  • Access the server using SSH as a non-root user with sudo privileges.

View Existing Swap Memory

Follow the steps in this section to view the already available swap memory on Ubuntu 22.04.

View the available server memory including RAM and Swap using the free utility.

console
$ sudo free -h

Verify the Swap memory available on your server similar to the output below.

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

Create Swap Memory

Follow the sections below to create swap memory using either a Swapfile or a dedicated block storage volume on your server.

Create Swap Memory using a Swapfile

  1. Create a new Swapfile with a specific size such as 2GB using the fallocate utility in your root directory /.

    console
    $ sudo fallocate -l 2G /swapfile.img
    
  2. Modify the Swapfile permissions to allow only the root user to read and write changes on the file.

    console
    $ sudo chmod 0600 /swapfile.img
    
  3. Format the file as swap using mkswap.

    console
    $ sudo mkswap /swapfile.img
    
  4. Verify the new swap 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 block storage

Vultr Block Storage is used here as an example to demonstrate the use of a dedicated block storage. You can replace the directories used in the commands to adjust according to any cloud provider.

  1. 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    40G  0 disk

    The Vultr Block Storage device is attached as vdb based on the above output with a 40GB size.

  2. Initialize the volume using the GPT partition table.

    console
    $ sudo parted -s /dev/vdb mklabel gpt
    
  3. Create a new partition using the entire block storage volume.

    console
    $ sudo parted -s /dev/vdb unit mib mkpart primary 0% 100%
    
  4. 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 
  5. Convert the new block storage partition to swap.

    console
    $ sudo mkswap /dev/vdb1
    
  6. Verify that the new swap partition is available.

    console
    $ sudo 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

  1. Enable the Swapfile as swap memory using the swapon utility.

    console
    $ sudo swapon /swapfile.img
    
  2. 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
  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
    
  4. 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) controls how file systems are mounted on a server. Follow the steps below to set up fstab so your swap memory volumes mount automatically at boot.

  1. Backup the original fstab configuration on your server.

    console
    $ sudo cp /etc/fstab /etc/fstab.bak
    
  2. Open the fstab configuration file using a text editor such as nano.

    console
    $ sudo nano /etc/fstab
    
  3. 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.

    • swap: Specifies the file system type as swap.
    • swap: Defines the mount point for the swap space.
    • 0: Disables automatic backups for the swap file system.
    • 0: Skips file system checks on the swap memory.
  4. View your block devices information and copy the UUID of new /dev/vdb1.

    console
    $ sudo 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"
  5. Add the block storage swap partition using its UUID value to enable automatic mounting on the volume.

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

    Replace UUID=ca230c16-b5a4-44e6-b5a8-930bb1f33fcf with your server block device UUID.

    After configuring automatic mounting, the server will now automatically mount and activate swap memory at boot.

Configure Swappiness

A swappiness value controls how the server manages memory usage between RAM and swap space. A lower value reduces swapping, while a higher value increases it based on the following ranges:

  • 0: Avoids swapping unless absolutely necessary.
  • 1-49: Swaps infrequently, only when RAM is nearly full.
  • 50: Balances RAM usage and swapping.
  • 51-99: Swaps more aggressively as RAM fills up.
  • 100: Prioritizes swapping, frequently moving processes from RAM to swap.

Follow the steps below to configure the swappiness value on your server.

  1. Run the following command to modify the /etc/sysctl.conf with your swappiness value. Replace 50 with your desired swapping level.

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

    Output:

    vm.swappiness = 50
  2. Reload the systctl configuration to apply the configuration changes.

    console
    $ sudo sysctl -p
    

Test the Swap Memory

  1. 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
  2. Run the following command to activate all inactive swap volumes in your /etc/fstab configuration.

    console
    $ sudo swapon -a
    
  3. 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

  1. 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
    
  2. 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

In this article, you added swap memory on an Ubuntu 22.04 server and enabled swapping by extending the default system memory. For more configuration options, run $ man swapon to view the swap commands manual.

Comments

No comments yet.