I did not create a swap partition during Ubuntu installation. Later, I freed up some space and made a swap partition. Now after each boot up, I am manually opening gparted to right-click the swap partition to turn on the 'swapon' option.

How can I automatically enable the swap partition at boot?

Also, if I had not created the swap partition, what are my other options?


You need to edit /etc/fstab and add the new swap partition.

sudo nano /etc/fstab

You need to add a line that looks like

UUID=735b3be3-779c-4d21-a944-b033225f3ab4 none   swap    sw      0       0

and you get the UUID using the command

sudo blkid /dev/sda3

(substitute /dev/sda3 with the appropriate device name).


  • Need Help for /etc/fstan i ma getting output :- bash: /etc/fstab: Permission denied – Ashu_FalcoN Aug 31 '14 at 8:20
  • Use sudo gedit /etc/fstab – user323419 Nov 29 '14 at 22:37
  • 3
    To find the appropriate device name: sudo fdisk -l (from: askubuntu.com/questions/182446/…) and then look for Type: Linux swap. – NumesSanguis Aug 17 '16 at 12:30
  • 1
    sudo blkid /dev/sda4 prints nothing for me (sda4 is newly created partition with code 82h, fdisk says it is "Linux swap / Solaris" partition). – Egor Skriptunoff Feb 25 '17 at 21:02
  • 1
    @elvis.dukaj it's perfectly fine to also use the device name. They basically express two different things. One says use the volume with exactly that UUID (something that will change with another mkswap run) and the other says to use primary partition number 3 of disk /dev/sda. Some prefer UUIDs because even if the assignment of device names changes, the UUIDs would remain the same. So that's why the difference matters. But if you are sure that you won't repartition the drive, /dev/sda3 should be equally safe to use. – 0xC0000022L May 24 '18 at 19:49

To create a swap partition after installation, create an empty partition. It should have no holes. You can then format this partition with:

sudo mkswap /dev/sdX

replacing /dev/sdX with your partition. Mount this partition as swap with

sudo swapon -U UUID

where UUID is that of your /dev/sdX as read from this:

blkid /dev/sdX

Bind your new swap in /etc/fstab by adding this line:

UUID=xxx    none    swap    sw      0   0

If you want to use your swap for hibernating then you need to update the UUID in /etc/initramfs-tools/conf.d/resume with this content RESUME=UUID=xxx. Don't forget to $ sudo update-initramfs -u.

To complete things: it is also possible to create a swap file if you do not have a spare partition. This answer gives you an idea of how to create a swap file and enable it on boot.

  • As of right now, I have a SSD with two partitions, but I would like to make a third one. If I have a primary ext4 partition that holds the OS and my files, can I use this to peel off 2GB off it into t a new partition? This primary partition has more than enough free space so that shouldn't be an issue. – dustin Apr 4 '15 at 22:46
  • 2
    Look at gparted for resizing your partition. – NumesSanguis Aug 17 '16 at 12:35

In case you don't want or you're not sure how to create a swap partition, you can create a swap file which will work in the same way as partition. Here are the steps (using terminal):

  1. Create an empty file (1K * 4M = 4 GiB).

    sudo mkdir -v /var/cache/swap
    cd /var/cache/swap
    sudo dd if=/dev/zero of=swapfile bs=1K count=4M
    sudo chmod 600 swapfile
  2. Convert newly created file into a swap space file.

    sudo mkswap swapfile
  3. Enable file for paging and swapping.

    sudo swapon swapfile

    Verify by: swapon -s or top:

    top -bn1 | grep -i swap

    Should display line like: KiB Swap: 4194300 total, 4194300 free

    To disable, use sudo swapoff swapfile command.

  4. Add it into fstab file to make it persistent on the next system boot.

    echo "/var/cache/swap/swapfile none swap sw 0 0" | sudo tee -a /etc/fstab
  5. Re-test swap file on startup by:

    sudo swapoff swapfile
    sudo swapon -va

    Note: Above commands re-checks the syntax of fstab file, otherwise your Linux could not boot up properly.

  • 13
    Thank you for the only answer that uses a swapfile and not a swap partition. In this case I could not create a partition. The commands worked great. – Sarel Botha Feb 4 '17 at 14:53
  • 2
    For those who need higher size swap space, increase count=4M, to XM, for X GiB swap space. In case if anyone missed it. – saurabheights May 15 '17 at 8:52
  • I'm unsure about step 5. Why would I turn off the swap to test? – Raffi Khatchadourian Jan 2 '18 at 16:02
  • 1
    Ah I understand now. You need to do that before you reboot. I got thrown off by on start up. – Raffi Khatchadourian Jan 3 '18 at 3:49
  • 1
    This is great for remote VPS where the disk is actually a virtual device and you don't get control over partitioning. Thanks! – ColinM Oct 25 '19 at 21:16

