Linux Tutorials

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


Alex August 3, 2019 at 11:11 am

Really detailed post and it helped me today. Highly recommended and simple step by step process.

Mohd Sohail August 3, 2019 at 11:46 pm

Thanks Alex! Comments like this encourage us to write more quality stuff.

Jason October 5, 2019 at 7:06 pm

Why post images instead of copy-able commands?
Really helpful article, but a bit irritating.

Nithin January 13, 2020 at 7:53 pm

bash: /var/swapfile: Permission denied

Sohail January 14, 2020 at 3:33 am

Run it with sudo or as root user.

Nekawa June 9, 2020 at 3:34 am

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)

candour March 24, 2022 at 3:54 pm

here’s what i got please help

swapon: /var/swapfile: swapon failed: Invalid argument

on fedora linux

Sohail March 24, 2022 at 9:48 pm

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 Comment

* By using this form you agree with the storage and handling of your data by this website.