How to Create Swap Partition in Ubuntu After Installation
The SWAP partition is an essential feature to get the best performance and stability, especially in a PC with smaller RAM. In a nutshell, SWAP space is like adding a RAM, but not a physical one, but allocating some amount of hard disk space as RAM – like a virtual RAM!
It makes sense to make a SWAP file for those who run resource-intensive applications like video convention, virtual machines, etc.. You don't want RAM to overflow, and the system becomes non-responsive. It is possible that you initially assumed that you don't need SWAP and completed the Ubuntu/Linux Mint installation, but now you figured out that you need SWAP too.
No worries, you don't need to reinstall the entire operating system again to add the SWAP partition. You can add the SWAP anytime.
Creating a SWAP partition in Ubuntu, Linux Mint and derivatives
METHOD 1: Command-line Way from Terminal (Fastest way!)
STEP 1: First step is to check if by chance is there any SWAP partition already created in your PC. Use the see 'swapon' command:
sudo swapon --show
Enter your root password. If you see no output, it means that the SWAP doesn't exist.
STEP 2: Next, let's see the current partition structure of your computer's hard disk. Use the 'df' command:
df -h
You should see the partition structure like the one I got in my test PC.
STEP 3: Now it's time to create the SWAP file. Make sure you have enough hard disk space. It's a matter of preference on how much SWAP size you need.
Typically, it is good to have twice as much as your RAM for the SWAP size. But you can even go ahead with exactly as much as your RAM – your choice.
In this example, I choose to go with twice that of RAM. I have my test PC with 2 GB memory. So, I will set 4 GB as SWAP. Run the following command. Use your number instead of 4:
sudo fallocate -l 4G /swapfile
STEP 4: SWAP file is created. Let's give root-only permissions to it.
sudo chmod 600 /swapfile
STEP 5: Mark the file as SWAP space:
sudo mkswap /swapfile
STEP 6: Finally enable the SWAP.
sudo swapon /swapfile
STEP 7: You can now check using the same swapon command to check if SWAP is created.
sudo swapon --show
STEP 8: Also recheck the final partition structure.
free -h
STEP 9: Once everything is set, you must set the SWAP file as permanent, else you will lose the SWAP after reboot. Run this command:
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
That's it! Exit the terminal.
You can launch the 'System Monitor' utility and check SWAP status. It should be enabled!
METHOD 2: GUI Way using GParted
STEP 1: First thing to know before you do anything is to figure out the SWAP size. Typically, it is good to have twice as much as your RAM. But you can even go ahead with exactly as much as your RAM – your choice. In this example, I choose to go with twice that of RAM. I have my test PC with 2 GB memory. So, I will set 4 GB as SWAP.
STEP 2: Once you figured out the size of SWAP space, it's time to create a partition of that size. You need to be careful here. Any wrongdoing here may mean a significant data loss! So for beginners, I would recommend taking a backup of your data to an external hard disk drive before moving ahead.
You can create a partition in several ways, but I recommend GParted, free software for partitioning and formatting needs, and is available from Software Center of Ubuntu/Linux Mint/elementary OS. But since you can't resize and create a new partition on an active partition on which you are currently running Linux, I suggest you create a GParted Live USB drive and come back to this tutorial for next step. Alternatively, you can use an Ubuntu or Linux Mint Live USB drive too because it comes packaged with GParted.
STEP 3: Boot your PC into the GParted Live USB drive. Ubuntu/Linux Mint live USB users need to start GParted after booting into live desktop.
STEP 4: In the GParted window, select the Partition which you want to resize. Right-click on it and choose 'Resize'.
STEP 5: You can drag the box and resize the partition. Or enter the SWAP size in MB in the box 'Free space following (MiB). For example, I have added 4000 to indicate 4 GB partition.
Resizing a current partition will automatically create an 'Unallocated' new partition which is going to be the SWAP partition. Note that the actions you are doing now on GParted are not applied immediately. It processes all the commands as a batch process when you initiate it. But that's at last. Proceed as follows.
STEP 6: You will see unallocated file system of the size you created. Right-click on it and select 'New'. In the context menu, select 'File system' as 'linux-swap'. You can enter 'SWAP' in the 'Label' field just for easier identifying.
STEP 7: Click 'Add'.
STEP 8: Click 'Apply'. Now GParted will shrink the partition and then add a new SWAP partition.
STEP 9: After the operations are complete, click 'Close'.
STEP 10: You should now see the new Linux-Swap partition that you created.
STEP 11: Once everything is set, you must set the SWAP file as permanent, else you will lose the SWAP after reboot. Select the SWAP and then click 'Partition' > 'Swapon'. You should see a lock added.
STEP 11: Close GParted and click the 'Exit' button on the GParted desktop and then reboot into your Linux system.
Your Linux system will automatically recognize the SWAP partition you have created! You don't need to do anything. You can confirm if SWAP is active from the 'System Monitor'.
Launch 'System Monitor' which is included in Ubuntu and Linux Mint by default (elementary OS users need to install it from AppCenter) and check the resources tab. You should see SWAP active.
How to Create Swap Partition in Ubuntu After Installation
Source: https://www.fosslinux.com/1064/how-to-create-or-add-a-swap-partition-in-ubuntu-and-linux-mint.htm