There's a graphical way to do this using gparted.

Run Gparted with:

sudo gparted

If you don't have it install, you can install it using

sudo apt-get install gparted

Once gparted is running:

  1. Allocate the partition as you would by right-clicking any unallocated block and clicking new
  2. Format the partition as linux-swap
  3. Right-click the partition and clickswapon

There should be a key icon next to your partition name once you're done

  • by far the simplest answer – Shobi Dec 1 '17 at 5:45
  • 2
    To make it permanent, you still have to edit fstab file as in the answer by @user4124 – Madura Pradeep Mar 17 '18 at 4:20
  • 1
    The question already says that they do this. – Chai T. Rex Oct 16 '18 at 18:31
  • 1
    This doesn't answer the question at all. – mook765 Oct 17 '18 at 7:22
  • @mook765, The question is "How do I add swap after system installation?". This perfectly answers that question. – Tayler Aug 29 '19 at 14:47

from linux.com just create swap file as that is more flexible and you can dynamically adjust how many and how large swap storages you have. That also allows you to move swap partition away from your system disk, thus making system faster. Also, if you use several swap spaces, adjust their priority of use to same value, so round-robin is used to write in them, allowing you to get even more speed improvements.

p.s. i would use UUID instead of device name, because it is easily possible to change device order if by accident you leave USB storage inside USB slot and simply removing and plugging back sata cables - you do not need to worry much about what drive is connected where.

  • This was the most helpful response, because it does not require re-partitioning the drive. Too bad Ubuntu doesn't create swap by default when you install it on a new machine! – Ernie Feb 27 '15 at 17:34

You have to have a swap filesystem defined in /etc/fstab. It should contain a line similar to

UUID=67682d1b-a1d8-4377-a3dd-67340c141619 none            swap    sw              0       0

Of course you need to substitute the value of UUID with the identifier of your device.

  • how can I get it? – user13593 Apr 5 '11 at 20:21
  • 3
    You can use the device name instead, like /dev/sda1 (without the UUID). – Adam Byrtek Apr 5 '11 at 20:30

(replace the "x" in sdax with swap partition number eg: sda5)

replace "xxx" with UUID number eg: 67682d1b-a1d8-4377-a3dd-67340c141619)

su (give password to get root permission)

blkid /dev/sdax (get UUID) mkswap /dev/sdax (format swap partition) swapon -U xxx

nano /etc/fstab (edit fstab) UUID=xxx none swap sw 0 0 (enter and save this text to make swap permanant) (then close fsantab)

nano /etc/initramfs-tools/conf.d/resume (create "resume" file and save this text to use hibernate/resume) RESUME=UUID=xxx (ctrl+x to close nano)->(y to save)

restart machine

login and now you can test hibernate / resume


This is a comprehensive Tutorial on how to make a Ram Drive for Ubuntu. Unfortunately.. the screenshots do not show up in this via cut and paste.. Later I will add them, but they are not needed.

The first thing you need to do is create space for your ram drive. I have a dual boot system with only a 32gb hard drive. My Ubuntu is actually installed on an SD card (yes, a real install and not a Live Casper thing). My swap was also on the SD and might get worn out quickly. I looked up how to change the swap and found this article and did not like the answers so I made a tutorial while I made the change.

