If you think the existing swap space on your server or desktop system is not enough, add the required one in Ubuntu 24.04 Noble or 22.04 LTS Jammy JellyFish Linux. Users can also follow this tutorial for Ubuntu 20.04 LTS.
Linux (like almost all other operating systems) tries to keep the operating system components, including required program data, in RAM (main memory) since access to RAM is much faster than to data carriers such as hard disk, CD, or USB stick.
However, RAM has its limit of volatile space, if your system has 4GB of memory then it can happen many times your RAM space is allocated entirely when we run many programs simultaneously. At this point, the Linux kernel frees up RAM by writing parts of the data stored in RAM to the hard disk; the portion of the hard disk is called “Swap” memory. If the data is needed again, it is loaded back into RAM, and other data not required at the moment is written to the swap. In such a case, “the system swaps” is also said.
If a system uses the swap intensively, the system becomes significantly slower and feels “tough” due to the frequent disk accesses. However, if you do not have a swap and the RAM is running total, the Linux kernel terminates programs at its discretion to free RAM. This usually results in data loss. Ubuntu (like all other distributions) creates a swap area by default during installation to prevent precisely this. Newer Ubuntu versions use a swap file; in older Ubuntu versions, a swap partition is used instead of the swap file. This usually has no disadvantage, but the handling differs in some details.
Steps to add Swap area in Ubuntu 24.04 or 22.04 LTS Linux
The appropriate memory size depends on several factors and cannot be generally specified. Depending on the system usage, about 1 GB of swap memory or 1 GB + RAM capacity for suspend-to-disk is sufficient. If the swap memory (and RAM) runs out, the system may crash, and all data that is not synchronized with the hard disk may be lost, which is why a generous swap area may be helpful. For example, if your system has 4GB of RAM, then 8GB of the Swap file is good to go, and if your hard disk has spare space, allocate that small amount.
1. Check the current Swap Space
Before increasing the Swap space on your Ubuntu 24.04 or 22.04, check the Swap file’s current size on your system. This lets you know how much space you need to add and whether the Swap has been enabled in the system. For that, use the given command:
sudo swapon -s
The Size column will show the amount of SWAP allocated on your system. For example, here we have 2GB. It could be /swapfile or /swap.img.
![Check current Swap Space Check current Swap Space](https://www.how2shout.com/linux/wp-content/uploads/2022/06/Check-current-Swap-Space.png)
2. Turn off Swap
To increase the size of the Swap file, first, disable its usage on your Ubuntu 24.04 / 22.04. Otherwise, the system will not let us change its size. Moreover, even if you try to do so, an error will generate saying “fallocate: fallocate failed: Text file busy in Ubuntu 24.04 / 22.04?“
sudo swapoff -a
Please wait for some time; the system will terminate the process or move the data from Swap to RAM before disabling its usage.
3. Create a Swap file on Ubuntu 24.04 or 22.04 to increase the size
Next, create a new Swap file with the space you want to allocate using the given command. While using the given command keep one thing in mind that- 8G means – 8GB of space you are about to allocate for Swap. Hence, you can increase or decrease as per your requirements.
sudo fallocate -l 8G /swapfile
4. Change file permission
For security reasons, change the file permission and give complete control only to the root user.
sudo chmod 600 /swapfile
5. Mark the SWAP space and activate it
Make the created file format readable as Swap by the system and activate the same to use by our Ubuntu 22.04 or 24.04 or any other version you use.
sudo mkswap /swapfile
sudo swapon /swapfile
![Create a Swap file on Ubuntu 22.04 Create a Swap file on Ubuntu 22.04](https://www.how2shout.com/linux/wp-content/uploads/2022/06/Create-a-Swap-file-on-Ubuntu-22.04.png)
After activating, you can check whether the Swap space is added to your system or not:
sudo swapon -s
or
sudo swapon --show
or
free -h
![Mark SWAP space and activate it Mark SWAP space and activate it](https://www.how2shout.com/linux/wp-content/uploads/2022/06/Mark-SWAP-space-and-activate-it.png)
6. Set the Ubuntu 22.04 or 24.04 SWAP file as permanent
If you have followed the steps above, you would have added a Swap memory to your system already. And your system will also start using it immediately. However, as you restart, it will be deactivated. To avoid that, make the added Swap Space file permanently on your Ubuntu system, edit /etc/fstab
the file, and add the information about your Swap file. Here is the command to follow for that:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
7. Set swap usage or swappiness
You can specify when the kernel moves data from memory to the swap partition or file. The Swappiness is defined with a value from 0 to 100, whereby 0 is only swapped out if there is no other way (memory full), and with 100, the memory is hardly used. The default value is 60, so the kernel is more prone to swapping. No value can be generally recommended, as it depends on the respective system and user behavior.
If the Swappiness has been set very low and many programs run in parallel, it can quickly happen that the memory is almost full. Exactly then, a program is started that puts a hefty load on the main memory. Now, the memory is exhausted, and the system has to load the already cumbersome program and outsource it. The result is a thoroughly utilized system.
The following command can be used to find out the current value of the Swappiness:
sysctl vm.swappiness
The result could look like this:
vm.swappiness = 60
To change the swappiness immediately, e.g., lower it to 25, use this command:
sudo sysctl vm.swappiness=25
However, this change is forgotten by the system after a reboot unless the following is entered in the system file /etc/sysctl.conf
:
sudo nano /etc/sysctl.conf
Add the following line at the end of the file. Change the value 25 to whatever you would like to set for Swap.
vm.swappiness=25
Save the file using Ctrl+O, hit the Enter key, and then exit the file editor using Ctrl+X.
Now reload the sysctl
configuration file
sudo sysctl -p
Other Articles:
- How to Install and Use Flatpak on Ubuntu 24.04 LTS Linux
- Get Microsoft Edge browser on Ubuntu 24.04 Linux via the command
- Customize Ubuntu 22.04 Jammy JellyFish Dock Panel
- List of Commands to get Linux system info using terminal
- Command to change the timezone in Ubuntu 20.04 server
- Krita FAQ: Open-source digital painting and drawing tool
Thanks a lot ! Very clear.
Thanks for the tutorial, awesome!
I am very grateful! Exactly the fix I have been searching for. Many thanks!
Thank you for this current how-to. Everything else I found was for Ubuntu 20.04 or earlier, and the swapfile method was changed in 22. The process worked beautifully.
I’d add that if you already have a /swapfile and are just changing the size, you can stop at step 5 as long as you create the new swapfile with the same name and location as the old one. Step 3 will just overwrite the old swapfile with the new one, and the system will know it’s permanent because the old one was. You do still need to run mkswap because the newly created swapfile will have a different UUID than the old one, but there’s no need to update fstab.