Skip to content

Instantly share code, notes, and snippets.

@karimlahlou
Last active August 19, 2026 18:06
Show Gist options
  • Select an option

  • Save karimlahlou/73bd46d59f0c850ed4681310065570ee to your computer and use it in GitHub Desktop.

Select an option

Save karimlahlou/73bd46d59f0c850ed4681310065570ee to your computer and use it in GitHub Desktop.
Automated fix for Apple T2 MacBook Pro hardware (keyboard, trackpad, Touch Bar, audio) on Ubuntu.
#!/usr/bin/env bash
#
# fix-t2-macbook.sh
# Script to restore Apple T2 drivers, kernel, Touch Bar, and audio support after an OS upgrade.
#
set -euo pipefail
# Ensure the script is run with root privileges
if [ "${EUID}" -ne 0 ]; then
echo "[!] This script must be run with sudo or as root."
echo " Usage: sudo bash $0"
exit 1
fi
# Detect current user (if run via sudo)
TARGET_USER="${SUDO_USER:-$USER}"
# Detect Ubuntu release codename
if command -v lsb_release >/dev/null 2>&1; then
CODENAME="$(lsb_release -cs)"
elif [ -f /etc/os-release ]; then
# shellcheck source=/dev/null
. /etc/os-release
CODENAME="${VERSION_CODENAME:-noble}"
else
CODENAME="resolute"
fi
echo "=========================================================="
echo " Setting up Apple T2 drivers for Ubuntu ($CODENAME) "
echo "=========================================================="
echo "[1/7] Installing prerequisites..."
apt update -y
apt install -y curl gpg ca-certificates lsb-release
echo "[2/7] Configuring T2 Linux APT repository..."
mkdir -p /etc/apt/trusted.gpg.d
curl -s --compressed "https://adityagarg8.github.io/t2-ubuntu-repo/KEY.gpg" | gpg --dearmor --yes -o /etc/apt/trusted.gpg.d/t2-ubuntu-repo.gpg
cat <<T2EOF > /etc/apt/sources.list.d/t2.list
deb [signed-by=/etc/apt/trusted.gpg.d/t2-ubuntu-repo.gpg] https://adityagarg8.github.io/t2-ubuntu-repo ./
deb [signed-by=/etc/apt/trusted.gpg.d/t2-ubuntu-repo.gpg] https://github.com/AdityaGarg8/t2-ubuntu-repo/releases/download/${CODENAME} ./
T2EOF
echo "[3/7] Updating package lists..."
apt update -y
echo "[4/7] Installing T2 kernel, Touch Bar (tiny-dfr), and audio config..."
DEBIAN_FRONTEND=noninteractive apt install -y linux-t2 tiny-dfr apple-t2-audio-config
# Ensure user groups for Touch Bar and audio hardware access
if [ -n "${TARGET_USER}" ] && id "${TARGET_USER}" >/dev/null 2>&1; then
echo "[*] Adding user '${TARGET_USER}' to video, audio, input groups..."
usermod -aG video,audio,input "${TARGET_USER}"
fi
# Setup default tiny-dfr configuration if not already present
mkdir -p /etc/tiny-dfr
if [ -f /usr/share/tiny-dfr/config.toml ] && [ ! -f /etc/tiny-dfr/config.toml ]; then
cp /usr/share/tiny-dfr/config.toml /etc/tiny-dfr/config.toml
fi
if [ -f /etc/tiny-dfr/config.toml ]; then
sed -i "s/MediaLayerDefault = false/MediaLayerDefault = true/" /etc/tiny-dfr/config.toml
sed -i "s/DoublePressSwitchLayers = 0/DoublePressSwitchLayers = 250/" /etc/tiny-dfr/config.toml
fi
echo "[5/7] Installing Touch Bar Suspend/Resume fix..."
mkdir -p /usr/lib/systemd/system-sleep
cat << "HOOKEOF" > /usr/lib/systemd/system-sleep/touchbar-sleep.sh
#!/bin/sh
case "$1" in
post)
sleep 1
systemctl restart tiny-dfr.service 2>/dev/null || true
;;
esac
HOOKEOF
chmod +x /usr/lib/systemd/system-sleep/touchbar-sleep.sh
cat << "UNITEFO" > /etc/systemd/system/touchbar-resume.service
[Unit]
Description=Restart Touch Bar tiny-dfr on wake from sleep
After=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
[Service]
Type=oneshot
ExecStartPre=/bin/sleep 1
ExecStart=/bin/systemctl restart tiny-dfr.service
[Install]
WantedBy=suspend.target hibernate.target hybrid-sleep.target suspend-then-hibernate.target
UNITEFO
systemctl daemon-reload
systemctl enable touchbar-resume.service 2>/dev/null || true
echo "[6/7] Configuring GRUB kernel boot parameters..."
GRUB_CONFIG="/etc/default/grub"
if [ -f "$GRUB_CONFIG" ]; then
# Add intel_iommu=on iommu=pt pm_async=off if missing
for param in "intel_iommu=on" "iommu=pt" "pm_async=off"; do
if ! grep -q "$param" "$GRUB_CONFIG"; then
echo "[*] Adding kernel parameter: $param"
sed -i "s/GRUB_CMDLINE_LINUX_DEFAULT=\"\([^\"]*\)\"/GRUB_CMDLINE_LINUX_DEFAULT=\"\1 $param\"/" "$GRUB_CONFIG"
# Clean up potential duplicate spaces
sed -i 's/ */ /g' "$GRUB_CONFIG"
fi
done
echo "[*] Updating GRUB..."
update-grub
fi
echo "[7/7] Done!"
echo "=========================================================="
echo " T2 driver configuration complete. "
echo " Please restart your MacBook if this is a first install: "
echo " sudo reboot "
echo "=========================================================="
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment