Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Unixten/4483ec6e435fe6d7e2cb6dfac3f8ccd0 to your computer and use it in GitHub Desktop.
Save Unixten/4483ec6e435fe6d7e2cb6dfac3f8ccd0 to your computer and use it in GitHub Desktop.
Linux Laptop Optimizations

Everything I do in this guide is mostly taken from the Arch Wiki, and is for Arch Linux, obviously this can probably be applied to other Linux distributions especially Arch based ones, this guide is for people who want a laptop with similar effciency they had on Windows or MacOS. I hate the excuse of having to compromise on Linux to have good battery or thermals on laptops.

Please think of this guide as more of a starting point, if you're serious about fully optimizing your laptop research your laptop and the hardware inside of it as that can get you even further down the rabbit hole.

This guide assumes you have a relatively modern laptop with at least an SSD from the factory, if you don't, don't worry you can still probably follow this guide perfectly, if you have 32-bit laptop I'm using 64-bit packages only, but there should be 32-bit packges in the Arch multilib repo, just don't assume everything will work or not break your laptop, please read carefully, and don't copy and paste commands or edit config files blindly and research everything before you do it, I won't be responsible for a broken OS or even broken hardware. This warning applies to modern laptops too.

This guide is not for people who want to get the most out of their battery by disabling stuff like Bluetooth, or by limiting their hardware or OS.

Everything in this guide is biased to my laptop with an Intel CPU and GPU, I'll leave the Arch Wiki pages for AMD and NVIDIA laptops where it's needed so people with that hardware aren't left out, for dual GPU laptops I've left Arch Wiki pages on how to enable GPU switching at the bottom of this guide please consider enabling it as thermals and battery life will improve significantly.

Please uninstall all your power saving packages if you plan on following this guide because they'll cause conflicts. (Unless its Thermald or power-profiles-daemon)

I'll keep updating this guide if I find anything new to improve efficiency. Please point out any errors, mistakes or inconsistencies.


Step 1. Kernel and Drivers

  • When picking what kernel to use (Latest or LTS) the latest kernel will most likely have better performance and battery life, especially on newer hardware, and the LTS kernel will have better stability and reliability, depending on your devices age you might be better off with the LTS kernel for stability reasons. (For AMD CPU devices you'll want kernel 5.17+ for the AMD p-state driver, it improves performance and power saving).

run sudo pacman -S linux-firmware if you haven't installed it.

As of 01/21/2022 the linux-firmware package was split into multiple packages to reduce disk space usage, research your hardware and see what you might need to additionally install. [1]


Step 2. Wayland

For KDE run sudo pacman -S plasma-wayland-session (if you are an NVIDIA user, also install egl-wayland, and if the session does not start with the proprietary NVIDIA driver, also enable the DRM kernel mode setting),[1] then select the session from your login screen.

On Gnome, Wayland should be enabled by default.

For other desktop environments I'll leave the Arch Wiki page for desktop environments.


Step 3. Early KMS

  • From the Arch Wiki "KMS is typically initialized after the initramfs stage. However, it is possible to enable KMS already during the initramfs stage."
  • From the Arch Wiki "KMS also enables newer technologies (such as DRI2) which will help reduce artifacts and increase 3D performance, even kernel space power-saving."
  • Early KMS can fix hibernation issues on Intel GPU's. [1]
  • I'm not sure how early KMS works with dual GPU's laptops with mixed brands please look into that before doing this.
  • This step is for Intel GPU's, I've left the Arch Wiki page for other GPU brands below, it should be similar to what is done here.
  • You'll need early KMS for step 9.

run sudo nano /etc/mkinitcpio.conf and change the "MODULES=" to how it's shown below.

# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=(piix ide_disk reiserfs)
MODULES=(i915)

Save and exit.

run lsmod | grep intel_agp If the module is not loaded run sudo mkinitcpio -P then reboot the laptop, and continue to step 4. If the module is loaded continue this step down below.

run sudo nano /etc/mkinitcpio.conf and change the "MODULES=" to how it's shown below.

# vim:set ft=sh
# MODULES
# The following modules are loaded before any boot hooks are
# run.  Advanced users may wish to specify all system modules
# in this array.  For instance:
#     MODULES=(piix ide_disk reiserfs)
MODULES=(intel_agp i915)

Save and exit.

  • From the Arch Wiki "Intel users may need to add intel_agp before i915 to suppress the ACPI errors (check the output of lsmod on your running system to see if intel_agp is loaded). This may be required for resuming from hibernation to work with a changed display configuration. If you use PRIME GPU with Intel IGP being your primary GPU and AMD as the discrete one, adding intel_agp may lead to troubles when resuming from hibernation (monitor gets no signal). See [2] for details."

run sudo mkinitcpio -P

Reboot your laptop.


Step 4. Hardware Acceleration

  • From the Arch Wiki "Hardware video acceleration makes it possible for the video card to decode/encode video, thus offloading the CPU and saving power."
  • This step is for Intel GPU's, I left Arch Wiki pages for other GPU brands below, it should be similar to what is done here.
  • For dual GPU laptops enabling GPU switching will use your iGPU for light work and dGPU for heavy work, the power savings can be significant, I left Arch Wiki pages for GPU switching at the bottom on this post.
  • After you've set everything up I recommend seeing this Arch Wiki page to enable hardware acceleration in applications.

run lspci | grep VGA to see your GPU

For Intel GPU's 2014 and newer run sudo pacman -S intel-media-driver libvdpau-va-gl libva-utils vdpauinfo intel-media-sdk | From the Arch Wiki "HD Graphics series starting from Broadwell (2014) and newer are supported by intel-media-driver."

For Intel GPU's 2013 and older run sudo pacman -S libva-intel-driver libvdpau-va-gl libva-utils vdpauinfo intel-media-sdk | From the Arch Wiki "GMA 4500 (2008) and newer GPUs, including HD Graphics up to Coffee Lake (2017) are supported by libva-intel-driver."

run export LIBVA_DRIVER_NAME=iHD | If you installed intel-media-driver.

run export LIBVA_DRIVER_NAME=i965 | If you install libva-intel-driver.

run export VDPAU_DRIVER=va_gl

run vainfo to confirm everything is working.

run vdpauinfo to confirm everything is working.

If everything is working run sudo nano /etc/environment and add "LIBVA_DRIVER_NAME=iHD" or "LIBVA_DRIVER_NAME=i965" (Depending on the hardware acceleration driver you've installed). Then add "VDPAU_DRIVER=va_gl", how it is shown below.

#
# This file is parsed by pam_env module
#
# Syntax: simple "KEY=VAL" pairs on separate lines
#
# 
# Hardware Acceleration / GPU 
LIBVA_DRIVER_NAME=iHD or LIBVA_DRIVER_NAME=i965
VDPAU_DRIVER=va_gl
MESA_LOADER_DRIVER_OVERRIDE=iris  |  If your Intel GPU is gen 8+

Check if your GPU and hardware acceleration driver can decode VP9 if it can't install the h264ify browser extension, [1] [2] otherwise you won't be able to watch videos with hardware video decoding, keep in mind h264ify will disable resolutions above 1080p.


Step 5. Undervolting

  • WARNING UNDERVOLTING CAN BRICK YOUR LAPTOP PERMANENTLY IF YOU ACCIDENTALLY OVERVOLT IT MAKING IT COMPLETELY UNUSABLE I WONT BE RESPONSIBLE IF YOU DO BRICK YOUR LAPTOP.
  • From Arch Wiki "Undervolting is a process where voltage to CPU is reduced in order to reduce its energy consumption and heat without affecting performance. Note that most desktop motherboards allow tweaking CPU voltage settings in BIOS as well."
  • From Arch Wiki "It is no longer possible to undervolt Intel processors with modern BIOSes and microcode, due to the changes needed to patch the Plundervolt vulnerability."
  • There is a method to undervolt K10 and newer AMD CPUs, I'll leave a link to the Arch Wiki page.
  • Do not use this guide to undervolt your AMD CPU.

run sudo pacman -S intel-undervolt

For kernels 5.9+ you need to add "msr.allow_writes=on" as a kernel parameter to your boot loader I'll leave the Arch Wiki page for adding kernel parameters to your boot loader. After you added the kernel parameter reboot your laptop.

run sudo nano /etc/intel-undervolt.conf

Decrease the CPU mV by -10 how it's shown below.

undervolt 0 'CPU' -10
undervolt 1 'GPU' 0
undervolt 2 'CPU Cache' 0
undervolt 3 'System Agent' 0
undervolt 4 'Analog I/O' 0

Save and exit, then run sudo intel-undervolt apply && sudo intel-undervolt read and make sure it applied properly.

Next stress you'll test for system stability as undervolting can cause a system to crash or hang or even become unbootable, run yay -S mprime-bin

run mprime

Stress test for 30 minutes, then decrease the mV by -10 for every successful run until your laptop crashes or hangs, once you find the sweet spot you can enable the undervolt service run sudo systemctl enable intel-undervolt.service and please make sure your system runs stable before enabling this service as it can result in an unbootable or unstable system.


Step 6. Hibernation Suspend and Swap

  • Swap makes memory management for your laptop easier,[1] [2] [3] if you're worried about SSD writes I'll show you how to reduce swappiness or turn off swapping (not recommended) so you can use swap for just hibernation.
  • If you plan on swapping with your partition or swapfile enable zswap, from the Arch Wiki "Zswap is a kernel feature that provides a compressed RAM cache for swap pages. Pages which would otherwise be swapped out to disk are instead compressed and stored into a memory pool in RAM. Once the pool is full or the RAM is exhausted, the least recently used (LRU) page is decompressed and written to disk, as if it had not been intercepted. After the page has been decompressed into the swap cache, the compressed version in the pool can be freed."
  • For hibernation to work you need a swap, I'll show you how to enable hibernation for a swap partition and leave the swap file Arch Wiki page, swap files on btrfs are also different I'll leave the Arch Wiki page.
  • People using LVM2 I wrote a separate guide below as it's slightly different.
  • Suspend should work without configuring anything, I'll show you a possible fix and leave trouble shooting Arch Wiki pages below it if it doesn't work properly.

To enable hibernation run sudo blkid and grab the swap partitions "UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"

Next add "resume=UUID=xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" as a kernel parameter to your boot loader here is the Arch Wiki page for adding kernel parameters to yours.

For LVM2 users run sudo lsblk and check where the swap partition is located.

Next add "resume=/dev/YourVolumeGroup/yourLogicalVolume" as a kernel parameter to your boot loader here is the Arch Wiki page for adding kernel parameters to yours.

Next configure the initramfs I'll copy and paste how to configure it from the Arch Wiki since it does a better job at explaining than I can.

run sudo nano /etc/mkinitcpio.conf

  • From the Arch Wiki "When an initramfs with the base hook is used, which is the default, the resume hook is required in /etc/mkinitcpio.conf. Whether by label or by UUID, the swap partition is referred to with a udev device node, so the resume hook must go after the udev hook. This example was made starting from the default hook configuration: HOOKS=(base udev autodetect keyboard modconf block filesystems resume fsck)"
  • When an initramfs with the systemd hook is used, a resume mechanism is already provided, and no further hooks need to be added.
  • Note: LVM users should add the resume hook after lvm2.

run sudo mkinitcpio -P and reboot.

  • I'll show you how to decrease or turn off swapping here, if you're doing this to reduce SSD writes and not for a specific reason please read. [1] [2] [3] IMO it's better to have better system performance at the sacrifice of an unnoticeable amount of SSD longevity. Personally I wouldn't touch swappiness or vfs cache pressure as the defaults work fine for most set up and touching them can actually slow your laptop down.

run sudo nano /etc/sysctl.d/99-sysctl.conf

add vm.swappiness = "Number"

  • From the Arch Wiki "The swappiness sysctl parameter represents the kernel's preference (or avoidance) of swap space. Swappiness can have a value between 0 and 200 (max 100 if Linux < 5.8), the default value is 60. A low value causes the kernel to avoid swapping, a high value causes the kernel to try to use swap space, and a value of 100 means IO cost is assumed to be equal. Using a low value on sufficient memory is known to improve responsiveness on many systems."

add vm.vfs_cache_pressure = "Number"

  • From the Arch Wiki "Another sysctl parameter that affects swap performance is vm.vfs_cache_pressure, which controls the tendency of the kernel to reclaim the memory which is used for caching of VFS caches,versus pagecache and swap. Increasing this value increases the rate at which VFS caches are reclaimed. [1] For more information, see the Linux kernel documentation."

After you finished this step the config file should look something to what is shown below.

vm.swappiness = Number
vm.vfs_cache_pressure = Number

Here are the Arch Wiki pages to fix suspend or hibernate not working consistently or if your laptop wakes up from suspend instantly. Running echo XHC > /proc/acpi/wakeup fixed my suspend issues and I'll show how to make that a permanent solution later in this tutorial. In step 12


Step 7. Thermald (Intel Only)

  • Thermald prevents throttling, if you feel your laptop won't throttle you don't have to install this, I still recommend it as a "just in case".
  • From Arch Wiki "thermald is a Linux daemon used to prevent the overheating of Intel CPU's. This daemon monitors temperature and applies compensation using available cooling methods."
  • From Arch Wiki "By default, it monitors CPU temperature using available CPU digital temperature sensors and maintains CPU temperature under control, before hardware takes aggressive correction action. If there is a skin temperature sensor in thermal sysfs, then it tries to keep skin temperature under 45C."

run sudo pacman -S thermald

run sudo systemctl enable --now thermald


Step 8. CPU Power Management

  • The purpose of this CPU power saving guide isn't to just to reduce thermals it's to make the fans spin up less in turn saving battery and having a more comfortable experience. Keep in mind this can reduce performance.
  • If you want to just disable your CPU's turbo boost feature see step 12.

For Gnome and KDE run sudo pacman -S power-profiles-daemon then enable it by running sudo systemctl enable --now power-profiles-daemon if no settings appear for setting the CPU's power profile reboot your laptop.

From the Arch Wiki "power-profiles-daemon conflicts with other power management services such as TLP, tuned and system76-power. To use one of the aforementioned services instead without uninstalling power-profiles-daemon (due to its potential status as a dependency), disable the power-profiles-daemon service by masking it (see also [1], [2])."

I'm unsure how other desktop environments handle CPU power management so I'll leave an Arch Wiki page for desktop environments.

For desktop environments that don't support CPU power management I'll add how to set your CPU's power profile in step 12.


Step 9. GPU Power saving

  • For this whole step you would've needed to enable early KMS from step 3.
  • If you don't have an Intel GPU don't follow the steps I give here, instead use the Arch Wiki pages below for your brand of GPU.
  • All of these options might cause serious stability issues on many laptops especially older ones please enable and test the options one by one to see what works and what might not, if you're using X.org tuning some settings might fix these issues I'll leave the Arch Wiki page. There is also possible stability fixes in the Arch Wiki Intel page below

run sudo nano /etc/modprobe.d/i915.conf

add options i915 enable_guc=2

  • From the Arch Wiki "On supported Skylake and newer processors, some video features (e.g. CBR rate control on SKL low-power encoding mode) may require the use of an updated GPU firmware, [3] which is loaded by default only on Gen 11+ hardware since Linux 5.4. [4] Enabling GuC/HuC firmware loading can cause issues on some systems; disable it if you experience freezing (for example, after resuming from hibernation)."

add options i915 enable_fbc=1

  • From the Arch Wiki "Making use of Framebuffer compression (FBC) can reduce power consumption while reducing memory bandwidth needed for screen refreshes."

add options i915 enable_psr=1

  • "allows the display to go to lower standby states when system is idle but display is on as it eliminates display refresh request to DDR memory completely" [1]

add options i915 enable_rc6=1

  • "RC6 is a technology which allows the GPU to go into a very low power consumption state when the GPU is idle (down to 0V)." [1]

After you finished this step the config file should look something to what is shown below.

options i915 enable_rc6=1
options i915 enable_psr=1
options i915 enable_guc=2
options i915 enable_fbc=1

Step 10. Audio Power saving

  • This can cause your audio to make crackling noises or your microphone to make popping noises.

run lspci -k and make a note of the audio kernel driver.

run sudo nano /etc/modprobe.d/audio_powersave.conf

add options snd_hda_intel power_save=1 or options snd_ac97_codec power_save=1 | Depending on the audio kernel driver.

  • From the Arch Wiki "By default, audio power saving is turned off by most drivers. It can be enabled by setting the power_save parameter; a time (in seconds) to go into idle mode. To idle the audio card after one second."

  • Arch Wiki page for audio power saving.


Step 11. Wireless Card Power saving

  • From the Arch Wiki "these power saving options are experimental and can cause an unstable system."

run lsmod | grep '^iwl.vm' and make a note of the result.

run sudo nano /etc/modprobe.d/iwlwifi.conf

add options iwlwifi power_save=1 add options iwlmvm power_scheme=3 or options iwldvm force_cam=0 | Depending on the lsmod result.

After you finished this step the config file should look something to what is shown below.

options iwlwifi power_save=1
options iwlmvm power_scheme=3 or options iwldvm force_cam=0

Step 12. Power Management Script

  • You can have an easier time by running sudo pacman -S powertop. I'll leave the Arch Wiki page.
  • What I did is make a script to apply the same optimizations PowerTop does but I added more that PowerTop doesn't.
  • DO NOT BLINDLY COPY AND PASTE THIS, READ THE INSTRUCTIONS.

run sudo nano /usr/bin/Laptop-Optimizations then see what you need from the lines below (yes you do need the "#!/bin/sh" part) below and please keep in mind these lines below are for my laptop specifically so you'll need to edit it to make it specific for your laptop, which shouldn't hard and if you don't need to enable some stuff in this script like "echo XHC > /proc/acpi/wakeup" don't. Not all laptops are the same and you should check what you need to and don't need to enable.

#!/bin/sh
echo XHC > /proc/acpi/wakeup

echo enabled | tee /sys/bus/usb/devices/{1-7,1-8,usb1,usb2}/power/wakeup (I've been having issues with suspend and this was the culprit use at your own risk).

echo auto > /sys/block/sda/device/power/control 

echo auto > /sys/bus/usb/devices/1-8/power/control

echo powersave > /sys/module/pcie_aspm/parameters/policy

echo auto | tee /sys/bus/i2c/devices/{i2c-0,i2c-1,i2c-2}/device/power/control

echo auto | tee /sys/bus/pci/devices/{0000:00:00.0,0000:00:1f.0,0000:01:00.0,0000:00:13.0,0000:02:00.0,0000:00:17.0}/power/control (Leave this at the bottom if the script, it caused me PCIe wakeup issues when it was at the top).

Save and exit.

  • If your desktop environment supports CPU power management you can skip this part and move on to the chmod command below, only do this part if your desktop environment doesn't support CPU power management, keep in mind this can reduce performance. This part is going to be for the intel_pstate driver only and my CPU specifically, I'll link the Arch Wiki page for other drivers and CPU's so you can edit this script for your CPU and driver.

run cat /sys/devices/system/cpu/cpufreq/{policy0,policy1,policy2,policy3}/scaling_available_governors && cat /sys/devices/system/cpu/cpufreq/{policy0,policy1,policy2,policy3}/energy_performance_available_preferences and make a note of the results. (Policy numbers will be relevant to how many threads your CPU has).

Add the lines you need into the #!/bin/sh script above, If you notice your laptop is slow or stuttering after this put the power profile up till it's not.

echo "your choice here" | tee /sys/devices/system/cpu/cpufreq/{policy0,policy1,policy2,policy3}/scaling_governor

echo "your choice here" | tee /sys/devices/system/cpu/cpufreq/{policy0,policy1,policy2,policy3}/energy_performance_preference

echo 1 > /sys/devices/system/cpu/intel_pstate/no_turbo | (This disables the turbo boost feature)

After you finished making your script run sudo chmod +x /usr/bin/Power_Management

Next make a systemd service that runs the script on boot so it's permanent, run sudo nano /etc/systemd/system/Laptop-Optimizations.service then copy and paste this in.

[Unit]
Description=Enables Laptop Optimizations

[Service]
Type=oneshot
ExecStart=/usr/bin/Laptop-Optimizations

[Install]
WantedBy=multi-user.target suspend.target hibernate.target hybrid-sleep.target

run sudo systemctl enable --now Laptop-Optimizations.service && sudo systemctl status Power_Management.service

(For SATA drives only). run cat /sys/class/scsi_host/host/link_power_management_policy* if the result isn't "med_power_with_dipm" here is the Arch Wiki page to changing it. (You probably can edit it into the script how its shown below).

From the Arch Wiki "It will become a default setting for Intel based laptops in Linux 4.16 [8]."

echo med_power_with_dipm > /sys/class/scsi_host/host*/link_power_management_policy*


Step 13. Improving performance

  • See these pages I won't write any guide since this is laptop specific.

Improving Performance

If you have an SSD the Arch Wiki page.


Step 14. CPU Microcode

  • From the Arch Wiki "Processor manufacturers release stability and security updates to the processor microcode. These updates provide bug fixes that can be critical to the stability of your system. Without them, you may experience spurious crashes or unexpected system halts that can be difficult to track down."
  • AMD or Intel CPU users should install microcode for their CPU.

Arch Wiki page for microcode.


Step 15. Reboot your laptop, look below for anything you might need, it can improve your laptop even further, I tried to include just about everything someone might need.

If you have NVMe storage.

Dynamic GPU Switching. | For laptops with dual GPU's.

NVIDIA Optimus | For laptops with dual GPU's.

PRIME | For laptops with dual GPU's.

Bumblebee | For laptops with dual GPU's. Arch Wiki "Bumblebee has significant performance issues."

Nvidia-xrun | For laptops with dual GPU's.

If you use an External GPU.

Power Management | This page has a lot this guide did, but I excluded some things.

Laptop | For specific laptop models or stuff like finger print scanners.

Laptop Mode | For HDD Laptops.

X.org | X.org does have options you can configure for your laptop, if you use X.org check this out.

OpenGL | Make sure you're using iris drivers on gen 8+ Intel GPU's.

Vulkan | Install this.

GPGPU/OpenCL | Caused instability try if you want to.

Fstab atime Options | Can reduce SSD writes and improve performance, google "your filesystem Arch Wiki" and an article should appear for your filesystem from there you can optimize it.

Tablet PC | For tablet laptops there is some useful features this can enable like sensors for device rotation.

iio-sensor-proxy | If your laptop has sensors like ambient light ones for auto brightness you'll want to install this package.

HiDPI | For HiDPI screens especially on X.org.

Touchscreen | If you have touch screen issues.

Touchpad Synaptics | If you have touch pad issues.

Security | It's a laptop so?

Variable Refresh Rate | Can improve battery life.

Hard Disk Shock Protection | From the Arch Wiki "There are several laptops from different vendors featuring shock protection capabilities. As manufacturers have refused to support open source development of the required software components so far, Linux support for shock protection varies considerably between different hardware implementations."

Audio Mute LED | If you have a mute LED and wish to enable it. (options snd-hda-intel model=mute-led-gpio fixed it for me on the 13-4103dx).

NTP | This can be useful for a laptop especially if you travel a lot with it.

Mobile broadband modem | If your device has a SIM card slot and you wish to use mobile data on the go.

GPS | Some laptops support GPS if you wish to enable it.

Device Geo Location Tracking | Once you enable GPS you can use GPS tracking, if you don't have GPS prey let's you track your device using other methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment