This question already has an answer here:

I'm new to Ubuntu . I need to increase size of swap file from 8 GB to 16 GB , how can i do it ? and is that in this file /etc/fstab ?

by command free -h

             total       used       free     shared    buffers     cached
Mem:          7.6G       4.8G       2.8G       623M       232M       2.5G
-/+ buffers/cache:       2.1G       5.5G
Swap:          15G         0B        15G

marked as duplicate by Sergiy Kolodyazhnyy, user364819, karel, Eliah Kagan, Pilot6 Jul 4 '17 at 6:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • do you want to use the swap file or a swap partition? either is acceptable, but I believe the partition is more efficient. how ever increasing your partition size requires a live boot, because you partitions have to be unmounted to resize them. – ravery Jul 2 '17 at 5:11
  • i want to try sawp file . but if i used it , is that a problem ? – prog Jul 2 '17 at 5:16
  • OK, first of all, you've asked same question a few hours ago. There were multiple links provided that explain how to do it. Have you even looked at them ? Have you tried any of the methods provided there ? – Sergiy Kolodyazhnyy Jul 2 '17 at 5:24
  • yes , but i thought that s a separate question .. my previous post was closed so i re-opened a new one with a different question and sorry if i didn't any mistake – prog Jul 2 '17 at 5:32
  • @Sergiy -- heynnema asked him to open anew question so he could post detailed instructions. but I got him through it in chat – ravery Jul 2 '17 at 6:10

To make things easier, here's a script that I've written before, but modified for your question. This makes it easy to add desired amount of swap with a single command. It can be also found on my personal github repository, which may include additional fixes and updates.

Steps to set it up:

  1. Copy the script presented below
  2. Save it as file somewhere in come directory. Name it addswap.sh
  3. Open terminal and do cd <directory where you saved the script>
  4. Run chmod +x ./addswap.sh

Usage is very simple:

sudo ./addswap.sh INTEGER LETTER

For adding 1 gigabyte you would do sudo ./addswap.sh 1 G. For adding 1 megabyte do sudo ./addswap.sh 1 M.


set -e  # bail if anything goes wrong

   if [ "$( id -u )" -ne 0  ] ; then
      return 1
   return 0

    # Obtain amount of swap in Gigabytes
    awk '/SwapTotal/{printf "%.2f",($2/1048576)}' /proc/meminfo

   # This is the function that does the job of creatig swap file
   # and enabling it.  All files are timestamped
   printf "Current swap ammount: %f\n" "$(get_swap_amount)"
   printf "Working on creating swap file\n"
   DATE=$(date +%s) # append date of creation to filename
   filename="/swapfile.""$DATE" # File will be /swapfile.$DATE
   dd if=/dev/zero  of="$filename" bs=1"$2" count="$1"
   chmod 600 "$filename"
   mkswap "$filename" && 
   swapon "$filename" && 
   printf "\nCreated and turned on %s\n"  "$filename"
   printf "Current swap ammount: %f" "$(get_swap_amount)"

   # Prompt user to enable this new swap file on boot. If user
   # enters y, the swap file will be added to /etc/fstab
   printf "Do you want to turn on this file at boot?[y/n]\n"
   read ANSWER
   case "$ANSWER" in
    [Yy]) printf "\n%s none swap defaults 0 0\n" "$filename"  >> /etc/fstab &&
       printf "\n %s added to /etc/fstab successfuly\n" "$filename"
       exit 0 ;;
    [Nn]) printf "Exiting\n" && exit 0 ;;
    *) printf "Wrong input: %s . Exiting. /etc/fstab not altered\n" "$ANSWER" && exit 1 ;;


     # check if second argument is a character 
     case "$2" in 
         [A-Z]) return 1;;
         *) return 0;;

    # Check if first argument is a digit. 
    # https://stackoverflow.com/a/3951175/3701431
    case "$1" in
        ''|*[!0-9]*) return 0;;
        *) return 1 ;;



    # Check if we're root and if args are OK. If everything is ok, do stuff

    if is_root 
        if [ $# -ne 2   ] ||  bad_arguments "$@"
            printf "%s\n" ">>> ERR: $0: bad or insufficient arguments" > /dev/stderr
            printf "%s\n" ">>> Usage: $0 INTEGER LETTER" > /dev/stderr
            exit 2
        make_swap_file "$@" && ask_to_enable_on_boot
        printf ">>> ERR: $0 must run as root\n" > /dev/stderr
        exit 1

main "$@"

Test run

$ sudo ./addswap.sh 1 G                                                                                                                                                                 
[sudo] password for xieerqi: 
Current swap ammount: 4.000000
Working on creating swap file
1+0 records in
1+0 records out
1073741824 bytes (1.1 GB, 1.0 GiB) copied, 6.83322 s, 157 MB/s
Setting up swapspace version 1, size = 1024 MiB (1073737728 bytes)
no label, UUID=8bf1d78d-0a8a-478b-a783-38c5935c362f

Created and turned on /swapfile.1498976162
Current swap ammount: 5.000000Do you want to turn on this file at boot?[y/n]

 /swapfile.1498976162 added to /etc/fstab successfuly
  • Thanks for your help. But if I need to increase it to 16 I can use ./addswap.sh 16 G – prog Jul 2 '17 at 17:45
  • @prog yes, you can do that – Sergiy Kolodyazhnyy Jul 2 '17 at 17:59
  • thanks for help , but i've problem i used this command to test if swap file is already now contains 16 GB or not free -h . and i got what i edited in the post . But the code which i want to increase memory for it still doesn't use this additional memory because i ran the command during my code running and found the used space are zero – prog Jul 3 '17 at 23:28

Not the answer you're looking for? Browse other questions tagged or ask your own question.