Create, modify and delete swapfile on Ubuntu

Create swapfile

# Set the size the swapfile (change 1G to the gigabyte size you want it to be)
sudo fallocate -l 1G /swapfile

# If fallocate command isn't available on your system, use this. Adjust the count to match your preference. The setting below will create 1GB swapfile
sudo dd if=/dev/zero of=/swapfile bs=1M count=1024

sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# verify that it's set correctly
free -h

Delete swapfile

# turn off all running swap
sudo swapoff -a

sudo rm /swapfile 

Modify swapfile’s size

If you want to increase or decrease the size of the swapfile, first you need to delete the old one, and re-create new one using the steps above

Leave a Reply

Your email address will not be published. Required fields are marked *