Swap space resides as either a dedicated partition or a file. Its purpose is to store data that the RAM can no longer hold on to. Effectively, trying to improve your system performance to a certain degree.
Follow the instructions in this guide to create and initialize swap space in your Ubuntu 20.04 installation. And if you ever decide to stop using swap, the walkthrough will also cover how you can safely remove the created swap files.
- A terminal
- A user account with root privileges
Before getting started we should check the current status of our swap, to check if there is already a swap file or partition preset on our system.
sudo swapon -s
If the above command does not output anything, it means that there is no swap action going on in your system right now. If it does show an output, it means you already have swap space active on your system.
Note: It is possible to have more than one swap partition or file running at a time, but generally one should be enough.
Other than the above command, you can also use
free to check if swap is running. It will show your system's RAM usage as well.
free -h total used free shared buff/cache available Mem: 1.9Gi 918Mi 282Mi 2.0Mi 758Mi 876Mi Swap: 0B 0B 0B
Swap of the output will show us the current swap status of the system.
CREATE THE SWAP FILE
Lets go ahead and create a swap file named
swapfile1G on our root directory. It will have a size of
1G (1 Gigabyte)
sudo fallocate -l 1G /swapfile1G
Note: For using a size lower than 1GB, for example 512MB, use
512M instead of
1G. And also, you can name the file anything you want, naming it something meaningful is up to you.
ENABLE THE SWAP FILE
Okay, now that we have a file with the proper size ready, we can go ahead and turn it into swap space.
Before anything, we need to make some permission changes to
sudo chmod 600 /swapfile1G
Now only the root user has the read and write permissions to the swap file.
Then mark the file as swap space with the command,
sudo mkswap /swapfile1G
Enable the swap file by running,
sudo swapon /swapfile1G
We can use
swapon to verify that swap is enabled,
sudo swapon -s Filename Type Size Used Priority /swapfile1G file 1048572 0 -2
swapon we can also use the
free to verify that swap is indeed enabled.
free -h total used free shared buff/cache available Mem: 1.9Gi 918Mi 282Mi 2.0Mi 758Mi 876Mi Swap: 1.0Gi 0B 1.0Gi
Our swap space is all ready to be used by Ubuntu. Just one last step remains!
MAKE THE SWAP SPACE PERSISTENT
Ubuntu will forget about the swap file we just enabled if we were to reboot the system now. To avoid this, we have to add an entry to the
Run the following command to add the required line to the end of the
echo '/swapfile1G none swap sw 0 0' | sudo tee -a /etc/fstab
And that should be all, you now have usable swap space on your system.
To demonstrate how this is done, let's go ahead and remove the swap space we just created.
The first step would be to disable the swap with the
sudo swapoff /swapfile1G
Then delete the swap file since its no longer needed.
sudo rm /swapfile1G
And the final step would be to open
/etc/fstab with an editor of your choice and to delete the line related to the swap space we are removing. In this case, the contents of the line would be "
/swapfile1G none swap sw 0 0"