How to create or add a SWAP partition in Ubuntu and Linux Mint

The SWAP partition is an essential feature to get the best performance and stability, especially in a PC with smaller RAM. In a nutshell, SWAP space is like adding a RAM, but not a physical one, but allocating some amount of hard disk space as RAM – like a virtual RAM!

It makes sense to make a SWAP file for those who run resource-intensive applications like video convention, virtual machines, etc.. You don’t want RAM to overflow, and the system becomes non-responsive. It is possible that you initially assumed that you don’t need SWAP and completed the Ubuntu/Linux Mint installation, but now you figured out that you need SWAP too.

No worries, you don’t need to reinstall the entire operating system again to add the SWAP partition. You can add the SWAP anytime.

Creating a SWAP partition in Ubuntu, Linux Mint and derivatives

METHOD 1: Command-line Way from Terminal (Fastest way!)

STEP 1: First step is to check if by chance is there any SWAP partition already created in your PC. Use the see ‘swapon’ command:

sudo swapon --show

Enter your root password. If you see no output, it means that the SWAP doesn’t exist.

STEP 2: Next, let’s see the current partition structure of your computer’s hard disk. Use the ‘df’ command:

df -h

You should see the partition structure like the one I got in my test PC.

Terminal - Showing Current Partitions
Terminal – Showing Current Partitions

STEP 3: Now it’s time to create the SWAP file. Make sure you have enough hard disk space. It’s a matter of preference on how much SWAP size you need.

Typically, it is good to have twice as much as your RAM for the SWAP size. But you can even go ahead with exactly as much as your RAM – your choice.

In this example, I choose to go with twice that of RAM. I have my test PC with 2 GB memory. So, I will set 4 GB as SWAP. Run the following command. Use your number instead of 4:

Terminal Showing all Steps (Sorry about a typo command!)
Terminal Showing all Steps (Sorry about a typo command!)

sudo fallocate -l 4G /swapfile

STEP 4: SWAP file is created. Let’s give root-only permissions to it.

sudo chmod 600 /swapfile

STEP 5: Mark the file as SWAP space:

sudo mkswap /swapfile

STEP 6: Finally enable the SWAP.

sudo swapon /swapfile

STEP 7: You can now check using the same swapon command to check if SWAP is created.

sudo swapon --show

STEP 8: Also recheck the final partition structure.

free -h

STEP 9: Once everything is set, you must set the SWAP file as permanent, else you will lose the SWAP after reboot. Run this command:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

That’s it! Exit the terminal.

You can launch the ‘System Monitor’ utility and check SWAP status. It should be enabled!

System Monitor -Resources shows SWAP usage
System Monitor -Resources shows SWAP usage

METHOD 2: GUI Way using GParted

STEP 1: First thing to know before you do anything is to figure out the SWAP size. Typically, it is good to have twice as much as your RAM. But you can even go ahead with exactly as much as your RAM – your choice. In this example, I choose to go with twice that of RAM. I have my test PC with 2 GB memory.  So, I will set 4 GB as SWAP.

STEP 2: Once you figured out the size of SWAP space, it’s time to create a partition of that size. You need to be careful here. Any wrongdoing here may mean a significant data loss! So for beginners, I would recommend taking a backup of your data to an external hard disk drive before moving ahead.

You can create a partition in several ways, but I recommend GParted, free software for partitioning and formatting needs, and is available from Software Center of Ubuntu/Linux Mint/elementary OS. But since you can’t resize and create a new partition on an active partition on which you are currently running Linux, I suggest you create a GParted Live USB drive and come back to this tutorial for next step. Alternatively, you can use an Ubuntu or Linux Mint Live USB drive too because it comes packaged with GParted.

STEP 3: Boot your PC into the GParted Live USB drive. Ubuntu/Linux Mint live USB users need to start GParted after booting into live desktop.

STEP 4: In the GParted window, select the Partition which you want to resize. Right-click on it and choose ‘Resize’.

GParted - Resize Partition
GParted – Resize Partition

STEP 5: You can drag the box and resize the partition. Or enter the SWAP size in MB in the box ‘Free space following (MiB). For example, I have added 4000 to indicate 4 GB partition.

Resizing a current partition will automatically create an ‘Unallocated’ new partition which is going to be the SWAP partition. Note that the actions you are doing now on GParted are not applied immediately. It processes all the commands as a batch process when you initiate it. But that’s at last. Proceed as follows.

STEP 6: You will see unallocated file system of the size you created. Right-click on it and select ‘New’. In the context menu, select ‘File system’ as ‘linux-swap’. You can enter ‘SWAP’ in the ‘Label’ field just for easier identifying.

SWAP Partition
SWAP Partition

STEP 7: Click ‘Add’.

