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!

7 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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,647FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

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.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

The 6 Best Download Managers for Fedora

It is a well-known fact that using download managers can help improve download speeds as compared to web browsers. Apart from the inbuilt download manager wget on Fedora, just as on any distribution that is based on GNU/Linux package, there are more options to explore.

10 Best Video Editing Software for Linux

This article is for all the Linux users out there who are looking for video editors to create their own professional videos, which can range from short documentaries to even movies.

VIDEO: MX Linux 19 Features and Desktop Tour

MX Linux is a Linux distribution based on Debian stable and using core antiX components. The distributions MX Tools is very popular among users and combined with other several ready-to-use tools, it is great for users who prefer to tweak their distro to their liking. In this video, we will take you through the features tour showing casing MX Linux 19.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.