If you simply wish to increase or decrease your Swap partition, you only need to increase or decrease the partition through your favorite partition manager. Ubuntu already knows where to go and it is size independent. If you want to change the location or make a new Swap, then read on. The first thing you will need to do is Shrink your hard drive to provide space for a swap. If you are shrinking a disk with the Windows system on it, then you'd better use Windows to shrink the disk (It knows its own stuff). If you are on a pure ubuntu, try resizing using a live Ubuntu USB.

Windows Resize Instructions:

  1. Backup your system, and defrag your drive.
  2. Right Click on the start bar.
  3. Select Disk management
  4. Right click ON THE CORRECT DRIVE
  5. Select Shrink.
  6. Type in the correct amount.

If you are in Ubuntu. Make sure you use Ubuntu tools. To finish the allocation, I will use a bootable Live USB of Ubuntu which includes GParted. A Live USB is the best way to use Gparted for any reason, and you should have a USB from your first install or make one to keep handy.

Typing in 2000 was not allowed because there was not enough room to do that even though I had “defragmented” the drive (I do not believe Windows really defragged it). With some experimentation, I was allowed 1000. Then I tried 1000 again and it worked. I should thank my primary school math teacher for that one more than the MS Windows programmers. That left me with 1.95gb which is close enough for my primary school math.

Format the partition

Now I am ready for gparted to format that space as a proper swap drive. If you are in Windows and switching back and forth between Ubuntu and Windows, MAKE SURE YOU HOLD THE SHIFT KEY when clicking "Power" and "Shut down." This is very important, especially when you are messing with the "C" drive particulars in another operating system. If you are dual booting, you should know already that Windows does a fake shut down under normal circumstances. Some people have disabled "Fast Boot". I do not have that choice on my system (Windows 10) and must use the shift key thanks to my friend Google. If you boot with a live USB and you cannot mount the "C" drive (by clicking on it), then you did not fully shut down properly. Reload windows and shut down again using the shift key.

Now you are in Ubuntu Live Mode and read below:

If you are a pure Ubuntu and did not shrink in Windows. You will use the Resize option in gParted. If you already shrank your drive, do not do the resize step. You can type GParted in the Dash bar and get the application quite quickly. Right click on the drive you want to resize.

Now we will format the unallocated space as a partitioned drive:


I did not know what to do, so I looked at my previous Ram Drive on the SD. It is formatted as Linux Swap. So I formatted my new allocation that I recently shrank as Linux Swap too.
I said OK to the Primary Partition and the warning notice and it worked!

Formatting the partition

After the format and new partition

It is a coincidence that the old swap partition on the sd was called mmblk1p5 and new one is also p5 with mmblk0 in front. Yours could be p6 or p7. Don't worry.

The mmblk0 is the physical disk and p is the partition on that physical disk. And mmblk01 is another physical disk, etc. (That is my SD) Before I did any more work, I opened Windows again to make sure Windows still booted. You do not need to do this step if you are lazy or on a pure ubuntu machine. If it broke Windows, I would likely wipe Windows do a fresh install of Ubuntu on that disk (with a swap too). If I broke my system, I would not need to follow any more steps because I can allocate the swap on the new install. All went well and Windows booted. Now you can see the new partition in the Windows disk manager: (Right click on the start bar to get disk management)

Now attach the Swap to the boot routine

Previous answers to this question actually had swapon/off commands which are not needed and can be complex. All one needs to do is find the right file, and point the swap to the new partition. Again, if you only resized the partition, you do not need to do anything. It is already setup to find the partition. The size gets done automatically.

If you changed the partition or are making a first ever swap, then read below:

Let me tell you that, this is the first time I am doing this. I am not a UNIX geek and I prefer GUI tools. I am learning and crossing my fingers as I make this tutorial. All has gone well so far. At the root of your system is a directory called etc. Inside that directory is a file called fstab (no extension). You will need to edit this file with super user "su" rights. Before we do that, we need the UUID of the new Swap Partition you made with the Live USB. Type sudo su Now you are in Super User mode. You can do anything (including wipe your own existence).

Type blkid and look for the uuid of the new swap partition. I saw mine below and highlighted it:

Pay no attention to PartUID's.

Now, the other people will say type:

sudo gedit /etc/fstab

However, I tried that and it did not find the right file (comments please).

