Bash profile function to fix the APT key signature error when using third party repositories in Ubuntu

So you've added a third party repository in your APT source.list and are faced with a GPG error when attempting to install a package from it:

W: GPG error: http://ppa.launchpad.net karmic Release: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 5A9BF3BB4E5E17B5

Here's a quick Bash function to put in your ~/.bashrc. It will grab the missing public key.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
getppakey() {
 
 if [ $(id -u) == "0" ]; then
  # gpg will complain about unsafe perms if ~/.gnupg 
  # is not owned by the user running the command
  echo Error: must not run this as root
 else
  local ks="keyserver.ubuntu.com"
   if [ "$1" == "" ]; then
    echo Error: please supply the key listed in the NO_PUBKEY error
   else
    gpg --keyserver $ks --recv-keys $1 && \
    gpg --export --armor $1 | sudo apt-key add -
   fi
 fi
 
}

Function in action:

$ getppakey 5A9BF3BB4E5E17B5
gpg: requesting key 4E5E17B5 from hkp server keyserver.ubuntu.com
gpg: key 4E5E17B5: "Launchpad PPA for chromium-daily" not changed
gpg: Total number processed: 1
gpg:              unchanged: 1
OK

Leave a comment

NOTE: Enclose quotes in <blockquote></blockquote>. Enclose code in <pre lang="LANG"></pre> (where LANG is one of these).