Most virtualized environments correctly eliminate swap in their guest environments, but this leads to crashes in Amazon EC2 or Digital Ocean "cloud" hosting where the hosting provider places hard limits on memory. Such crashes often are intended to force the customer to upgrade to larger (and more expensive) servers or to otherwise implement high availability for web applications.
(1) Connect to the virtual guest using SSH and obtain root access (e.g. sudo su -).
(2) Create a loopback device (a file that we will mount as a disk to create our own local "swap" space).
(3) Format the loop file using mkswap.
(4) Mount the loop file to the file system as (/mnt/swap)
(5) Turn on swap (swapon /mnt/swap).
(6) Edit /etc/fstab to remount this swap file at boot up.
(1) Where possible (e.g. Amazon EC2) use a separate EBS volume for swap to avoid slowing down the system. This will also allow scalability.
(2) Remember the rule: allocate 1.5 to 2 times the amount of RAM for your swap space.
Need help implementing this strategy, email the author at