How to add swap space on Ubuntu 18.04

March 09, 2019 Post tags: how | ubuntu | linux | swap |

What is SWAP?

Swap space can take the form of a disk partition or a file. Users may create a swap space during installation or at any later time as desired. Swap space can be used for two purposes, to extend the virtual memory beyond the installed physical memory (RAM), a.k.a "enable swap", and also for suspend-to-disk support.

If it is beneficial to enable swap depends on the amount of installed physical memory, and the amount of memory required to run all the desired programs. If the amount of physical memory is less than the required amount, then it is beneficial to enable swap. This avoids out of memory conditions, where the Linux kernel OOM killer mechanism will automatically attempt to free up memory by killing processes. To increase the amount of virtual memory to the required amount, add the necessary difference as swap space. For example, if your programs require 7.5 GB of memory to run, and there are 4 GB of physical memory installed, add the difference of 3.5 GB in swap space. Add more swap space to account for future requirements. It is a matter of personal preference if you prefer programs to be killed over enabling swap. The biggest drawback to enabling swap is its lower performance.


You need to have an Ubuntu 18.04 server with a non-root sudo-enabled user account.

Don't have a server? Rent one for cheap on services: AWS, DigitalOcean, GoogleCloud

If you need help you can contact us.

Setup SWAP

Checking the system for swap information:

free -h
sudo swapon --show

Checking available space on the hard drive partition:

df -h

Creating a swap file:

sudo fallocate -l 1G /swapfile
sudo chmod 600 /swapfile

Check swap permissions:

ls -lh /swapfile

Enabling the swap file:

sudo mkswap /swapfile
sudo swapon /swapfile
sudo cp /etc/fstab /etc/fstab.bak
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Tuning your Swap Settings:

sudo nano /etc/sysctl.conf

At bottom of the file add these two lines: vm.swappiness=10 and vm.vfs_cache_pressure=50

Product - ORI PWA

Speed up your business with PWA application. PWA can be your website and mobile application for both Android and iOS.

We have taken special care to include everything you need. Reduce your headaches both during production and regular maintenance.

Our PWA can pull data from anywhere, custom RESTFUL api, Word Press, Magento, Drupal, Contentful, XML, JSON, spreadsheets and more. We can organize, display and process pulled data per your liking.

Do you want a hassle-free, modern and super fast website?

Find out more


ORI provides full business web identity creation service! Contact us