gnupg2 always uses the agent (so the use-agent directive is not needed) and starts it automatically. You only need to manually start it if you also use it for ssh authentication because ssh doesn't know about it.
The most known way to start it is what Eric mentioned but there is a "newer" one too (it was mentioned in the mailing list some time ago but it was put in the manpage of gpg-agent too).
Code:
cat .gnupg/gpg-agent.conf
use-standard-socket
enable-ssh-support
If you do not use ssh keys then you do not need the enable-ssh-support directive (and if you use 2.1 version of gnupg2 then you don't even need the use-standard-socket directive because it is always used).
.zshrc or .bashrc or equivalent interactive shell startup file
Code:
unset GPG_AGENT_INFO
unset SSH_AGENT_PID
if [ "${gnupg_SSH_AUTH_SOCK_by:-0}" -ne $$ ]; then
export SSH_AUTH_SOCK="${HOME}/.gnupg/S.gpg-agent.ssh"
fi
The above code unsets the "old-way" variables and then tells ssh to use the S.gpg-agent.ssh socket in order to talk to the agent's ssh emulation (again only needed if you use ssh keys)
Code:
GPG_TTY=$(tty)
export GPG_TTY
I think this is only needed for curses and tty pinentry so that the password prompt is printed in the right terminal and is not needed for gtk/qt pinentry but i always set it anyway.
Code:
if [ -x "$(which gpg-connect-agent)" ]; then
gpg-connect-agent updatestartuptty /bye >& /dev/null
fi
The above code is again only needed for ssh support. As we said in the beginning, when you use gpg-agent for ssh, then you must start the agent manually because ssh doesn't know how to start it. The gpg-agent manpage tells us to run "gpg-connect-agent /bye" to start the agent. The code above does that and also tells the agent to update the tty that is uses to point to the current one (again i think it is not needed for gtk/qt pinentry but i have it anyway).
If you do not use ssh emulation then only put "use-standard-socket" and everything will work.
Edit: This way works only with gnugp2 and not with gnupg.