Allocate Swap After Ubuntu 14.04 LTS Installation

14.04swap

I've installed Ubuntu 14.04 LTS about one month ago, I'm dual booting Ubuntu 14.04 LTS and Windows 8.1.

I didn't create any swap space while installation but now I really need to add this for Ubunutu. I tried checking for any assigned swap space by using sudo swapon -s and I got empty headers like shown below:

ahsan@ahsan-Inspiron-N5110:~$ sudo swapon -s
Filename                Type        Size    Used    Priority

Then I tried to allocate some swap space using dd of=output.dat bs=1 seek=390143672 count=0 and the output was:

0+0 records in
0+0 records out
0 bytes (0 B) copied, 0.000170607 s, 0.0 kB/s

I also tried the command sudo dd if=/dev/zero of=/swapfile bs=1G count=4 and output was:

dd: memory exhausted by input buffer of size 1073741824 bytes (1.0 GiB)

Then I tried sudo fallocate -l 4G /swapfile but output was:

fallocate: /swapfile: fallocate failed: Operation not supported

I aslo checked my hard drives following is my hard drive structure:

ahsan@ahsan-Inspiron-N5110:~$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2        99G   18G   77G  19% /
none            4.0K     0  4.0K   0% /sys/fs/cgroup
udev            1.4G  4.0K  1.4G   1% /dev
tmpfs           286M  1.2M  284M   1% /run
none            5.0M     0  5.0M   0% /run/lock
none            1.4G   24M  1.4G   2% /run/shm
none            100M   56K  100M   1% /run/user

And the output of free -m is:

ahsan@ahsan-Inspiron-N5110:~$ free -m
             total       used       free     shared    buffers     cached
Mem:          2850       2665        184        421         25        846
-/+ buffers/cache:       1794       1055
Swap:            0          0          0

Please show me how can I add the swap without affecting any data of mine. I've installed 64 bit Ubuntu LTS and have 3GB of RAM and a 500GB hard drive.

I have been to this Ask Ubuntu question and I tried the commands and the output is:

ahsan@ahsan-Inspiron-N5110:~$ sudo dd if=/dev/zero of=/swapspace bs=1G count=4
4+0 records in
4+0 records out
4294967296 bytes (4.3 GB) copied, 47.1951 s, 91.0 MB/s
ahsan@ahsan-Inspiron-N5110:~$ sudo dd if=/dev/zero of=/swapspace bs=1G count=4sudo mkswap /swapspace
dd: invalid number ‘4sudo’
ahsan@ahsan-Inspiron-N5110:~$ 

Best Answer

First, to create 4,000 MB of swap space:

$ sudo dd if=/dev/zero of=/swapspace bs=1M count=4000
4000+0 records in
4000+0 records out
4194304000 bytes (4.2 GB) copied, 5.92647 s, 708 MB/s

or

$ sudo fallocate -l 4000M /swapspace 

Next turn it into a usable swap file:

$ sudo mkswap /swapspace
Setting up swapspace version 1, size = 4095996 KiB
no label, UUID=7d1895e4-7ccf-42c6-979a-51ebddb49e91

Activate it:

$ sudo swapon /swapspace 

Confirm active swap spaces:

$ cat /proc/swaps
Filename                Type        Size    Used    Priority
/swapspace              file        4095996 0       -1

Next, add the following line to /etc/fstab to activate the new swap at boot:

/swapspace none swap defaults 0 0

See also this wiki page: https://help.ubuntu.com/community/SwapFaq