Swap Partition – How to Create Swap After Installation in Ubuntu 14.04 LTS

14.04partitioningswap

I installed Ubuntu and forgot to create swap during installation… So I did cut off 4GiB out of my system partition and I'm confused what to do next? I can reboot Ubuntu everything is fine but when I run:

sudo blkid

or

sudo parted -l

or

sudo fdisk -l

it doesn't appear anywhere… for example this output:

ubuntu@ubuntu:~$ sudo parted -l
Model: ATA Hitachi HDP72503 (scsi)
Disk /dev/sda: 320GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type      File system  Flags
1      32,3kB  47,5GB  47,5GB  primary   ext4         boot
3     47,5GB  51,5GB  3999MB  primary   linux-swap(v1)
2      51,5GB  320GB   269GB   extended
5      51,5GB  320GB   269GB   logical   ntfs


Warning: Unable to open /dev/sr0 read-write (Read-only file system).
/dev/sr0 has been opened read-only.
Error: Can't have a partition outside the disk! 

I was using this topic but still cannot figure it out.

other outputs:

ubuntu@ubuntu:~$ sudo fdisk -l

Disk /dev/sda: 320.1 GB, 320072933376 bytes
255 heads, 63 sectors/track, 38913 cylinders, total 625142448 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x27252724

/dev/sda1   *          63    92851625    46425781+  83  Linux
/dev/sda2       100663357   625137344   262236994    5  Расширенный
/dev/sda3        92852224   100663295     3905536   82  Linux своп / Solaris
/dev/sda5       100663359   625137344   262236993    7  HPFS/NTFS/exFAT

and:

ubuntu@ubuntu:~$ sudo blkid
/dev/sda1: UUID="98b0cee6-3b81-4e39-bb43-4847e297b7ff" TYPE="ext4" 
/dev/sda3: UUID="807aba97-139f-4170-9bbf-5b58def95125" TYPE="swap" 
/dev/sda5: UUID="2644E68F76515A43" TYPE="ntfs"

and:

free -l
         total       used       free     shared    buffers     cached
memory:    2063796    1238992     824804       8024      78256     535504
Low:        880428     576176     304252
High:      1183368     662816     520552
-/+ buffer/cache:     625232    1438564
Swap:            0          0          0

Best Answer

To begin, one last check to make sure you definitely don't have swap:

sudo swapon -s

This should list any available swap files. If nothing is listed then you will need to create one. The following command will create a 4gb swap, as an approximate rule the size of your swap should be the same size as the amount of RAM your machine has:

sudo fallocate -l 4G /swapfile

(obviously replace 4G with 8G if you have 8gb of RAM etc.!)

Verify that the swap size is correct with:

ls -lh /swapfile

Next set the permissions on the swap file so it's only accessible by root:

sudo chmod 600 /swapfile

Next set the swap file up using:

sudo mkswap /swapfile

Finally use the following command to enable the swap:

sudo swapon /swapfile

You can check that swap is now in use by typing:

sudo swapon -s

Congrats, you should now have swap. Next you might want to...

Make swap automatically load at startup:

sudo nano /etc/fstab

And then add the following line to the bottom of the file:

/swapfile   none    swap    sw    0   0

For more information, such as optimizing swap, this article has a great overview.

Related Question