How to add swap after system was installed

i installed kde neon like a week ago with some problems but nothing i couldnt really handle

except a problem i had in recent days, while watching videos in youtube i had system to almost froze, cursor still moved a bit and all of a sudden, firefox was closed and i got a message saying system avoided running out of memory

i did a small search on that problem and found that it was kde showing systemd giving that message, i indeed have not much ram, only have 8 gbs and the gpu takes 2 afik

i never saw that message on linux and decided to install and run gparted, the os does not have a / or a home or a swap partition

i assume that swap runs now like pagefile.sys does on windows, on a file

but i think it is not working for me well enough, so i want to add a real partition, how can i do that after having the os already installed?

if i cant, how can i customize the partition creation next time? this system has the ext4 with everything and a fat32 for the efi stuff

what does swapon say?

a fresh install should at least give you a half GB of swap file to prevent a hard crash.

making a new swapfile (you can have more than one) is easy to do and enable (you must specify a size)

or if you have unallocated room on the disk you can create a swap partition and configure it with one of the partition managers.

if the disk is occupied but you have unused space in one of your partitions, you can shrink the partition to create the unallocated space you need for the step above.

i would recommend 12GB of swap space either way so that you can suspend/resume without issues.

This should help you out. You do not need a partition, you just need to create a fille. The major caveat is you need to use bit values to create the swap file, and this can be a bit fiddly. I recommend that your swap file be equal to the installed memory. In your case, 8gb. There is nothing preventing you from making it bigger though, such as 16gb. Its performance will depend on the speed of the ssd you install it on.

https://www.howtogeek.com/455981/how-to-create-a-swap-file-on-linux/

thank you for your reply

the installation process as i mentioned created the fat 32 for uefi and the rest went for ext4, around 240gbs

i tried to resize using gparted but it will not let me

i guess it is because the partition is mounted and i am running the os from that partition it doesnt let me, that is why i mentioned the second option that is reinstall

since this machine is not slow and i have very little stuff in it i can reinstall any moment and recover in like 20 minutes

to avoid not have a swap partition, what are the partitions i should have, / of course, the uefi and a swap for at least, 4 gbs? i have 8 gbs of ram but gpu takes 2 gbs

my question is focused on how to do it, i used to install linux in a very different way and i stopped using it and now the uefi seems to be quite different to what it used to be so i left the install process to erase and partition by itself but it made a hard crash a couple hours ago doing that out of memory stuff s i guess swap in file for me is not working, this time it didnt recovered so i just turned off pc

so i think that reinstall is the best path and i want to learn how to partition i modern linux by hand in the install process

you need to run gparted from a live USB in order to touch the partition your file system is running on… that’s like trying to pull the rug out from under you while you are standing on it.

i always recommend a separate partition for /home and separate partitions for backups (preferably on a separate disk) as well as a separate partition for games so they can be easily backed up separate from the rest of your data (also makes them portable).

and i use suspend/resume with hibernation so i always find using a separate partition to be more stable and reliable.

if you choose to reinstall (might be easier at this point) you will get to the part where is asks you where to put the OS

choose the manual or “something else” option so it will put you into a partition management view of your system disks (much like gparted).

from there you can create and allocate partition space as you see fit and assign the mount points for each of the parts you want to keep separate… something akin to this for a 240GB disk

 1GB    fat32    /boot
75GB    ext4     /   
75GB    ext4     /home
75GB    ext4     /backup|games
12GB    ext4     /swap

thank you

in the past i just left around 600mbs unallocated iirc because the distro i used back then wasnt that clear or perhaps i didnt understood well enough the partition used by uefi, a swap of around the same amount of my ram or 1.5 times bigger and the rest at / since i had another hard disk with my other files on a ntfs partition

the installer i imagine, used that free space to make the uefi and used my other settings, i guess i can do the same now but instead of leave the space empty, i can just create a fat32 partition there

the size is what i dont know what is best here, this machine does not dual boot, so i dont care about windows at all

i was told many years ago that the uefi partition should be 500mbs, the one in my pc is just 300mbs, but you recommend 1 gb

which value is the safest?

