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

Create a SWAP in Ubuntu, elementary OS, Linux Mint

SWAP partition is very important 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 an 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 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 entire operating system again just to add SWAP partition. You can add it anytime!

Create a SWAP in Ubuntu, elementary OS, Linux Mint

Create 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 now 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 check the final partition structure again.

free -h

STEP 9: Once everything is set, you must set the SWAP file as permanent, else you will loose 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 ‘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 how much SWAP do you need. 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 wrong doing here may mean a major data loss! So for beginners, I would recommend to take a backup of your personal data to an external hard disk drive before moving ahead.

You can create a partition in several ways, but I recommend GParted, a 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 a active partition on which you are currently running Linux, I suggest you to create a GParted Live USB drive and come back to this tutorial for next step. Alternatively, you can use a 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 select ‘Resize’.

GParted - Resize Partition
GParted – Resize Partition

STEP 5: You can drag the box and resize the partition. Or simply 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 a ‘Unallocated’ new partition which is going to be the SWAP partition. Note that the actions you are doing now on GParted is not applied immediately. It processes all the commands as a batch process when you initiate it. But that’s at last. Proceed as follows.

Resize a Partition
Resize a Partition

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.

SWAP Partition Created
SWAP Partition Created

STEP 11: Once everything is set, you must set the SWAP file as permanent, else you will loose 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 ‘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
Hi there! I'm Kiran Kumar, founder of FOSSLinux.com. I'm an avid Linux lover, and enjoys hands-on with new promising distros. Currently, I'm using Linux Mint as a daily driver and run several other distros such as Fedora, Solus, Ubuntu, 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. 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 my love for Linux. If you find time, drop me an email or feedback from 'Contact' page. Or simply leave a comment below if you found this article useful. Have a good day!

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz