UPDATE: 2023-Nov-15, Back in August, a PR I submitted to the apt-manage project was accepted, it includes a quick-install script which will install apt-manage on Debian and Ubuntu and their derivitives.
Once again, I've modified my answer below, replacing the installation with the quick-install script.
UPDATE: 2023-May-16, I can confirm apt-manage is the safest most consistent way I found to manage apt sources with keys properly.
I've rewritten the answer, removing my personal script (you can view it in the revisions history if you'd like) to focus on the command and not my personal code.
All of the answers here are great, and I've learned a lot from them.
Per @mesterlion comments, as of this time there is a single tool that does all of this, and does it correctly, properly maintained by the Pop!_OS team.
That tool is apt-manage
which is part of RepoLib module which introduced the key
sub-command in version 2.
So in Pop!_OS 22.04, installation will look like this:
sudo apt install --yes apt-manage
The upstream projects (Ubuntu and Debian) are missing this package, for that: the apt-manage
project has a quick-install script (full disclosure, I submitted the PR for it).
Installing will look like:
curl https://raw.githubusercontent.com/pop-os/repolib/HEAD/quick-install.sh | bash
More details can be found in the Installation section of the git-repo's README.md
.
Example 1
After installing apt-manage
, below is an example of a set of calls from my system:
# Adding PPA
sudo apt-manage add --terse --format=sources ppa:ppa:alessandro-strada/ppa
# Adding APT source and assinging a key
sudo apt-manage add --terse --format=sources --name Microsoft --identifier packages-microsoft-com 'deb [arch=amd64] https://packages.microsoft.com/ubuntu/22.04/prod jammy main'
# Assigning a key via URL
sudo apt-manage key packages-microsoft-com --url=https://packages.microsoft.com/keys/microsoft.asc
# R-Project needed some additional love - because they do something "different":
sudo apt-manage add --terse --format=sources --name R-Project --identifier cloud-r-project-org 'deb [arch=amd64] https://cloud.r-project.org/bin/linux/ubuntu jammy-cran40/ ""_""'
# Specific to R-Project, as one of the values are empty and the command line can't pass it effectively.
sudo sed -i /etc/apt/sources.list.d/cloud-r-project-org.sources -Ee 's/""_""//g'
# Assigning a key via fingerprint
sudo apt-manage key cloud-r-project-org --fingerprint=E298A3A825C0D65DFD57CBB651716619E084DAB9
# After all were added, run update.
sudo apt update
Example 2: Initialize docker on a fresh Ubuntu compute instance
set -e
export DEBIAN_FRONTEND=noninteractive
apt-get update \
&& apt-get install --yes --no-install-recommends \
apt-transport-https ca-certificates \
wget gnupg software-properties-common lsb-release \
curl jq bash-completion
has() { command -v "$1" > /dev/null; }
# Try differnt ways of installing apt-manage
# First let's hope there is package ready for installation
has apt-manage \
|| curl https://raw.githubusercontent.com/pop-os/repolib/HEAD/quick-install.sh | bash
# If the repo isn't part of existing apt sources, add it
REPO_URL="https://download.docker.com/linux/ubuntu"
APT_NAME="$(
apt-manage list -a \
| grep -B4 "${REPO_URL}" | head -1 \
| sed -Ee 's/^\W*([^:]+):$/\1/'
)"
if [[ -n "${APT_NAME}" ]]; then
>&2 printf '%s\n' \
'' \
"$(apt-manage list "${APT_NAME}")" \
'' \
"Docker repository found [${APT_NAME}]. Skipping."
else
apt-manage add --identifier download-docker-com --name Docker \
--terse --format=sources \
"$( printf "deb [arch=%s] %s %s %s" \
"$(dpkg --print-architecture)" \
"${REPO_URL}" \
"$(lsb_release --codename --short)" \
"stable"
)"
apt-manage key download-docker-com \
--url=https://download.docker.com/linux/ubuntu/gpg
fi