Better Battery Life on Ubuntu 17.10
For your Dell XPS 15 9560 or Nvidia Optimus notebook
Say, for a moment, that you’re like me in two particular respects: you’ve recently decided you’re going to take the leap and move over to Ubuntu full-time after a few years of administering Linux machines in the cloud, and you own a Dell XPS 15 9560 (or a similar Nvidia GPU-equipped laptop). You’ll probably have noticed one significant detail upon that squeaky-clean fresh install of Ubuntu 17.10:
The battery life sucks.
There’s good news and bad news. Bad news: power efficiency just isn’t as good under Linux, compared to when running Windows and Mac. Your mileage may vary, I’ve heard it from some that their battery life is just as good, but overall, for most hardware configs, you’re gonna lose out. But good news, there is a lot we can do to improve on that measly single-lunch-break-spanning battery estimation we’re seeing. I usually see 6–8 hours on battery utilising the integrated Intel graphics unit, and have the ability to restart my laptop and use the high-powered Nvidia graphics when I’ve got power nearby. Note that I have a Dell XPS 15 9560 with the FHD-display and smaller 56Whr battery, if you’re using the 4K-screen model with the larger 97Whr battery, I would expect your numbers to vary!
Disclaimer: I’m not awesome at Linux. I work with it more-or-less daily at work, but I’d describe myself as an intermediate. So the solution I have for you is a “best-effort” solution. It gets only most of the way there, and there is a lot of room for improvement, but it’s also not going to take you 4-hours to configure. If you have any advice for me, hit me up with a comment!
Our 3-step (ok, 4-step) plan to better battery life
There are 3 parts to our setup:
- TLP Power Management: the TLP project page introduces itself with the following:
“TLP brings you the benefits of advanced power management for Linux without the need to understand every technical detail. TLP comes with a default configuration already optimized for battery life, so you may just install and forget it.”
That bold bit sounds awesome, let’s do that. - PowerTOP: PowerTOP is a Linux tool released by Intel that helps us peek at a bunch of power-usage metrics on our system to see what’s sucking all our juice, and auto-configure more nifty power-saving settings.
- Configure Nvidia PRIME: this will let us switch to the integrated Intel graphics chip and turn off that power-hungry GTX 1050 when we don’t need it. This means big savings on battery.
- Have the right versions of things: the secret final step that no-one tells you about. But never fear, my steps will specify important version that are what I’m using right now as I type this. Feel free to try whatever versions of things you like, but your mileage will likely vary drastically.
To Bee, or not to Bumblebee…
Nvidia Optimus dual-graphics setups are a bit of a pain under Linux, and if you’ve done a bit of reading on this topic before finding my article, then you’ll have come across Bumblebee, a method for being able to selectively utilise the powerful dedicated GPU only when you want it. It looks awesome, there are a lot of resources out there, but I’ll be frank: I couldn’t get it working. Maybe that’s the hardware setup in the XPS 9560, maybe it’s Ubuntu 17.10, maybe it’s just that I’m not an advanced Linux user. Who knows. (if you know, let me know with a comment!)
I ended up working out a more simple (and yes, it has limitations) method for managing the dedicated GPU in the 9560 using just the Nvidia drivers.
Install Ubuntu 17.10
If you need to, go ahead and fresh install Ubuntu 17.10. I’ll wait here. When I installed, I needed to configure two BIOS settings:
- Secure Boot > Secure Boot Enable: turn this off
- System Configuration > SATA Operation: set this (from
Raid On
) toAHCI
Upgrade the Linux kernel and configure kernel parameters
If that title freaks you out, take a couple of deep breaths. It’s not that complicated, just follow the steps and you’ll be fine. I tried a couple of Linux kernel versions, the details of which I won’t bore you with, and settled on v4.13.10
, the proverbial Goldilocks.
mkdir ~/kernel-v4.13.10
cd ~/kernel-v4.13.10wget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13.10/linux-headers-4.13.10-041310_4.13.10-041310.201710270531_all.debwget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13.10/linux-headers-4.13.10-041310-generic_4.13.10-041310.201710270531_amd64.debwget http://kernel.ubuntu.com/~kernel-ppa/mainline/v4.13.10/linux-image-4.13.10-041310-generic_4.13.10-041310.201710270531_amd64.debsudo dpkg -i *.deb
With the new kernel version installed, we need to configure a particular kernel parameter to keep things rolling. Open up /etc/default/grub
with your text editor and edit the line starting with GRUB_CMDLINE_LINUX_DEFAULT
so it looks like the following:
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_rev_override=1"
Save the file, and run the following command to ensure the new config is rolled into our bootloader, ready for next time we restart.
sudo update-grub2
Restart your machine to make sure everything is still looking good with the new kernel configuration.
Back? Good, let’s go on.
Install TLP and PowerTOP
These guys are really easy on Ubuntu 17.10, just install the packages, enable the tlp
service, and run powertop --auto-tune
to configure some more power-saving magic:
sudo apt-get update
sudo apt-get install tlp tlp-rdw powertop
sudo tlp start
sudo powertop --auto-tune
Now if you run powertop
while on battery it should report a discharge rate of somewhere around 8–12W while idling (but may vary rapidly), and an estimated battery life of somewhere in the ballpark of 4–6 hours (when looking at these numbers remember that I have the FHD-display and smaller 56Whr battery!). Better, but we’re not done yet. What if we want to be able to use the Nvidia GPU?
Configuring Nvidia Optimus and PRIME on Ubuntu
First we add the Nvidia drivers PPA, and install the Nvidia driver (version 384.90, specifically):
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update
sudo apt-get install nvidia-384=384.90-0ubuntu3.17.10.1
sudo apt-mark hold nvidia-384
Reboot to allow the new drivers to take effect (thanks @yensonlau), and now if we run nvidia-smi
we should get some output like the following, which indicates our driver is configured correctly:
Running powertop
(no autotune flag now) should now report a battery discharge of somewhere in the realm of 15–25W (2–4 hours), because we have the dedicated GPU active. Oops. Let’s install Nvidia PRIME, which gives us the prime-select
command we can use to switch between our power-saving integrated intel
graphics and the dedicated nvidia
graphics:
sudo apt-get install nvidia-primesudo prime-select intel
nvidia-smi # errors about missing drivers: GTX 1050 is disabledsudo prime-select nvidia
nvidia-smi # info output about the GPU
The Gotcha
When swapping back from nvidia
to intel
using prime-select
, you’ll need a system restart to have the dedicated card correctly “switch off” to a low power-state and see that 6–8 hours of battery life:
sudo prime-select intel# system restart nvidia-smi # gives error: nvidia card is off
sudo power-top # using 6-12W, 6-8 hours batterysudo prime-select nvidia
nvidia-smi # outputs GPU info
sudo power-top # using 15-25W, 2-4 hours batterysudo prime-select intel
nvidia-smi # gives error: nvidia card is "off"
sudo power-top # using 15-25W, 2-4 hours battery# system restartsudo power-top # using 6-12W, 6-8 hours battery
Quite frankly, I haven’t bothered to do anything about this just yet, sorry. I rarely have need for powerful graphics acceleration, so I’m perfectly happy to be stuck needing a restart to switch out of it, and enjoy the improved battery life the rest of the time. If you’ve got a solution for me, let me know in the comments and I’ll add it in here!
Thanks for reading, and enjoy better battery life on your Dell XPS 15 with Ubuntu!
If you want a more compact version of this post, check out the Gist.