DEV Community

Micah Wade
Micah Wade

Posted on • Originally published at policypackets.au on

How to Setup Ubuntu Minimal With the Lowest Resource on Your Server

What is Minimal Compute

This guide is aimed at servers with minimal compute for this i consider anything with less than 2 cores or less than 4GB of ram. For the guide I will be using the free compute Oracle gives you which is 1/8th of a CPU and 1GB of memory. If you are running this on your own hardware I would recommend installing Alpine Linux instead. The reason I made this guide is for people who are running their server in the cloud because they may not have the option of using Alpine. This is what I did when setting up the Ubuntu Server that this is running on.


The Two Constraints

So when we are optimising there are two main constraints: one is CPU it can only do so many instruction, two would be memory there is only so much which can be stored.

Memory

Swap

When dealing with minimal memory we want to make sure the server does not crash, because it ran out of memory even if that means it's slower. To alleviate this we will create a swap, which means when the memory is close to full it will move the least important things onto the disk. While the disk is slower it can save the server from crashing. Due to us only creating 2GB and we are doing this with a cloud VPS the Drive it uses will be plenty fast the main issues with be latency.

First we will reserve 2G for the swap:

sudo fallocate -l 2G /swapfile

Enter fullscreen mode Exit fullscreen mode

Then will make sure it has the read/write permissions:

sudo chmod 600 /swapfile

Enter fullscreen mode Exit fullscreen mode

Next we Need to tell the Linux kernel that this file is for virtual memory:

sudo mkswap /swapfile

Enter fullscreen mode Exit fullscreen mode

Next we need to activate the swap file:

sudo swapon /swapfile

Enter fullscreen mode Exit fullscreen mode

In order to confirm this worked you can run:

free -h

Enter fullscreen mode Exit fullscreen mode

If it worked you should have something like this:

              total used free
Mem: 952Mi 188Mi 137Mi
Swap: 2.0Gi 23Mi 2.0Gi

Enter fullscreen mode Exit fullscreen mode

Great now this is working but it won't stick around if we reboot. In order to fix that issue we need to mount this swap file in our fstab:

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

Enter fullscreen mode Exit fullscreen mode

Snap

Snap is a package manger mostly only used by Ubuntu most servers don't use snap. If you don't intend to use snap you can remove it to get a small memory reduction.

To start we want to remove any snaps installed:

snap list | awk 'NR>1{print $1}' | xargs -I {} sudo snap remove {}

Enter fullscreen mode Exit fullscreen mode

Now that everything installed through snap is gone you can remove it through apt:

sudo apt purge -y snapd

Enter fullscreen mode Exit fullscreen mode

That should have got rid of everything but snap has some dependences which may not get removed. To remove these we will scan for the packages that were installed as a dependency but are no longer needed:

sudo apt autoremove --purge -y

Enter fullscreen mode Exit fullscreen mode

Well done you have now deleted snap.

If you ever want to use snap you can install it with:

sudo apt update && sudo apt install -y snapd

Enter fullscreen mode Exit fullscreen mode

CPU

Disable Daily Upgrades

While Updating and Upgrading in the background can be good, if your server is already working to the max it can cause issues when you have little CPU power. But be careful if this is a public facing server i would recommend you setup a script to do updates when it has low CPU usage as this can be a security risk.

sudo systemctl disable --now unattended-upgrades.service
sudo systemctl disable --now apt-daily.timer
sudo systemctl disable --now apt-daily-upgrade.timer

Enter fullscreen mode Exit fullscreen mode

If you Disable these make sure to be running updates and upgrades regularly:

sudo apt update && sudo apt upgrade -y

Enter fullscreen mode Exit fullscreen mode

AppArmor

AppArmor is great and you should probably not disable it. But if your trying to get every tiny bit of performance and make sure all your other security is high it's most likely okay.

To disable AppArmor run this:

sudo systemctl disable --now apparmor

Enter fullscreen mode Exit fullscreen mode

Conclusion

Now as i stated above you should probably be using Alpine Linux. But if you insist on using Ubuntu this how you can need to reduce the base line system usage of Ubuntu.

Top comments (0)