How to enable Zswap on Linux for better performance
SWAP(aka virtual memory) is very useful on Linux systems that have a minimal amount of physical memory. Without this feature, many low-end computers running modern Linux operating systems would freeze all the time.
Still, SWAP does have one huge down-side: disk I/O. If your machine is working with the SWAP file or partition regularly, it has the potential to seriously bog down the performance of your hard drive, which can negate the benefit of using SWAP in the first place.
That’s where Zswap comes in. It’s a Linux kernel feature (introduced in version 3.8) that, when enabled, allows the system to compress the Swap cache, resulting in better performance on your Linux system overall.
If you’re noticing some serious disk performance on Linux while using Swap, you need Zswap! Follow along with this guide and learn how to enable the Zswap feature on your Linux system! Here’s how to set it up.
Ubuntu/Debian instructions
The Zswap feature is an optional Linux kernel parameter. Since it’s a kernel parameter, you won’t be able to find “Zswap” in the settings on your Ubuntu or Debian system. Instead, it requires tinkering around with the bootloader.
On Ubuntu and Debian, Grub is used, so accessing the Grub configuration file is what must be done to enable Zswap. However, before adding this command-line argument, it’s essential to create a backup of your Grub config file.
Back up Grub
To create a backup of Grub, launch a terminal window. Then, gain root access with su or sudo -s.
su -
or
sudo -s
Now that you’ve got a Root shell use the CP command to make a quick backup of the Grub configuration file.
cp /etc/default/grub /etc/default/grub.bak
Enable Zswap
With the Grub config file backed up, open it up in Nano for editing purposes.
nano -w /etc/default/grub
In the Nano editor, locate the line GRUB_CMDLINE_LINUX_DEFAULT. This line of code in the configuration file should have a few command-line arguments, such as “quiet splash,” and maybe a few others. Don’t erase them! Instead, add the code below at the end of the line.
zswap.enabled=1
It should look like the following picture.
When everything looks good in the Grub CFG file, save the edit with Ctrl + O and exit with Ctrl + X. Then, finish up the process by updating your Grub bootloader via update-grub.
sudo update-grub
Reboot your Ubuntu or Debian PC. When it loads back up, Zswap should be up and running!
Disable for Ubuntu/Debian
Not happy with Zswap? Want to get rid of it? Here’s how to do it. First, open up a terminal and gain root with su or sudo -s.
su –
or
sudo -s
Next, delete the Grub file with rm.
rm /etc/default/grub
Restore the backup file with the mv command.
cd /etc/default/ mv grub.bak grub
Finally, update Grub using the update-grub command.
sudo update-grub
Arch Linux instructions
Zwap is available on Arch Linux, and it’s incredibly easy to enable. It doesn’t require the modification of any Grub configuration files whatsoever. Instead, the user only needs to install a package and enable it with the systemd init system.
sudo pacman -S systemd-swap sudo systemctl enable systemd-swap
With the systemd-swap service active with systemd, reboot your computer. When it comes back online, you’ll have Zswap!
Fedora/OpenSUSE instructions
Fedora and OpenSUSE are very similar operating systems in a lot of ways. Mainly, they use the same packaging format (RPM) and Redhat-based tools. In addition to these similarities, the way they set up Grub is very similar.
For this reason, we’re going to show you how to set up the Zswap feature on Fedora and OpenSUSE in the same section.
Back up Grub
To start, launch a terminal window and gain a root shell. On Fedora and SUSE systems, the root account isn’t turned off, so access superuser with su.
su -
Once you’ve got root access, make a quick backup of your Grub configuration file using the cp command.
cd /etc/default cp grub grub.bak
Enable Zswap
Now that you’ve made your backup, it’s time to enable Zswap in the Grub bootloader.
nano /etc/default/grub
Look through the Grub configuration file for GRUB_CMDLINE_LINUX_DEFAULT=
Note: on Fedora, the line may be GRUB_CMDLINE_LINUX= instead.
Once you’ve found the GRUB_CMDLINE_LINUX_DEFAULT or GRUB_CMDLINE_LINUX line in the file, paste the code below in at the end, before the last quotation mark.
zswap.enabled=1
Save the edit you’ve made to the Grub bootloader with the Ctrl + O keyboard shortcut. Then, exit with Ctrl + X.
Finally, finish the process by updating your bootloader using grub2-mkconfig.
grub2-mkconfig -o /boot/grub2/grub.cfg
Disable for Fedora/OpenSUSE
Decide you don’t need Zswap on your Fedora or OpenSUSE Linux system? Here’s how to turn it back off. First, launch a terminal window. Then, gain root with the su command.
su -
Once root is active, use the CD command and move into /etc/default/.
cd /etc/default
Delete the Grub file with rm.
rm grub
Next, restore the backup Grub file with mv.
mv grub.bak grub
Finally, update Grub with grub2-mkconfig.
grub2-mkconfig -o /boot/grub2/grub.cfg