DEV Community

zemse
zemse

Posted on • Edited on

Setup GPG on macOS

Install

brew install gpg
Enter fullscreen mode Exit fullscreen mode

Create new key

# generate key
gpg --full-generate-key

# get the public key using key ID
gpg --armor --export XXXXXX

# set the key ID in git
git config --global user.signingkey XXXXXXX

# always sign commits
git config commit.gpgsign true
Enter fullscreen mode Exit fullscreen mode

Setup keychain

gpg collects password from cli. This causes issues if using vscode to create a commit. So input can be taken from a popup or keychain.

brew install pinentry-mac
Enter fullscreen mode Exit fullscreen mode

The brew installation will print these caveats:

==> Caveats
You can now set this as your pinentry program like

~/.gnupg/gpg-agent.conf
    pinentry-program /opt/homebrew/bin/pinentry-mac
Enter fullscreen mode Exit fullscreen mode

So just create a ~/.gnupg/gpg-agent.conf file if it doesn't exist and put the line pinentry-program /opt/homebrew/bin/pinentry-mac in it.

Now, to check if it works.

1.gpg --list-keys to print the existing keys.

  1. pkill -TERM gpg-agent.
  2. Restart the terminal.
  3. echo test | gpg -e -r <PUT THE KEY ID HERE> | gpg -d

This should open a pin entry popup and make sure "save in keychain" option is selected.

More links

Documentation on GitHub for setup: https://docs.github.com/en/authentication/managing-commit-signature-verification

Top comments (1)

Collapse
 
jonathanyeong profile image
Jonathan Yeong

Great post! Helped me with my GPG setup