So I just typed gedit and open it the whimpy GUI way (Success!). Since, I was already in Super User mode (sudo su) I have root permission to do ANYTHING in Gedit, including saving the file after I change it. Wonderful! Before you change or corrupt anything, save the fstab as backup called... fstab.bak.

Make the backup by using the whimpy GUI way and choose the familiar “Save As” and add "bak" to the file fstab.bak. Check to see it is there with a .bak extension. Geeks will tell you to use the terminal cp command, but we novices cannot find the file in terminal to get this far in the first place. Just like any word processor, after the “save as” command, your current file is now the fstab.bak. Open the real one you had before, and then close the backup file. Double check to see you are editing the real fstab with no extension. If you edit the backup file, nothing will happen when you reboot, plus your backup copy will not be a real backup of the original.

My original fstab file looks like this:

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/mmcblk1p6 during installation
UUID=a611601a-6ea8-4e34-9859-42a2470cac29 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/mmcblk0p1 during installation
UUID=00F2-C0CE  /boot/efi       vfat    umask=0077      0       1
# swap was on /dev/mmcblk1p5 during installation
UUID=8fb20159-62e4-41a3-8550-791c7a66c328 none            swap    sw              0       0

The last line (I made red for you), has the original swap info that is used probably on boot up time. From the previous blkid command, I could see my new drive UUID. Copy that phrase by highlighting the text in the terminal and then right clicking and choosing copy. Paste that phrase into another separate editor without su rights as a scratch pad (you can use Libre Office, and geeks can use nano). This step helps you get the right text easily without the quotes so you can paste easily into the fstab file. You want to change this file as little as possible.


So, my new UUID is listed in red above. All I need to do is change the uuid for the swap. If you have no swap drive, then copy my line in the above fstab file and then add the proper uuid from the previous blkid command. (choose the right uuid please!)
If this is your first Swap line, make sure you includes the complete line with the extra stuff

none            swap    sw              0       0

Do not worry about the exact spacing so much.

My new file looks like this: Notice I added a comment above my change too The # character means it is a comment.

# /etc/fstab: static file system information.
# Use 'blkid' to print the universally unique identifier for a
# device; this may be used with UUID= as a more robust way to name devices
# that works even if disks are added and removed. See fstab(5).
# <file system> <mount point>   <type>  <options>       <dump>  <pass>
# / was on /dev/mmcblk1p6 during installation
UUID=a611601a-6ea8-4e34-9859-42a2470cac29 /               ext4    errors=remount-ro 0       1
# /boot/efi was on /dev/mmcblk0p1 during installation
UUID=00F2-C0CE  /boot/efi       vfat    umask=0077      0       1
# swap was on /dev/mmcblk1p5 during installation  NOW changed to mmcblk0p5
UUID= 709ea197-e6fb-428b-9c1f-4b258452b10c  none            swap    sw              0       0

Do not paste the quotes. Do not have spaces after the = sign.

Save your file.

Open it again, just to be sure your changes saved. Close the gedit and terminal with dangerous su rights that geeks will scold you for doing (because it is easy work with... and easy to destroy things too).

REBOOT and and cross your fingers!

I rebooted and then I checked to see if my swap was working by looking at system performance app. I still had a Swap drive! But was it the new one or the old one? I could not verify my new drive was actually loaded because the new one is the same size as the old drive. Therefore, I typed: swapon (DO NOT TYPE ANYTHING MORE THAN SWAPON and it will only list the swap for you: safe). Now I see my new drive on mmblk0p5 which matches the UUID work I did on fstab. My slim 32gb main drive took effect:

This concludes this tutorial from a novice user who has never done this before. Half of the reason I made this tutorial was so the Geeks who help us (and thank you) can see how to help novice users. I suspected the previous answers were misleading and decided to make a tutorial as I did my work... By guessing from the other answers which were useful, I concluded that all I needed to do was edit the fstab flle and reboot. All worked well and I still have windows 10 and have not wiped my system yet. If anyone knows how to share a Windows pagefile.sys as a Linux swap file, I would be interested since 2 gb is taken for that in Windows and hd space is very pricy on my 32 GB machine.