STEP 8: Click ‘Apply’. Now GParted will shrink the partition and then add a new SWAP partition.

STEP 9: After the operations are complete, click ‘Close’.

STEP 10: You should now see the new Linux-Swap partition that you created.

STEP 11: Once everything is set, you must set the SWAP file as permanent, else you will lose the SWAP after reboot. Select the SWAP and then click ‘Partition’ > ‘Swapon’. You should see a lock added.

Make SWAP permanent using Swapon
Make SWAP permanent using Swapon

STEP 11: Close GParted and click the ‘Exit’ button on the GParted desktop and then reboot into your Linux system.

Your Linux system will automatically recognize the SWAP partition you have created! You don’t need to do anything. You can confirm if SWAP is active from the ‘System Monitor’.

Launch ‘System Monitor’ which is included in Ubuntu and Linux Mint by default (elementary OS users need to install it from AppCenter) and check the resources tab. You should see SWAP active.

System Monitor -Resources shows SWAP usage
System Monitor – Resources shows SWAP usage

Kiran Kumar
Hi there! I'm Kiran Kumar, founder of FOSSLinux.com. I'm an avid Linux lover and enjoy hands-on with new promising distros. Currently, I'm using Ubuntu as a daily driver and run several other distros such as Fedora, Solus, Manjaro, Debian, and some new ones on my test PC and virtual machines. I have a day job as an Engineer, and this website is one of my favorite past time activities, especially during Winter ;). When I'm not writing for FOSSLinux, I'm seen biking and hiking on scenic trails. I hope you enjoy using this website as much as I do writing for it. Feedback from readers is something that inspires me to do more and spread Linux love!. If you find a time, drop me an email or feedback from the 'Contact' page. Or simply leave a comment below if you found this article useful. Have a good day!

11 COMMENTS

  1. Thanks!
    Tried the first method of creating a swap file.
    My kernel wouldn’t update due to low memory, and while investigating I noticed I had no swap drive.
    All sorted.

  2. Very helpful though I haven’t tried it yet. I neither wanted to set it up using Microsoft windows nor reinstall the Distro. (Ubuntu studio 20.04 en I hope it will work the same with kali Linux).

  3. Kiran, Thank you for sharing … very clear and precise instructions!

    I have only recently invested in my first large capacity SSD (4TB SATA3 – 6Gb/s – on /dev/sdb) and so I now have a spare, though relatively tiny, 32GB mSATA SSD (also SATA3 – 6Gb/s – on /dev/sdc) that would become unused if I let ubuntu default to using just /swapfile . (FWIW, /dev/sda is a 1.5TB SATA3 HDD)
    Since I also upgraded RAM from 8GB to 16GB I doubt that memory page swapping will happen very often. My system dual boots Ubuntu (primarily) and then Windows 10 (only when gaming .. hence the upgrade to 16GB of DDR3).

    So after tweaking UEFI settings for that mSATA SSD to use AHCI, I used gparted to create one ~20GB partition for VM-Win10 and one ~12GB partition for VM-Linux. (So now this SATA3 connected SSD serves as my virtual memory device.) I read that when hibernating ubuntu compresses RAM contents to a default 2/5 of RAM capacity but was not as confident about Windows doing something similar for hiberfil.sys.

    ** ** Can you think of any reason why dedicating this otherwise unused 32GB SSD to virtual memory might be counter-productive? ** **

    It is an older technology (Samsung PM830) that, for the past 6 years, has been used in concert with Intel Smart Response Technology to both cache a slower HDD and also to support Intel Rapid Start Technology by storing the hiberfil.sys. Of course, neither function is important now that I have a capacious SSD from which to boot either OS. If the older SSD wears out then it’s no great loss but like I said earlier I doubt that it will see much paging activity and any hibernation writes will be relatively infrequent.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,275FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

CopyQ – Advanced clipboard manager for Linux

We have all had that moment when we copied a text, but we first needed another one, and in the process, lost the first one. If you're lucky, you can get it back quickly with a bit of work.

Test drive a Linux distro online before you hate it

Enter DistroTest.net, a website that allows Linux users to test various distros online, without downloading the ISO or installing the distro. With DistroTest.net, you can check a distro with no muss, no fuss.

Top 10 Reasons to use Xfce as your Desktop Environment

There are many choices for desktop environments for Linux based operating systems. Mainly, you can install any DE of your choice on most of the Linux based distributions, even if they are not offered as a package officially. In our recent articles, we discussed the best of KDE and Cinnamon. In this article, we wish to present to you the top reasons why you should consider Xfce as your desktop environment.

10 Best PDF Editors for Linux

In this article, we will take a look at 10 of the best PDF editors and tools out there in 2019 that are available for Linux platforms. The editors are going to be judged on the basis of their functionalities, portability, ease of installation, price, and convenience.