Make Ubuntu faster(maybe)

Make Ubuntu faster(maybe)

Windows is slow and terrible, we all know that. But what do you do when Ubuntu slows down? This mostly applies to old hardware, but can also help on low end, budget VPS hosting.

RAM

The biggest problem you’ll likely run into is running out of RAM on old hardware and low-end VPS’s. You don’t have the money to upgrade, or just don’t want to. So, what do you do? Easy, enable swap. Swap is disk-space(i.e. swapfile or swap partition) that Linux will use just as it uses RAM. Since disks are slower than RAM, this means that if important things are swapped out, your performance will suck. So, Linux tries to only put what isn’t as important on swap. The important stuff stays in RAM, and that chrome window with 15 YouTube tabs you forgot about goes to swap, if the system needs more RAM. If you have tons of RAM, you should still have swap, but your system won’t use it too much. This is not meant to be a replacement for enough RAM, but that didn’t stop me, and many other people from trying.

zSwap and zRAM

zSwap compresses the contents of swap, which can increase performance in some cases. I didn’t really notice that much of a difference, but that doesn’t mean it won’t work for you. I think the general idea is that the less data has to be transferred to and from the disk, the better the performance will be. It’s worth a try, so to enable it, simply follow this answer. If you’re too lazy to click on a link, here’s the important part:

to enable zswap permanently, edit the GRUB configuration file to include zswap.enabled=1 at the end of the GRUB_CMDLINE_LINUX_DEFAULT entry.

By the way, the GRUB config file is generally at /etc/default/grub. After updating this file, run update-grub as root(sudo).

If your system feels faster, great! If not, try the next option: zRam. zRam takes it a step farther from zSwap, and creates a compressed swap thingie in RAM. While having swap in RAM may seem counter-intuitive, it really isn’t in this case. Because the swap is compressed, it allows you to squeeze a bit more out of the RAM you already have, at the expense of a bit more processing power. Another upside to doing this in RAM is that no disk space is lost, something that might also be scarce on a budget VPS. Enabling this is even easier than zSwap, simply run:

sudo apt install zram-config

and then:

sudo service zram-config restart

and check if swap has been added with the free command.

Note: To customize zram-config, you can follow this.

This is what made my system faster when doing RAM-intensive tasks, and freezing was reduced to seconds from minutes.

earlyoom

For those of you that don’t know, the Linux kernel’s last defense against a full system crash is the OOM killer, a.k.a. Oout Of Memory killer. Once all other options are exhausted, and there is no more RAM or swap space, the kernel kills the task with the highest OOM score. The score is a bit complicated, so I won’t go into it now. The problem is that sometimes it can take forever before the OOM killer does something, and until it does, your system can be frozen and unusable. So, earlyoom was created. It runs in the background, and does pretty much the same thing as the OOM killer. The main difference is that it can be configured to run way before the OOM killer does, saving time. While you can manually install it, you can also use my script from my Script GitHub repository. Anyways, to use it, just run the following:

wget https://raw.githubusercontent.com/NerdOfLinux/Scripts/master/earlyoom.sh
sudo bash earlyoom.sh

Then, enter the percentages when asked:

Please enter the minimum percent of free ram(leave blank if unsure):
RAM%: 10
Please enter the minimum percent of free swap(leave blank if unsure):
Swap%: 50

Those are just example values, so feel free to change them. A good starting point is 10 for both, but I prefer a higher one for swap, because it can take quite a long time to fill up when the system begins swapping.

Stress testing

A good way to choose the best swap choice(zRam or zSwap, partition or file, etc.), is to run a stress test and see how well your system performs. My favorite stress testing tool is the command stress, which can be installed with:

sudo apt install stress

Then, to stress the RAM, use:

stress -m 8

8 does 2GB, because each one is 256M. You can change how much each one is worth with the --vm-bytes option. If your system freezes for more than a few seconds, then you should try another solution for swapping. earlyoom really helps out here, and should kill the stress process pretty quickly, or Google Chrome 🙂 .

Reboots

Luckily, unlike Windows, Ubuntu doesn’t require reboots that often. You have to reboot even less when you enable live kernel patching. But, you will have to eventually reboot, and that can be slow, especially on a 5400 rpm hard drive, like my laptop has. Sure, you can use e4rat, or e4rat-lite, but that takes a lot of time to setup. The easier solution is kexec-tools, which can be installed with:

sudo apt install kexec-tools

This makes reboots much shorter by loading the new kernel, or the same one, without doing a full system reboot, which bypasses the BIOS, or UEFI, or whatever else you have. While this does not makes normal boot-ups faster, it does make the reboot process shorter, so there’s no reason not to have it.

Desktop Environment

Another thing to consider is switching to a more lightweight desktop environment. The default one, Unity, or more recently, Gnome, can be quite taxing on older systems. So, you can install a more light weight one, such as LXDE or XFCE.

LXDE

This can be installed with:

sudo apt install lxde

for just the desktop, or

sudo apt install lubuntu-desktop

for the full light-weight feel of Lubuntu.

XFCE

You can install just the desktop environment with:

sudo apt install xfce4

or, the entire Xubuntu feel with:

sudo apt install xubuntu-desktop

Either choice will require logging out and selecting the new desktop environment. Both XFCE and LXDE are lightweight, and whichever one you choose is up to. I personally prefer XFCE, but you can choose either.

TL;DR

In case you skipped to the bottom, here’s the short version:

sudo apt install zram-config kexec-tools
sudo service zram-config restart
wget https://raw.githubusercontent.com/NerdOfLinux/Scripts/master/earlyoom.sh
sudo bash earlyoom.sh

This will make reboots faster, create a compressed swap device in RAM, and kill high-memory processes before your entire system goes down.

Notes

  • Swap is not a replacement for sufficient RAM, it is meant to work with it.
  • Do not install earlyoom if you regularly use high-memory applications, or just configure both percentages to 0
  • Save everything before running stress, in case you’re forced to do an unsafe shutdown and boot-up(press and hold the power button, or alt + sysrq + o).
  • Look at shell scripts before you run them, in case they have been tampered with or make any modifications you do not want.

Leave a Reply(Markdown is On)

%d bloggers like this: