Ubuntu – How to create a swap partition if I already have 4 primary partitions


I need to add a swap partition to a running installation of Ubuntu. So I freed up 4 GB, but I can't create a new partition to assign as Swap because I get the following error: "It is not possible to create more than 4 primary partitions"

I have dual boot with windows, my four partitions are:
– The 100 MB partition Windows always makes
– The Windows Partitions
– The Ubuntu partition
– The second Ubuntu partition for files

When I installed Ubuntu I decided to not have a Swap partition but now I would like to add it. This is the distribution of the HDD:

My Partitions

How can I solve this without formatting my current partitions, and add a swap partition?

Best Answer

You do not need a swap partition: you can just as well use a swap file. There is no performance difference between a swap partition and a swap file. To add a swap file:

  1. Create the file (in this example, a 4 GiB file named /swapfile):

    sudo fallocate -l 4G /swapfile
  2. Format it as swap:

    sudo chmod 600 /swapfile
    sudo mkswap /swapfile
  3. Enable swapping on the new swap file:

    sudo swapon /swapfile
  4. Add the newly created file to /etc/fstab by appending /swapfile none swap sw 0 0 to /etc/fstab.

See the Digital Ocean tutorial How To Add Swap on Ubuntu 14.04.

Related Question