How to Add a Swap File On Ubuntu

This tutorial shows how to set up a swap file on Ubuntu Linux. The steps have been tested in recent Ubuntu versions incl Ubuntu 18.04 LTS (Bionic Beaver)

1.1-What Does Swapping Mean in Linux?

A swap file enables an operating system (OS) to utilize hard disk space to simulate additional memory. Effectively, as the system goes on the low-memory mode, it swaps a certain section of the RAM that an idle program may be using onto the hard disk to release memory capacity for other vital programs. Subsequently, when the user returns to the swapped out program, it swaps places with another program in RAM. This intelligent mix of RAM and swap files is commonly referred to as virtual memory, and its use allows the system to run a far greater number of programs than possible using only the built-in RAM.

Expressly in the context of Linux, the RAM on the Linux server comprises memory chunks technically termed as pages. To free up these RAM pages, a LINUX swap may be executed to copy the memory page from the parent RAM to a preconfigured space on the hard disk. Linux swaps of this nature allow a system to exploit more physical memory than was initially made available. Though a useful tool, swapping does suffer from a few demerits. Firstly, the activity triggers a sizeable amount of hard disk reading and writing, which invariably slows down your system to a considerable extent. Secondly, swap-thrashing may happen if the system gets inundated with a heavy load of files being swapped in and out frequently.

1.2-Important: Preliminary Check before setting up a Swapfile

Before the users proceed with the process of setting up a swap file, it is imperative to check for any existing swap files that may have been enabled on the VPS. This can be done by reviewing the summary of swap usage. The following command shall lead you to the required summary:

sudo swapon -s

The above command shall lead to the following results:

Filename Type Size Used Priority

If the above happens to be an empty list, you may rest assured that no swap files have been enabled.

1.3-Adding a Swap File on Ubuntu - A Step-By-Step Guide

Once the preliminary check has been done, the users are now all geared up for adding a Swap File on Ubuntu. All they need to do is follow the steps given below:

1.3.1: Step 1 - Checking the File System

Once it has been firmly established that there do not exist any active swap files on the virtual server, one must check the amount of space available on the server using the df command. The swap file would normally occupy 256MB of space, which would amount to merely 8% of the /dev/sda on a typical 32GB capacity server, one may assuredly proceed with the process The following set of commands shall help users complete the first step.


Filesystem 1K-blocks Used Available Use% Mounted on

/dev/sda 20907056 1437188 18421292 8% /
udev 121588 4 121584 1% /dev
tmpfs 49752 208 49544 1% /run
none 5120 0 5120 0% /run/lock
none 124372 0 124372 0% /run/shm

The above step shows the free space on your hard disk. Ensure that there is enough free space for your swap file. Next, we will create the actual file with dd as explained below.

1.3.2: Step 2 - Creating the Swap File

The second step comprises creating the Swap File with a size of 256 MB by using the dd command follows:

sudo dd if=/dev/zero of=/swapfile bs=1024 count=256k
of=/swapfile” designates the file’s name. In this case the name is swapfile.

Consequently, the swap file can be prepared by creating a designated Linux swap area using the following command:

sudo mkswap /swapfile

The above command shall display the following results:

Setting up swapspace version 1, size = 262140 KiB
no label, UUID=103c4545-5fc5-47f3-a8b3-dfbdb64fd7eb

Completion of Step 2 shall lead to the creation of the Swap File. The next step, as described below, shall lead to the activation of the Swap File thus created.

1.3.3: Step 3 - Enabling the Swap File

The process of creating the Swap File on Ubuntu needs to befinished by enabling the file so created. This can be done by using the following command:

sudo swapon /swapfile

Completion of Step 3 shall enable users to view the new swap file when under swap summary, as follows:

swapon -s
Filename Type Size Used Priority
/swapfile file 262140 0 -1
The file displayed as per details above shall exist on the virtual private server till the time the machine reboots. However, users enjoy the option of making the Swap File permanent by following the next step. 1.3.4: Step 4-Adding the Swap to the fstab file In order to lend permanence to the swap file you created, you must add the same to the fstab file. The following command that will help you open up the file remains the first in the process that shall help you do that:
sudo nano /etc/fstab
Subsequently, users need to paste in the following line:
 /swapfile       none    swap    sw      0       0

Post the above step, the Swappiness in the file must ideally be set to 10, by using the following set of commands:

echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf

Important: Doing away with the above important sub-step may invariably lead to poor system performance, while configuring the setting to 10 shall lead to the swap acting as an emergency buffer, thus averting irksome out-of-memory crashes.

1.3.4: Step 4 - Setting Up Appropriate Permission Levels on the Swap File

The last and final step in the process of creating a Swap File on Ubuntu remains setting up apt permission levels for the file. This is essential to prevent the file from becoming world-readable and lending appropriate security to the same. The same may be executed by using the following either of the following commands (depending upon the case)::

sudo chown root:root /swapfile
sudo chmod 0600 /swapfile

1.4 Test

Finally, we test that the swap file is active. Run the top command


the swap space should be shown in the column

KiB Swap: 262140
Share this page:

Suggested articles

4 Comment(s)

Add comment


By: Jens Huehne

It should probably be mentioned that a standard install of Ubuntu would already have created and activated a decently sized swap partition. A swap file is rather uncommon in Linux so this is useful only if you have installed without a swap partition and are loath to re-partition your drive.

By: iswinar

swap file is good if theres a memory limitation

By: Ernst Kloppenburg

This guide should really be updated to use fallocate instead of dd, because it is a lot fastere.g.:fallocate -l 256k /swapfile

By: John Deputy

Do remember, it is not possible to create a 'swapfile' on a BTRFS filesystem volume.

If you are doing this on a VM such as HyperV/Vmware, no need to shutdown the guest, just add a new drive with a fixed size for the swap drive, and then:

a) type "fdisk -l"this will give you the list of drives and the new drive should come up as /dev/sd*

b) type: "mkswap /dev/sd*"(the one you just added, sourced from the fdisk)

c) note the UUID, and simply edit the fstab file replacing the /swapfile part of the line with the UUID from step (b).

d) type "swapon -a"

e) type "top"you should see the swap size show the size of the drive you assigned.

Reboot the guest.

Do note that if you have multiple virtual scsi controllers, it may throw the drive mapping off, which means you will need to re-order the drives accordingly. Always a good idea to add a new drive to the 'last' virtual scsi-controller if you have more than one so that it keeps its order during virtual bios assignment.

Hope the above helps anyone else doing gods work in a virtual environment.