
In Ubuntu 24.04, swap memory plays an important role in keeping your system stable when physical RAM runs out. Swap is a dedicated portion of disk space that acts as virtual memory, allowing the system to offload less frequently used processes from RAM. This helps free up memory for tasks that need it most and prevents the system from crashing or slowing down under heavy load. By adding swap space, Ubuntu 24.04 can handle more demanding workloads and multitasking more efficiently—even on machines with limited RAM.
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. If you're using a different Linux distribution, check out our articles on how to add swap memory in Rocky Linux 9 and how to add swap memory on Debian 12 for detailed instructions tailored to those systems.
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.
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.
Verify the Swap memory available on your server similar to the output below.
Based on the above output, the server includes a 2GB Swap memory volume.
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 a new Swapfile with a specific size such as 2GB using the fallocate utility in your root directory /.
Modify the Swapfile permissions to allow only the root user to read and write changes on the file.
Format the file as swap using mkswap.
Verify the new swam memory size, label information and UUID similar to the following output.
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.
Output:
The Vultr Block Storage device is attached as vdb based on the above output with a 40GB size.
Initialize the volume using the GPT partition table.
Create a new partition using the entire block storage volume.
List the server storage devices again and verify that the new block storage partitiion is available.
Output:
Convert the new block storage partition to swap.
Output:
View your block devices information and verify that the new swap partition is available.
Output:
Note the swap partition UUID value to use when mounting the volume automatically at boot time.
Enable the Swapfile as swap memory using the swapon utility.
View the server swap memory and verify that the new Swapfile is active.
Output:
Run the following command to enable the Vultr Block Storage swap partition as swap memory. Replace /dev/vdb1 with your actual partition path.
View the server swap memory and verify that the swap partition is active.
Output:
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.
Open the fstab configuration file using a text editor such as nano.
Add the following Swapfile configuration at the end of the file.
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.
You have enabled automatic mounting of swap memory on your server. The server automatically mounts and enables the swap memory at boot time.
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. Replace 50 with your desired swapping level.
Output:
Reload the systctl configuration to apply the configuration changes.
View all available swap volumes on your server.
Output:
Run the following command to activate all inactive swap volumes in your /etc/fstab configuration.
View your server memory and verify the amount of swap memory actively in use.
Output:
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.
View the swap volumes on your server and verify that the default Swapfile is disabled.
Output:
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.
0 Comments
Be the first to comment and share your perspective with the community.