Skip to content

Instantly share code, notes, and snippets.

@chessmango
Last active May 9, 2024 10:13
Show Gist options
  • Save chessmango/904dbfd1c9bc25b7a474eb2fe9219ff1 to your computer and use it in GitHub Desktop.
Save chessmango/904dbfd1c9bc25b7a474eb2fe9219ff1 to your computer and use it in GitHub Desktop.
Simple Bash add-apt-repository drop-in

Things are awkward in the apt world while apt-key is deprecated and things like Launchpad and general PPA usage are in flux.

This snippet attempts to replicate add-apt-repository functionality for sanity, probably quite badly. There's no error checking or anything advanced, but at least the script is easy to understand.

Syntax: [sudo] addaptrepo.sh <repo-string> <gpg-key-fingerprint>

Then follow up with [sudo] apt-get update. I'm not making that call for you :P

You can find the gpg key fingerprint for a Launchpad repo by expanding Technical details about this PPA.

Example, using a PPA provided by AppImageLauncher:

sudo addaptrepo.sh ppa:appimagelauncher-team/stable ACD802F535B6F55D365285574AF9B16F75EF2FCA

Not sure I like the idea of pulling the fingerprint straight from Launchpad with this script, so I'll leave that to you to fork/implement :P

#!/bin/bash
set -eo pipefail
# https://gist.github.com/chessmango/904dbfd1c9bc25b7a474eb2fe9219ff1
# USAGE: [sudo] addaptrepo.sh <repo-string> <gpg-key-fingerprint>
if [ "$EUID" -ne 0 ]
then echo "Please run as root, or using sudo"
exit 1
fi
if [ -z "$1" ] || [ -z "$2" ]; then
echo "Usage: $(basename $0) <repo-string> <gpg-key-fingerprint>" >&2
echo "See https://gist.github.com/chessmango/904dbfd1c9bc25b7a474eb2fe9219ff1"
exit 1
fi
PPA_REPO_UNSTRIPPED="$1"
PPA_REPO_STRIPPED="${PPA_REPO_UNSTRIPPED#ppa:}"
PPA_USER="${PPA_REPO_STRIPPED%%/*}"
PPA_REPO="${PPA_REPO_STRIPPED#*/}"
PPA_FINGERPRINT="$2"
gpg --quiet --homedir /tmp --no-default-keyring --keyring /usr/share/keyrings/"$PPA_USER"-archive-keyring.gpg --keyserver hkps://keyserver.ubuntu.com --recv-keys "$PPA_FINGERPRINT"
echo "deb [signed-by=/usr/share/keyrings/$PPA_USER-archive-keyring.gpg] https://ppa.launchpadcontent.net/$PPA_USER/$PPA_REPO/ubuntu $(lsb_release -sc) main" | tee /etc/apt/sources.list.d/"$PPA_USER"-"$PPA_REPO".list >/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment