Set Swap in Linux
Without swap, when your system's physical memory (RAM) is fully utilized, it can lead to performance issues and potentially cause processes to be killed due to lack of memory.
To address this issue, you can follow these steps to create and enable swap on your Ubuntu system:
Create a Swap File: You can create a swap file to use as virtual memory. Choose an appropriate size for the swap file based on your system's memory requirements. For example, to create a 4GB swap file:
sudo fallocate -l 4G /swapfile
Set Permissions on the Swap File: Adjust the permissions of the swap file to only allow root to read and write to it:
sudo chmod 600 /swapfile
Set Up Swap Area: Set up the swap area using the mkswap command:
sudo mkswap /swapfile
Enable Swap: Enable the swap file:
sudo swapon /swapfile
Make Swap Permanent: To make the swap file permanent across reboots, add an entry for it in the /etc/fstab file:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Verify Swap: Check that swap is now enabled and being used:
free -h
You should see the swap space listed along with its size and usage.
By following these steps, you can create and enable swap on your Ubuntu system, which will help improve performance and prevent memory exhaustion issues. Adjust the size of the swap file according to your system's requirements and available disk space.













