PROWAREtech
Swap File for Ubuntu 20.04, 22.04 and 24.04
Setup swap file on Ubuntu Linux 20.04/22.04/24.04.
Alternatively to setting up a swapfile, compress the memory of the Ubuntu Linux machine.
Ubuntu can be downloaded from here.
Check If Swap File Is Already Installed
Some installations may already have a swap file setup. Issue this command in the Terminal:
sudo free -h
The output should look something like this if one is not configured:
total used free shared buff/cache available Mem: 475Mi 155Mi 6.0Mi 0.0Ki 313Mi 298Mi Swap: 0B 0B 0B
Create a Swap File
Create a 4 GiB swap file in the root directory with fallocate:
sudo fallocate -l 4G /swapfile
Should fallocate not be available, use dd.
sudo dd if=/dev/zero of=/swapfile bs=1k count=2048k
Allow Only ROOT User Access to the Swap File
Only the root user should be accessing the newly created swap file. Issue command:
sudo chmod 600 /swapfile
Make the New File the Swap File
Use mkswap as follows:
sudo mkswap /swapfile
Activate the Swap File
Use swapon as follows:
sudo swapon /swapfile
Edit the /etc/fstab File
Use nano as follows to modify this file, adding one line:
sudo nano /etc/fstab
This file should have a line reading this:
/swapfile swap swap defaults 0 0
Verify the Installation
Use free to check the swapfile
sudo free -h
The output should look something like this:
total used free shared buff/cache available Mem: 475Mi 155Mi 6.0Mi 0.0Ki 313Mi 298Mi Swap: 4.0Gi 10Mi 4.0Gi
Comment