How to Add Swap Space on Ubuntu Linux

One of the simplest ways of watching against out-of-memory problems in applications is to increase some swap size in your server. In this article, we will explain how to add a swap file to an Ubuntu server.

Step 1: Checking Swap Information

Before we start, first make sure to check if the system already has swap space available by running the following swapon command.

$ sudo swapon --show
Check Swap Space in Ubuntu
Check Swap Space in Ubuntu

If you don’t see any output, that means your system doesn’t have swap space available currently.

You can also confirm that there is no swap space available using the free command.

$ free -h
Check Free Swap Space in Ubuntu
Check Free Swap Space in Ubuntu

You can see from the above output, that there is no active swap on the system.

Step 2: Checking Available Space on the Partition

To create a swap space, first, you need to check your current disk usage and confirm that there is enough space to create a swap file on the system.

$ df -h
Check Filesystem Disk Usage in Ubuntu
Check Filesystem Disk Usage in Ubuntu

The partition with / has enough space available to create a swap file.

Step 3: Creating a Swap File in Ubuntu

Now we will create a swap file called "swap.img" on our Ubuntu root (/) directory using the fallocate command with the size of 1GB (you can adjust the size as per your needs) and verify the size of the swap using the ls command as shown.

$ sudo fallocate -l 1G /swap.img
$ ls -lh /swap.img
Create Swap File in Ubuntu
Create Swap File in Ubuntu

From the above output, you can see that we’ve created the swap file with the correct amount of space i.e. 1GB.

Step 4: Enabling the Swap File in Ubuntu

To enable the swap file in Ubuntu, first, you need to set the correct permissions on the file so that only the root user has access to the file.

$ sudo chmod 600 /swap.img
$ ls -lh /swap.img
Set Permission on Swap File
Set Permission on Swap File

From the above output, you can see that only the root user has read and write permissions.

Now run the following commands to mark the file as swap space and enable the swap file to start utilizing it on the system.

$ sudo mkswap /swap.img
$ sudo swapon /swap.img
Enable Swap Space in Ubuntu
Enable Swap Space in Ubuntu

Verify that the swap space is available by running the following commands.

$ sudo swapon --show
$ free -h
Verify Swap Space in Ubuntu
Verify Swap Space in Ubuntu

From the above output, it is clear that our new swap file has been created successfully and our Ubuntu system will start to use it as necessary.

Step 5: Mount the Swap File Permanent in Ubuntu

To make the swap space permanent, you need to add the swap file information in the /etc/fstab file and verify it by running the following commands.

$ echo '/swap.img none swap sw 0 0' | sudo tee -a /etc/fstab
$ cat /etc/fstab
Mount Swap File Permanently in Ubuntu
Mount Swap File Permanently in Ubuntu

Step 6: Tuning Swap Settings in Ubuntu

There are a few settings that you need to configure that will have an effect on your Ubuntu’s performance when using the swap.

Adjusting the Swappiness Value

Swappiness is a Linux kernel parameter that specifies how much (and how often) your system swaps data out of RAM to the swap space. The default value for this parameter is “60” and it can use anything from “0” to “100”. The higher the value, the higher the usage of swap space by Kernel.

First, check the current swappiness value by typing the following command.

$ cat /proc/sys/vm/swappiness
Check Swappiness Value
Check Swappiness Value

The current swappiness value of 60 is perfect for Desktop usage, but for a server, you must set it to lower value i.e. 10.

$ sudo sysctl vm.swappiness=10

To make this setting permanent, you need to add the following line to the /etc/sysctl.conf file.

vm.swappiness=10

Adjusting the Cache Pressure Setting

Another similar setting that you may want to alter is the vfs_cache_pressure – this setting specifies how much the system will want to cache inode and dentry details over other data.

You can check the current value by querying the proc filesystem.

$ cat /proc/sys/vm/vfs_cache_pressure
Check Cache Pressure Setting
Check Cache Pressure Setting

The current value is set to 100, which means our system removes inode information from the cache too rapidly. I suggest we should set this to a more stable setting like 50.

$ sudo sysctl vm.vfs_cache_pressure=50

To make this setting permanent, you need to add the following line to the /etc/sysctl.conf file.

vm.vfs_cache_pressure=50

Save and close the file when you are done.

Step 7: Removing a Swap File in Ubuntu

To remove or delete the newly created swap file, run the following commands.

$ sudo swapoff -v /swap.img
$ sudo rm -rf /swap.img
Delete Swap File in Ubuntu
Delete Swap File in Ubuntu

Finally, delete the swap file entry from the /etc/fstab file.

That’s all! In this article, we have explained how to create a swap file on your Ubuntu distribution. If you have any queries regarding this article, feel free to ask your questions in the comment section below.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Ravi Saive
I am an experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

5 Comments

Leave a Reply
  1. Why do you use “-rf” in the rm command?

    This can be dangerous if someone misses types the command as:

    $ sudo rm -rf / swap.info
    

    (note space between the "/" and “swap.info”.

    I managed to do this on a production SCO server 25 years ago late on a Friday afternoon. Totally ruined my weekend, but learned to only use “rm -r” when you really mean it.

    In my opinion, it is a bad idea to teach using “rm -rf” without explaining that if this command is miss-typed, you may remove your entire file system, because the command that you are issuing is: remove recursively, without bothering me with questions, because I definitely want to do this.

    In your case, you are doing, so you have to write permission on the file (no need for -f), and it is a file, not a directory (no need for -r).

    The safer command to teach is
    sudo rm /swap.img

    Reply
    • @Dragonmouth,

      Yes, you can setup the Swap partition, only if there is a Free space available on the filesystem. What if there is no free space? in this case adding swap space is useful when you running out of memory..

      Reply
      • “What if there is no free space?”

        Straw man argument. If there is no free space available on the system you cannot set up swap space, either. It’s time to get a bigger disk or another disk.

        Reply
        • @Dragonmouth,

          Yes, if no space is left on the disk, you must add a new hard disk and create a space for a swap.

          Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.