Allocate swap after Ubuntu 14.04 LTS installation

25,952

Solution 1

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

Solution 2

To answer your question indirectly, you don't need to manage swapfiles yourself. There is a package called swapspace which will dynamically add swap files as needed.

  1. sudo apt-get install swapspace

Then you are done. Your system will grow and shrink swap space as needed.

Solution 3

Follow these steps:

  1. sudo dd if=/dev/zero of=/mnt/{filename}.swap bs=1M count={swap_size}
  2. sudo mkswap /mnt/{filename}.swap
  3. sudo swapon /mnt/{filename}.swap
  4. sudo gedit /etc/fstab
  5. Add the following text at the end of the file, /mnt/{filename}.swap none swap sw 0 0

Note: Replace {filename} with any name you want to set to the file and replace {swap_size} with the size you want to assign to the swap file. Be sure the size of the file must the twice larger than the memory size.

Share:
25,952

Related videos on Youtube

Ahsan Hussain
Author by

Ahsan Hussain

Updated on September 18, 2022

Comments

  • Ahsan Hussain
    Ahsan Hussain over 1 year

    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:~$ 
    
    • shellter
      shellter over 9 years
      The clue was in the error message dd: invalid number '4sudo', right? I would guess you had a copy/paste problem. Glad you got your problem solved! In the future, read those error messages carefully and try to figure out why you're getting that message!;-) Good luck.
  • Ahsan Hussain
    Ahsan Hussain over 9 years
    sorry i mistakenly tried to run command twice, i think its worked for me :) now also showing swap in system monitor.. Thanks alot
  • Ahsan Hussain
    Ahsan Hussain over 9 years
    i restarted the machine and the swap is gone, its again not available
  • nyuszika7h
    nyuszika7h over 9 years
    The "running out of memory" part should be added to the answer. Comments are meant to be transient, they may be deleted.
  • nyuszika7h
    nyuszika7h over 9 years
    "Be sure the size of the file must the twice larger than the memory size." That advice no longer holds nowadays unless you're really low on memory (say, 1 GB or less). If you want to use hibernation, you'll need at least the amount of physical memory as swap, but there's no reason to have 8 GB swap if you have 4 GB RAM.
  • Wolverine
    Wolverine over 9 years
    Your suggestion is considered.
  • LOG_TAG
    LOG_TAG over 7 years
    sudo apt-get install swapspace is best option compare to allocating like this?