How To: Enable a Swapfile
Yeah, yeah… I have a modern, large SSD. I have more RAM than I’ll ever possibly use. I still want/enable swap, in this case a swapfile. Imagine my dismay when I installed Lubuntu 20.04 and found there’s no swap available during the basic installation? (It’s there in 21.04.)
Sure, I could have made a swap partition during the installation, but that didn’t seem like something I wanted to do – besides, I can always add it later. Which, of course, I did.
There are all sorts of views about whether swap is required in my situation, or in any situation, but I’m of the mind that disk space is cheap and my computer is faster than I am. So, if it has any chance of helping, it’s all good. It should also be mentioned that swap is far more complicated than ‘a place where the kernel sticks stuff when there’s no more RAM left’. In fact, it’s a lot more complicated than that. It’s where the kernel pages content that’s seldom used, and it’ll happily use swap even when there’s plenty of RAM available.
Since it’s lacking, let’s learn how to add a swapfile to Ubuntu (and official flavors) 20.04 and presumably other similarly aged variants. It’s a pretty painless process.
Like normal, let’s crack open your terminal emulator with
Now, let’s check to see if you’ve already got some swap going on.
If it shows nothing but a new line, you have no swap. If it says anything else, you’ve got swap enabled already and probably don’t need this article.
Just so you know, I personally just did this a couple of days ago, after upgrading to Lubuntu 20.04. So, I’m pulling this data from .bash_history.
Let’s make a swapfile.
[code]sudo fallocate -l 8G /swapfile[/code]
Why 8 gigabytes when I have ample RAM and an SSD? Because I never, ever want to worry about it again. I want to be able to open up every app I have and leave them open for a month. You do you and decide how big you want it to be!
Now, we need to set some permissions. We don’t want anyone writing to swap, we only want root writing to swap.
[code]sudo chmod 600 /swapfile[/code]
Next, we need to let the OS know that’s swap space.
[code]sudo mkswap /swapfile[/code]
And turn it on with:
[code]sudo swapon /swapfile[/code]
And you now have swap in the form of a swapfile and it’s turned on. I suppose we should make this permanent. To do this, we need to edit fstab and nano is a good tool for this.
[code]sudo nano /etc/fstab[/code]
And add this at the bottom of that document:
[code]/swapfile none swap sw 0 0[/code]
Those are the 0 digit, in case the font here makes it confusing. (I think I’ll try messing with the fonts.)
Either way, you should now have a swapfile that gets loaded on reboot and is currently loaded and working. You can next edit the swappiness value. In Ubuntu, it is a default of 60. If you want to edit it, you’ll have to wait for another article.
Like always, thanks for reading. It’s missing at the moment, or not working, or I’d say subscribe and get notifications of new articles. However, I’ll have to work on that. I just haven’t made the time to do so.