How to Add a Swap Space on Ubuntu 24.04
Memory overloading is a common issue that system users encounter frequently. When a system RAM is overloaded, it can significantly slow the work progress and harm the system’s stability. This is where the swap space comes to the rescue. It is a temporary virtual memory that provides extra room for your system and prevents your application from crashing. The system uses the swap space when there is no longer sufficient space left on your RAM for holding the application data. The data in swap is slower to access than RAM, but it helps prevent your system from crashing due to memory overload.
If you are using the Ubuntu 24.04 system and are interested in adding a swap space, follow this guide, here, you will learn:
How to Add a Swap Space on Ubuntu 24.04
To add a swap space on Ubuntu 24.04, follow the below-given steps:
Step 1: Check the Swap Information
Before beginning the process of adding a swap space, first ensure there is no swap space available on the system. You can have multiple swap files on the system, but one swap file is generally enough. To check for the swap space information, run the below-provided command:
sudo swapon --show
The blank output indicates that there is no swap space available on your system.
You can also verify the swap space availability on the system by running the below-given command:
free -h
The swap with 0B indicates the system doesn’t have swap space.
Step 2: Check Available Space
It is also crucial to check for available disk space on the system before you add swap space to the system. Ensure your disk has enough space to add swap space, this can be done using:
df -h
The /dev/sda file system mounted on / will be the hard drive of the system.
Step 3: Creating a Swap File
Once you find your available disk space, it’s now time to create a swap file based on your system needs. Generally, a swap space can be created based on the RAM of your system. You can keep the swap space value equal to your RAM size or double it accordingly.
Here, I am creating a 1.5GB swap space for a 2GB RAM server as an example using the fallocate command:
sudo fallocate -l 1.5G /swap_file_name.img
Note: Change the value of swap space and swap_file_name according to your choice.
Step 4: Verify Swap File is Created
Once you created the swap file, you can verify it by using the following command:
ls -lh /swap_file_name.img
Step 5: Enable the Swap File on Ubuntu
Now, you have to turn the swap file you created into the swap space so that your system will detect it. But before performing the further operation, lock down the permission of the swap file so that only the root user will be able to read the content. You can set the specific permission for the swap file on Ubuntu by using:
sudo chmod 600 /swap.img
After setting the permission, you can then mark the swap file as a swap space on Ubuntu by using the following command:
sudo mkswap /swap.img
Once done, you can enable the swap space and allow your system to use it by running the following command:
sudo swapon /swap.img
Step 6: Make a Permanent Swap Space
The above changes you made to enable the swap space are only restricted to the current sessions. The changes will be lost if you reboot the system, however, you can make the changes permanent by adding the swap file to the fstab file. You can open the fstab file on Ubuntu by using the below-given command:
sudo nano /etc/fstab
Once the file is opened, simply add the following line and save the file using CTRL+X, add Y and press Enter.
/swap.img none swap sw 0 0
Step 7: Verify Swap Space Information
You can now check the swap information on Ubuntu by using the following command:
Alternatively, you can also verify swap space information through:
free -h
Step 7: Adjust Swappiness Value
The swappiness parameter controls how often your system uses the swap file (disk space) for memory management. The value of the swappiness parameter is set between 0 and 100. If a value close to 0 is set, the disk is avoided unless there is a need for it. If the value is closer to 100, the programs are swapped to disk instantly. The default value for swappiness is set to 60 on the Ubuntu system, and you can verify it by using the following command:
cat /proc/sys/vm/swappiness
If you are using an Ubuntu desktop, the swappiness value close to 60 is not a bad choice. However, for a server, you can change the value and move it close to zero by using the following command:
sudo sysctl vm.swappiness=20
The value you set for swappiness is restricted to the current session only when using the sysctl command. The changes will be lost when you reboot the system. However, you can permanently set the swappinness value by opening the sysctl.conf file on Ubuntu by using:
sudo nano /etc/sysctl.conf
Then add the swappiness value at the end of the file and save it:
vm.swappiness=20
Step 8: Adjust Cache Pressure Value
The cache pressure is a factor that determines how quickly your system Kernel releases cached memory when needed by other processes running on your system. The default value for the cache pressure is 100, and you can set the values between 50-200.
To check for the cache pressure value on Ubuntu, use the following command:
cat /proc/sys/vm/vfs_cache_pressure
To change the cache pressure value, simply use the following sysctl command:
sudo sysctl vm.vfs_cache_pressure=120
Note: To permanently set the cache pressure value on the system, add the value inside the systl.conf file and save it.
How to Remove Swap Space on Ubuntu 24.04
To remove swap space on Ubuntu 24.04, first deactivate it using the following command:
sudo swapoff -v /swap.img
Then remove the swap file entry line from the file /etc/fstab.
Once done, you can remove the swap file from the system by using:
sudo rm /swap.img
Conclusion
Adding a swap space is a crucial task that prevents the system from crashing due to memory overload. You can add swap space on Ubuntu 24.04 by first ensuring whether there is a swap space available. After that, check for available space and create a swap file using fallocate command. Then set permission and mark the swap file using chmod and mkswap commands, respectively. Once done, you can then enable the swap space using the swapon command followed by the swap file name. You can also set the swap space permanently by adding the line ”/swap.img none swap sw 0 0” inside the /etc/fstab file; the complete step-by-step instructions can be followed in the above section of this article.

