sorry i didnt saw your reply, the pc was sleeping and saw your other reply first

it says this

NAME TYPE SIZE USED PRIO
/swapfile file 512M 0B -2

thank you for your reply, sorry i didnt saw it the pc was suspended and didnt showed me your reply, from the previous comment i have the file, installer in fact created it but is too small, just 512mbs, i guess it is easier to just reinstall pc with a swap partition that is at least 12 gbs

The better bet is to just make add a second bigger swapfile… the system can use as many as you want. There is no speed difference with modern FS, and a file is vastly more flexible; you can make it bigger or smaller as needed without needing to mess with partitions. It is not hard to do. I change mine all the time (occasionally 3D rendering projects get memory leaks, and I will throw more swap at it to see if its infinite and where it ends up, for example).

To make an 8gig do this in a term one line at a time:

sudo dd if=/dev/zero of=/home/swapfile bs=1024 count=8048576
(it will take a few moments)
sudo mkswap /home/swapfile
sudo chmod 600 /home/swapfile
sudo swapon /home/swapfile

Then open the FSTAB:
kate /etc/fstab
Add this to the bottom as a new line:
/home/swapfile none swap sw 0 0
Reboot.
Test it with System Monitor.

1 Like

thank you for your reply

i ran the commands you shared and when i run swapon i get this

NAME TYPE SIZE USED PRIO
/home/swapfile file 7,7G 0B -3
/swapfile file 512M 0B -2

so i understand it worked

if i need to reinstall i will try to create those as partitions

i will run the pc for a couple days to see how it runs

1 Like

Your desire to learn is laudable, but slightly misplaced in this matter.

Swap is a very old-school solution to inadequate hardware, and swap partitions were preferred with old hard drives for performance reasons, but modern kernels have minimised this difference (though many old-school users are stuck in their ways).

  • Swap partitions are required for hibernation on some distributions.

  • Partitions are inflexible and wasted (reserved) space if you do not hibernate.

  • Swapfiles are flexible, easily resized and removed with commands.

  • Space efficient - uses only needed space, resizing dynamically as required.

  • Simpler.

  • Negligable performance overhead.

  • Not always supported by all distributions for hibernation.

sudo fallocate -l 4G /swapfile  # Create 4GB file
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

Check it out:

swapon --show
free -h` # should show 'Swap' line.

Alternatives:

⮞ swapon --show
NAME       TYPE      SIZE USED PRIO
/swapfile  file        4G   0B   -2
/dev/zram0 partition  16G 5.3G  100

I have 16 GiB installed and a swapfile 4GiB.

Your choices are as follows (IMO from worst to best)

  1. Swap partition
  2. Swap file
  3. zswap
  • You only need huge swap if you hibernate.
  • If you have 1TB RAM, then maybe you won’t need swap.

If you have 8GiB RAM, then you should have 3GiB Swap with no hibernation, 20GiB with hibernation, maximum 32GiB.

Swap - Manjaro is an interesting resource.

Warning - this guide is not intended for BTRFS.

thank you for your reply

i want to learn not just to make swap because as you say us old users know how swap works, more for the uefi partition in reality, reinstal linux is easy these days, very few time needed

about hibernation, i only have the option to let it sleep, which i personally find useful enough but is not perfect, if i am watching youtube shorts, the pc will not go into sleep because video is playing

i was told recently, dont remember where, that hibernation is not a good idea on linux for stability and that is why is disabled, perhaps is just for the lack of swap partition? as you see lack of information or misinformation is abundant, or who asks doesnt know, or who answers thinks that knows

i use this pc as a htpc and daily driver for online stuff, watch youtube and stuff like that, when i fall asleep i let it sleep but if i have the mouse at my hand it will wake up again, i want it this way, but if i am asleep during night it has to sleep again so it doesnt waste electricity, it is connected to my tv on my room

hibernation in my case doesnt sound useful, am i wrong there? should i leave swap as a file as it is now or as a partition so it can hibernate if it is useful for me?

why 1GB… meh, it’s a nice round number and i’ll never miss that 500MB.

good luck with your new swapfile.

lol, thank you

everything going well so far