How to Create Swap Space in Arch Linux (Guide)

This article will show you how to create swap space in Arch. Swap space is a great way to give yourself a safety net when it comes to memory, or RAM. Not only is it good to have this as a safety net, it’s just great to have more memory!

This article will show you how to create swap space in Arch.

Introduction

Swap space is a great way to give yourself a safety net when it comes to memory, or RAM. Not only is it good to have this as a safety net, it’s just great to have more memory! This article will go through disk partitioning, creating a swap filesystem, activating it and adding an entry for it in the fstab file so that it will automatically mount at boot.

Disk Partitioning

The first step is in partitioning the disk you’d like to use is identifying the device. Use the lsblk command to display all connected disk devices. If there are already disks partitioned, this command will output them in a tree formation. The name of the device you are looking for will look like this:

/dev/sdb

Now that you have identified the name of the device and location, we need to partition the disk. In my case, I will create a single partition that will be the size of the entire disk. To do that, open the device with fdisk.

fdisk /dev/sdb

To create this single partition disk, use the d command until you are informed that there are no partitions left. Next, use the n command and hit enter through the defaults to create a partition that takes up the full size of the disk. Lastly, use the w command to write the changes to disk and exit fdisk.

The Swap Filesystem

The next step is to create the swap filesystem and activate it. The command to do this is mkswap. With this command, we will identify the disk that we want to use and, more specifically, the partition number that needs a swap space filesystem. Here’s what that will look like, keeping in mind that you should be replacing /dev/sdb with whatever your disk device is named.

mkswap /dev/sdb1

As you can see, we add the number 1 to the device name in order to specify the partition number to configure. Now, if we haven’t received any errors, we can activate that newly created swap space with swapon.

swapon /dev/sdb1

Now you can take a look at your system monitoring tool, like top or htop, and you’ll see that you now have more swap space! However, if we were to restart the system right now, we’d lose that swap space. We would have to reactivate it with swapon, unless we have an fstab entry that configures the swap to activate automatically at boot.

Creating an Fstab Entry

The fstab file is a configuration file that specifies which disk devices need to be mounted/activated and how exactly that should be done. You can take a look at what already exists in that file with the cat command, like this.

cat /etc/fstab

The output that follows may have one or many lines, depending on how many disks are connected to your system. At least one of them will probably include one of the partitions on /dev/sda, which is most likely an ext4 type filesystem. Taking a quick glance at this will give you a general idea of how it all works.

Now that you understand how this file works, add a line to the end of the fstab file with echo. Replace /dev/sdb1 with the name and partition number with yours, if different.

echo "/dev/sdb1 none swap sw 0 0" >> /etc/fstab

Now, safely restart your system and check top or htop to see that the swap space has successfully been reactivated on boot.

Conclusion

If you have gotten this far, congratulations! You have now created swap space in Arch Linux. Now you have an extra layer of RAM memory that can be used as either a supplement or a safety net.

CategoriesAll