How To Add Swap Space on AWS Lightsail Server Instance

In this video tutorial I will walk through the steps to add swap file to your server instance.

What is a swap file?

A swap file is a mechanism to increases your servers available memory and can safe-guard your application from crashing. For example If you have an AWS Lightsail instance with 1GB memory and you’re running services that uses up all of that memory, then your application may run into out-of-memory exceptions and can crash since there is no more memory to use.

Having a swap file allows the system to treat this file on your disk as an extended memory and will start to move older data from the real memory to the swap file, freeing up real memory for newer data.

Recently the Lightsail Blueprint by Bitnami started configuring swap space to their configuration by default but older instances did not do that to my knowledge. In this video I’ll show you the steps to check if you have a swap file, and if you don’t have it, I’ll show you how to configure swap space for your instance.

Steps to add Swap Space to your WordPress Instance

  1. Check if your server is configured with Swap File. Run the following command:
sudo swapon --show

If nothing returns back then your system does not have a Swap Space configured. You can verify it again with the following command:

free -h

The results should show there is 0 Swap space configured.

  1. Choose the right size for the Swap File.

Use this chart as a guideline on how much Swap space you should create. Keep in mind this is for the Ubuntu OS. You should Google for the right size based on recommendations for your OS if different. Some OS’s recommend to double the RAM size for your Swap Files size.

RAM SizeSwap Size (Without Hibernation) Swap size (With Hibernation)
 256MB 256MB 512MB
 512MB 512MB 1GB
 1GB 1GB 2GB
 2GB 1GB 3GB
 3GB 2GB 5GB
 4GB 2GB 6GB
 6GB 2GB 8GB
 8GB 3GB 11GB
 12GB 3GB 15GB
 16GB 4GB 20GB
 24GB 5GB 29GB
 32GB 6GB 38GB
 64GB 8GB 72GB
 128GB 11GB 139GB
From <>
  1. Run the following command to create the Swap File:
sudo fallocate -l 1G /swapfile
  1. Verify the file was created with correct size
ls -lh /swapfile
  1. Change permissions to make file only accessible by root
sudo chmod 600 /swapfile
  1. Verify permissions
ls -lh /swapfile
  1. Mark the file as Swap
sudo mkswap /swapfile
  1. Enable the Swapfile
sudo swapon /swapfile
  1. Check if the swap is active
sudo free -h
  1. Make the swap file permanent
    1. Backup the /etc/fstab prior to editing.
    2. Open the fstab file in your favorite editor (vi used here)
    3. Add the following line to the end of the file, save and exit
sudo cp /etc/fstab /etc/fstab.bak
sudo vi /etc/fstab
##add this following line
/swapfile swap swap defaults 0 0
  1. Verify the swap file is active
sudo swapon --show or free -h
  1. Optimize the swap file, run the following commands for production servers
cat /proc/sys/vm/swappiness
# this should return a value of 60, change the value to 10
sudo sysctl vm.swappiness=10
# or in some case you may have to run this command instead
sudo sysctl -w vm.swappiness=10
  1. Persist the change, open systcl.conf file in your favorite editor (vi used here)
sudo vi /etc/sysctl.conf
  1. Add the following line to the end, save and exit

These steps to enable swap file on your system should help you resolve out-of-memory issues and errors. However if you continue to receive them, then you may need to consider upgrading your instance to a more appropriate size for your applications to run smoothly.


All videos tutorials on the website as well as the YouTube channel aim to provide a simplified process for a specific scenario; there could be many different factors and unique use cases you may have. The tutorials may not cover every situation; so treat is as a starting point or learning concept to apply to your unique situations, and consider this inspiration but not prescription or explicit direction.

Scroll to Top