How to Create Swap Partition in Linux

A swap partition can be an important component of any Linux system. It provides additional memory when your system’s RAM is fully utilized. Without enough swap space, your system could slow down dramatically or even freeze when memory demands are high.
In this comprehensive guide, we will walk through the entire process of creating a swap partition on a Linux system, specifically for Ubuntu. We will cover everything from checking your current swap configuration to creating and formatting the new partition, making it permanent, and troubleshooting any issues along the way.
Whether you are setting up a new Linux install or want to add additional swap space to an existing system, this guide will provide all the necessary steps. By the end, you will have a fully configured swap partition ready to help optimize your system’s performance.
Prerequisites
Before we get started, there are a couple of requirements to complete this guide:
- Basic knowledge of Linux and comfort using the terminal. We will be using various command line utilities.
- Root access to an Ubuntu Linux distribution. To create partitions and modify system files, you will need to operate with root privileges.
- Available disk space. We will be creating a new partition for swap, so you need free space on your disk.
- Backups (recommended). When modifying disk partitions, it’s always a good idea to have backups, just in case.
As long as you meet these requirements, you are ready to begin creating your swap partition. Let’s start by looking at the current swap setup.
Checking Current Swap Information
First, we need to check if the system already has any swap space configured. This gives us an idea of the current swap situation and whether we need to add more.
Use the swapon command to see currently enabled swap areas:
sudo swapon --show
This will print out any active swap partitions or files. If nothing prints, there is no swap enabled.
To see more details on swap areas including size, use swapon with the -s flag:
sudo swapon -s
This provides more detailed output including the swap file or partition name, type of swap, size, used space, and priority.
You can also use a utility like grep to parse the output of /proc/swaps which contains similar information:
sudo grep --color=auto Swap /proc/swaps
In addition to currently enabled swaps, we also want to check any swap entries that may be configured to activate on boot via /etc/fstab.
Examine this file for any lines starting with “swap”:
sudo cat /etc/fstab
This will print lines that define permanent swap mounts. The output will include the swap area name and mount options. With this information, you can now see if any swap space is already set up, the size of any existing swap areas, and if additional swap is needed.
Creating the Swap Partition
If you have determined that additional swap space is required, the next step is to create a new swap partition.
There are a couple of ways to go about this, but we will use fdisk it to create a new primary partition for swap.
Note: This will erase any existing data on the partition, so be sure you do not overwrite another important partition. First, run fdisk against the disk you want to partition:
sudo fdisk /dev/sda
Replace /dev/sda with your actual disk device name. Inside the fdisk interface:
-
- Type
pto print the partition table and identify available free space. - Type
nto create a new partition. - Specify the partition type as
Linux swap. - Enter the starting and ending blocks for the size you want.
- Type
tto set the hex code82for a Linux swap partition. - Verify the partition table and changes look correct.
- Type
wto write the partition table and exit.
- Type
For example:
Command (m for help): p
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 62914559 30411280 83 Linux
Command (m for help): n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
Partition number (1-4, default 2):
First sector (20973568-62914559, default 20973568):
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-62914559, default 62914559):
Using default value 62914559
Command (m for help): t
Selected partition 2
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap'
Command (m for help): p
Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 sectors
Units = sectors of 1 * 512 = 512 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 2048 2099199 1048576 83 Linux
/dev/sda2 2099200 62914559 30411280 82 Linux swap
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
This will create a new swap partition of the desired size. Next, we need to prepare it for usage.
Making the Swap Partition Usable
Now that we have an unformatted swap partition, we need to set it up to be used as an actual swap area. First, we format the partition with the mkswap utility:
sudo mkswap /dev/sda2
Replace /dev/sda2 with your actual swap partition.
This formats the partition with the Linux swap file system.
Next, enable the swap area with the swapon command:
sudo swapon /dev/sda2
Our new swap space is now active and ready for use!
We can verify it is enabled by checking swapon --show again and it should now be listed.
Making the Swap Permanent
Currently, our new swap partition is enabled, but it will not persist on reboot unless we add it to the /etc/fstab file. First, get the UUID of the new swap partition:
sudo blkid | grep swap
The output will provide the UUID in a format similar to:
/dev/sda2: UUID="5b6e7eb0-1e89-4127-b0b7-26a37be8fce4" TYPE="swap"
Next, add a new line to /etc/fstab with the UUID and swap mount options:
UUID=5b6e7eb0-1e89-4127-b0b7-26a37be8fce4 none swap sw 0 0
Now our swap partition will automatically mount on boot.
Test that the /etc/fstab entry works by unmounting and remounting the swap:
sudo swapoff -a sudo swapon -a
Check it was re-enabled using swapon --show.
The last step is to enable the OS to start swapping memory when needed:
sudo sysctl vm.swappiness=10
This sets the swappiness value to 10 which is a normal level for desktop systems. We now have a permanent swap partition that will be used by Linux to optimize performance!
Troubleshooting
There are a few issues that may come up when creating and configuring a new swap partition. Here are some common problems and solutions.
- Swap partition not enabled on boot
If your new swap area is not enabled after restarting, the /etc/fstab entry may be incorrect. Double-check check the UUID matches the outputĀ blkid and that the mount options are correct.
- Swap space not being used
Check the swappiness value with cat /proc/sys/vm/swappiness. If it is 0, then the system will not swap at all. Set it to a lower value like 10. Also, ensure there are no limits set in /etc/sysctl.conf.
- Out-of-memory errors
If you are still getting out-of-memory errors after adding more swaps, the swap space may be too small for your workload. Create an additional swap partition or swap file to add more capacity.
- Slow performance
Excessive swapping can slow down performance. Make sure the swappiness value is not too high and close resource-heavy programs to reduce memory pressure. Adding more RAM may also help reduce swap usage. Be sure to monitor swap usage with tools like top or htop to see if the new swap is being utilized as expected.