Here’s how you can create and add a swap file on Ubuntu Linux:
- Determine the size of the swap file: The recommended size for a swap file is equal to twice the size of your RAM. You can use the following command to determine the size of your RAM:
free -m
- Create a swap file: Use the
dd
command to create a new file with the desired size. Replace[swap_file_size]
with the size of the swap file you want to create, in MB.
sudo dd if=/dev/zero of=/swapfile bs=1024 count=[swap_file_size]
- Set the correct permissions for the swap file:
sudo chmod 600 /swapfile
- Mark the file as a swap file:
sudo mkswap /swapfile
- Enable the swap file:
sudo swapon /swapfile
- Make the swap file permanent: Add the following line to the
/etc/fstab
file to make the swap file permanent across reboots:
/swapfile none swap defaults 0 0
After completing these steps, your swap file should be up and running, and you should be able to see the new swap space when you run the free
command.