Published: 07 July, 2024

Expand existing Swap File size on Debian or Ubuntu AWS Lightsail VPS

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.

sudo swapon -show

Expected result (size, used, priority may be different!):

Filename                                Type            Size    Used    Priority
/swapfile                               file            2097148 169728  -2

Step 2.

Disable current Swap File in order to make edits.

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

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.

<< Back