Last updated on

How to switch Ubuntu to darkmode at sunset

TL;DR: Grab the auto-darkmode-script from my Github.

The Problem

I want my Ubuntu 22.04 and Ubuntu 24.04 installations to switch to a light theme at sunrise, and to a dark theme at sunset. Automatically. And while we are at it, set the preferred theme flag for websites, too.

This isn’t currently possible, though.

Prior Solutions

I found some scripts that worked by querying weather.com periodically and setting the theme based on the time of day.

I had some issues with these solutions:

  • They all required an Internet connection
  • They had to run periodically
  • They didn’t switch the theme precisely at sunrise and sunset

My Solution

So, I wrote my own theme-switcher script. You provide it with your location (latitude and longitude) and specify the themes you want to switch between.

It utilizes systemd to schedule when the script is being executed: upon user login, at sunrise, and at sunset.

It uses hdate to calculate the times of sunrise and sunset based on the entered location and the current date.

Features:

  • Runs offline
  • Runs only twice a day (plus however often you log in to your PC)

Check it out on Github!

Future Work

For laptops, some form of GPS location integration would be cool.