SWAP is a partition or a disk space that can take some load for RAM and can be used as a memory space on Ubuntu systems. Normally, while installing a new Linux-based operating system, it will offer you to create a new SWAP partition. The file partition has a SWAP flag, and usually, the size of a SWAP space is 2GB on average. When the system memory is full, the Linux kernel automatically moves some unused files into the SWAP area instead of stopping them. As we know, in Linux, everything is either a file, SWAP can handle files pretty well. So, if you’re using a Ubuntu system on a virtual machine, you definitely should try adding SWAP space to your machine.
Swap Space in Ubuntu Linux
You may ask, how much storage should I use for the SWAP space? As SWAP can take the extra RAM load, can I allocate as much SWAP as I want? Well, here is the standard formula. If you have more than 2GB of physical RAM on your machine, SWAP is recommended for you.
You can also use SWP if you have a higher amount of Physical Memory. The amount of SWAP you should use is kind of dependent on how much actual RAM you have. Please follow the table given below to understand the RAM-SWAP ratio. In this post, we will see how to add a SWAP space in the Ubuntu system.
RAM Size | Swap Size (Without Hibernation) | Â Swap size (With Hibernation) |
---|---|---|
 256MB |  256MB |  512MB |
 512MB |  512MB |  1GB |
 1GB |  1GB |  2GB |
 2GB |  1GB |  3GB |
 3GB |  2GB |  5GB |
 4GB |  2GB |  6GB |
 6GB |  2GB |  8GB |
 8GB |  3GB |  11GB |
 12GB |  3GB |  15GB |
 16GB |  4GB |  20GB |
 24GB |  5GB |  29GB |
 32GB |  6GB |  38GB |
 64GB |  8GB |  72GB |
 128GB |  11GB |  139GB |
Step 1: Check the Current System Swap Space on Ubuntu
Before you start creating a new SWAP space on your Ubuntu and Debian-based system, please ensure no existing SWAP space on the system. You can any of the following commands given below to check your SWAP space on your Ubuntu machine.
sudo swapon -s sudo swapon --show
In the terminal shell, you can see that the SWAP is mentioned as a ‘File Type’ because, in Linux, everything is a file.
Step 2: Create Swap File on Ubuntu
We will use the allocate command to assign a specific space as the SWAP file in this step. In the below-mentioned command, the allocate command assigns a 4 Gigabyte of space as the SWAP space. When the command finishes creating the file, please run the next change mode command to allow the SWAP file to read and write permission.
sudo fallocate -l 4G /swapfile chmod 600 /swapfile
If you have any issues using the allocate command, please try the following command to make the SWAP space.
sudo dd if=/dev/zero of=/swapfile bs=4096 count=1048576
Now, make the SWAP file executable and enable it on your Ubuntu system by running the following commands on your terminal shell with root access.
sudo mkswap /swapfile sudo swapon /swapfile
Step 3: Configure the Script and Allocate the Swap
After creating the SWAP space on your Ubuntu system, you can edit the configuration to set the SWAP parameters. For example, use the following command with root access, edit the SWAP partition, and see the UUID.
sudo nano /etc/fstab
Now, when the configuration script opens, add the following line inside the script, save it and exit the file. The line indicates the mount point, file type.
/swapfile none swap sw 0 0
Now, run the following command to edit the kernel level script for enabling the SWAP space on your machine.
sudo nano /etc/sysctl.conf
When the script opens, add the following line inside the script, then save and exit it. You can change the value according to your urgency.
vm.swappiness=10
Now run the following system control command to reload the SWAP space and see the impact on your machine.
sudo sysctl -p
You can now run the swapon command given below to check if the SWAP space is activated or not on your Ubuntu system.
sudo swapon --show
Step 4: Resize SWAP in Ubuntu
If you ever need to add more space inside your SWAP disk, you can just do it by adding more disk space into that. To resize the SWAP space on a Ubuntu machine, you need to turn off the SWAP space and then re-allocate the desired space. You can run the following command lines on the shell with root access to make that happen.
sudo swapoff /swapfile sudo fallocate -l 2G /swapfile
When the resizing finishes, mark the space as SWAP and enable the file on your system. Run the following commands to mark and enable the SWAP space.
sudo mkswap /swapfile sudo swapon /swapfile
Step 5: Remove SWAP on Ubuntu Linux
Sometimes, when you have a multi-boot computer, you might need to allocate the SWAP space on your system for each operating system. The following process will allow you to empty the SWAP space on your Ubuntu machine. First, run the following nano command to edit the swap2ram script to edit it. When it opens, add the script given below, then save and exit the file.
sudo nano /usr/local/sbin/swap2ram.sh
Then, add the following lines to the script, then save and exit the file to deactivate the SWAP file.
#!/bin/sh mem=$(LC_ALL=C free | awk '/Mem:/ {print $4}') swap=$(LC_ALL=C free | awk '/Swap:/ {print $3}') if [ $mem -lt $swap ]; then echo "ERROR: not enough RAM to write swap back, nothing done" >&2 exit 1 fi swapoff -a && swapon -a
Please run the change mode command to execute the files if you face any issues editing the script and run the commands.
sudo chmod +x /usr/local/sbin/swap2ram.sh sudo /usr/local/sbin/swap2ram.sh
You can also turn off the SWAP and remove it by running the following rm command on your terminal shell.
sudo swapoff -v /swapfile sudo rm /swapfile
Issues You Might Face
If you are using old hardware or an operating system, there are chances that you may face a few issues. For example, you might need to change the swappiness value to let your Ubuntu kernel know how often you want the machine to use the SWAP space after creating the SWAP space.
If you can’t change the swappiness value on your Ubuntu system, please try the following command line to add your custom line inside the script. Here, 60 is the memory paging (swappiness) value, and you can change it according to your configuration.
cd /etc/sysctl.d/ sudo touch 60-my-swappiness.conf
Then run the SWAP configuration script on your notepad/editor to change the vm.swappiness = 60
 value.
sudo nano 60-my-swappiness.conf
Final Words
No doubt, SWAP can speed up your system, and swapping in Linux can really save your computer from crashing due to a lack of RAM space. But before creating a SWAP space on your system, ensure that you really need the SWAP space.
And it is recommended to add the swap space through the installation wizard during the OS installation. In the entire post, I’ve described the notion of SWAP, how many swaps you might need, and the process of adding a SWAP space on a Ubuntu system.
If you find this post useful and technical, please share it with your friends and the Linux community. You can also write down your opinions regarding this post in the comment section.