How to add or resize swap partition on Ubuntu 16.04



WARNING: Always do a backup before following this tutorial. You could loose all your information!

RAM is orders of magnitude faster than a harddrive, even a ssd harddrive. However RAM is capacity is also orders of magnitude less than hardrive size. When all the RAM capacity is used the Operating system needs to free some ram and its required a place to leave the freed information selected to be removed in the RAM, this is what is called Swap space. A swap space is usually an special partition in your hardrive and is proportional to the RAM capacity of your computer.

In some vps like digitalocean you dont have any swap partition and since they use SSD it is not recommended to add a swap partition. If you own a computer with a SSD hardrive you need to know that can cause hardware degradation to add a swap partition on this kind of disks.

Check your current swap partition

sudo swapon -s

This will return a brief summary of your swap partition or none if you dont have a swap partition

Planning the swap partition

The size of your partition depends a lot on the usage of a computer. If your computer memory workload is high you need yo have a big swap space. For the average case we recommend to use this settings:

  • For less than a 8gb of RAM: use 2 or 3 times the amount of RAM
  • For more than 8gb and less than 64gb: use between 1 and 1.5 times the amount of RAM
  • For more than 64gb: 4gb of swap or no swap at all.

On personal desktop computers or homelabs I like to add the swap parition on a different disk than the one used for the OS to improve the performance.

Adding the swap space

Check if your harddrive has free space to add a new partition, if in your case you dont have any freespace to add a new partition you could add swap space using a special file.

With cfdisk you can check how much free space left on a device to create a partition:

cfdisk /dev/sdX

In my case my test vm didnt have free space for a new partition. So the alternative is to use a swap file. If you want to create a partition skip the next section and go to Adding a Swap partition.

Adding a swap file

Check if you have enough free space with df.

df -h

Now create an empty fiel with zeros: Replace X with the desired swap space

sudo dd if=/dev/zero of=/swapfile bs=XG count=4

Now we prepare the file

sudo chmod 600 /swapfile
sudo mkswap /swapfile

Finnaly we enable the swap with swapon:

sudo swapon /swapfile

To have the swap file on the next boot add it to the /etc/fstab

/swapfile   none    swap    sw    0   0

Adding a Swap partition

Skip this step if you created the swapfile.

But if you have space use fdisk to add the new swap space.

fdisk /dev/sda

Now press n and press enter if you are ok with the first sector (you usually are ok). Then fdisk will prompt with Last cylinder, +cylinders or +size{K,M,G} (XXX-YYY, default ZZZ): * in this step use XG+ where X is the value in gigabytes of your swap partition. Now configure the partition type to swap. Use the t select the correct partition number (be sure to select the correct one!) and use 82 as value type. Commit the changes with w* to the hardrive.

Format the new partition with: X is the letter of your drive and Y is the number of the partition.

mkswap /dev/sdXY

WARNING: the following steps could break you settings!

Add the partition to the /etc/fstab. Open it with vim or nano and add this:

/dev/sdXY       swap        swap        defaults    0 0

Check with df -h which harddrive has space to add the swap partition.

Checking everything

Now re execute swapon to verify that the swap was created.

sudo swapon -s

Advance swap setting

swappiness is a parameter to configure how much often your system swap data from the RAM. The swappiness valid value range is between 0 and 100. Values near 0 means that the system will swap when is absolutely necessary. Check you swappiness with:

cat /proc/sys/vm/swappiness

You can change it using the sudo sysctl vm.swappiness=10 or make it permanent changing the /etc/sysctl.conf and adding vm.swappiness=10.