How To Create Or Increase Swap Space In Linux

The operating system makes use of swap space when its available physical memory (RAM) is running out due to ever-demanding applications. In this situation, the operating system moves the inactive pages in physical memory to swap space.  

This freeing up of physical memory will be used for other applications. When the physical memory is available enough, the swap memory area will be brought back to the physical memory.  The administrators ensure that sufficient swap space present in the system so that some free physical memory always available to the operating system. This article provides steps to create or increase swap space and also delete if you need it.

​Do I really need swap space?

Not always, the provided system has a large amount of physical memory (RAM).  But it is recommended to have swap space handy.  The system may crash when the system is run out of physical memory when many applications are running with large memory footprint.  When compared to RAM, disk space is relatively cheap!  

Partition or file?

Swap space can be a dedicated swap partition (recommended), a swap file, or a combination of both. By default, most of the Linux distributions create a dedicated swap partition or a file on the system partition during installation. Windows operating system generally has the swap space as a file.  

What is the recommended swap size?

Though there is no hard and fast rule to have swap space, it is recommended to have at least 1.5 times the physical memory.  In the case of hibernation, the swap partition should be at least as big as the RAM size.  

Creating swap space

Following are the instructions to create swap space using a file:

  • Login as root.
sudo su
get superuser ubuntu linux
  • Create swap file in directory “/var” with name “swapfile”.  At the shell, create the file and set root permissions as follows:
cd /var
touch swapfile
chmod 600 swapfile
ls -la swapfile
create swap file
  • Use “dd” command to fill the swap file with 1 GB size (as an example) as follows :
dd if=/dev/zero of=/var/swapfile bs=1024k count=1000
create swap file with data
  • Now setup the swap file:
mkswap /var/swapfile
  • Enable the swap file:
swapon /var/swapfile
enable swap file
  • To check whether the new swap file was successfully created, either of the below commands can be used.​
# cat /proc/swaps
# swapon –show 
get all swap files
  • Add below line to the “/etc/fstab” file so that next time when the system boots, it enables the newly created swap file:
/var/swapfile none swap sw 0 0

Disable and remove a swap file

Disable the swap file.

# swapoff /var/swapfile 

Delete the swap file.

# rm /var/swapfile 

Remove the entry from “/etc/fstab” file.

/var/swapfile none swap sw 0 0   


The swapping mechanism does have a downside.  Because the swap space resides in hard disks, the access time for swap is slower and hence it cannot be a complete replacement for the physical memory.  


System administrators can benefit greatly by adding sufficient swap space to keep the system running smoothly.  Regular monitoring of system memory usage helps in determining the size of the swap space.

Credit to – Ramakrishna Rujure


Mohd Sohail is a web developer and a Linux sysAdmin. He also loves to write how-to articles, applications reviews and loves to use new Linux distributions.

Articles: 853


    • Nithin, you probably got that when you just entered the last config that “enables the newly created swap file”ou
      You can’t simply type “/var/swapfile none swap sw 0 0” into the console. If you do you get “bash: /var/swapfile: Permission denied”

      What you need to do is:
      (either already running as root or type “sudo” first)
      “nano /etc/fstab”
      navigate to bottom with arrows
      then you add
      “/var/swapfile none swap sw 0 0” into the end of that file (without the quotes)

      Then save and exit (Ctrl+O, Ctrl+X)
      Then Reboot system

      (SIDE NOTE: It’s advised to give swap about 2 times your current ram, so in my case i had 6gb ram so here i put “dd if=/dev/zero of=/var/swapfile bs=12280k count=1000” instead of 1024k)

    • Please remove colon from swapon.

      Edit: I found that the error occurs when the filesystem does not support swap. To fix the error, you should change to the ext filesystem.

Leave a Reply

Your email address will not be published. Required fields are marked *