Expand existing Swap file size on Debian or Ubuntu VPS (AWS Lightsail)
A tutorial explaining how to expand the size of an existing Swap file in Debian or Ubuntu.
This tutorial will teach you how to expand an existing Swap File on an Ubuntu or Debian VPS. This has been specifically tested on a low-spec AWS Lightsail VPS although this method should work with any generic VPS running Debian or Ubuntu with enough storage space available.
Expanding your Swap file can help relieve pressure on your low-spec VPS’ RAM, which on some hosting providers can cause your system to crash or hang (such as AWS Lightsail’s “burst capacity” being exceeded).
What is a Swap file?
Swap files are a type of virtual memory in Debian & Ubuntu which allow you to offload RAM usage into a flat file on the disk. This frees up physical RAM space which is particularly useful for low-spec Virtual Private Servers.
Tutorial:
Step 1.)
Validate that we have an existing Swap file. Run the following command:
sudo swapon -show
Expected Result (Size, Used, Priority may vary):
Filename Type Size Used Priority
/swapfile file 2097148 169728 -2
Step 2.)
Disable the current Swap file so that we can edit it. Run the following command:
sudo swapoff -a
Step 3.)
Allocate the amount of disk space you desire for the Swap file:
Replace X with the amount of GB you want to allocate
sudo fallocate -l XG /swapfile
NOTE: If you are receiving the following error when trying to expand or configure a Swap file then it likely means you already have a Swap file enabled:
fallocate: /swapfile: fallocate failed: Text file busy
REFER TO STEP 2 IF YOU ARE SEEING THE ABOVE!
Step 4.)
You will need to ensure the Swap file has the correct permissions. You can do this with the CHMOD command:
sudo chmod 600 /swapfile
Step 5.)
Re-enable the Swap file with the following command:
sudo swapon /swapfile
Step 6.)
Verify that the changes to the Swap file have taken effect by re-running the same command in Step 1:
sudo swapon -show
That’s it! You have expanded the size of the Swap file on your Ubuntu or Debian VPS. This will relieve your RAM when it becomes overloaded and prevent burst capacity exceeding it’s limit on AWS Lightsail.