GuidesRunning & maintaining

GitHub CLI: install gh, gh auth login, and tokens for agents

Install gh on Ubuntu, macOS or Windows, run gh auth login on a headless server, where the token is stored, and a fine-grained token for a coding agent.

December 4, 2026The Everpod team
The short answer

GitHub CLI is GitHub’s official command-line tool, gh: pull requests, issues, Actions runs and repositories from the terminal. As of December 2026 the current release is 2.106.1 (December 4). Install it with brew install gh on a Mac, winget install --id GitHub.cli --source winget on Windows, and from GitHub’s own apt repository on Debian or Ubuntu, not Ubuntu’s package, which is a version the maintainers call broken. Then run gh auth login: on a server it prints a one-time code that you enter at github.com/login/device from any browser. That login can read and write every repository your account reaches; for a coding agent, a fine-grained token limited to its repositories, in GH_TOKEN, is the narrower choice.

Installing gh

The project’s Linux install page separates the packages GitHub maintains from the ones it does not, and the difference matters on Ubuntu. sudo apt install gh on a stock Ubuntu 24.04 installs 2.45.0 from Ubuntu’s archive, and 26.04 carries 2.46.0; the gh team says the community 2.45.x and 2.46.x builds are “broken due to deprecated GitHub APIs”. One symptom users have reported from that package is a pull request command failing with “GraphQL: Projects (classic) is being deprecated in favor of the new Projects experience”. Use GitHub’s repository instead, which this block adds and installs from (it needs sudo):

(type -p wget >/dev/null || (sudo apt update && sudo apt install wget -y)) \
  && sudo mkdir -p -m 755 /etc/apt/keyrings \
  && out=$(mktemp) && wget -nv -O$out https://cli.github.com/packages/githubcli-archive-keyring.gpg \
  && cat $out | sudo tee /etc/apt/keyrings/githubcli-archive-keyring.gpg > /dev/null \
  && sudo chmod go+r /etc/apt/keyrings/githubcli-archive-keyring.gpg \
  && sudo mkdir -p -m 755 /etc/apt/sources.list.d \
  && echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | sudo tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
  && sudo apt update \
  && sudo apt install gh -y
Copied

After that, sudo apt update and sudo apt install gh keep it current. Fedora, RHEL, Amazon Linux and openSUSE have their own official repository on the same page, with commands for DNF5, DNF4, yum and zypper. The gh team recommends never installing it as a snap.

SystemInstallUpgrade
macOS
brew install ghbrew upgrade gh
Windows
winget install --id GitHub.cli --source wingetwinget upgrade --id GitHub.cli --source winget
Debian, Ubuntu, Raspberry Pi OS
The block abovesudo apt update, then sudo apt install gh
Anything else
A binary from the releases page

On Windows the installer changes your PATH, and Windows Terminal only sees it in a new window, not a new tab. Check what you have with gh --version.

gh auth login, question by question

Run in a terminal, gh auth login asks four things, in this order as of 2.106 (the third only if you chose HTTPS). Each has a flag, which is how you answer it in a script or when an agent runs the command:

It asksChoicesFlag
Where do you use GitHub?
GitHub.com, or Other (Enterprise Server)--hostname github.com
How would you like to authenticate GitHub CLI?
Login with a web browser, or Paste an authentication token--web, or --with-token
What is your preferred protocol for Git operations on this host?
HTTPS or SSH--git-protocol https
Authenticate Git with your GitHub credentials?
Yes makes gh git’s password source for HTTPSRun gh auth setup-git afterwards

HTTPS or SSH. With HTTPS, git sends the same token gh holds, so one sign-in covers both. With SSH, gh looks for a key, offers to generate one, and uploads it to your account, which adds the admin:public_key scope to the login; --skip-ssh-key leaves your keys alone. The protocol also decides how gh repo clone and gh pr checkout address the remote.

Browser or token. The browser login is GitHub’s device flow and gives gh an OAuth token with the repo, read:org and gist scopes. Pasting a token is meant for a classic personal access token: the manual names all three as its minimum, while gh’s own prompt asks for only repo and read:org. For a fine-grained one, see below.

On a server with no browser

Over SSH, pick the browser option anyway. gh prints a one-time code and then asks you to press Enter to open github.com in your browser; on a server there is none, so it reports “Failed opening a web browser” and tells you to enter the URL manually. Open https://github.com/login/device on your laptop or phone, type the code, approve, and the server’s gh finishes on its own. Run with no terminal attached, as when an agent runs it, gh skips the questions and prints the two lines directly:

! First copy your one-time code: XXXX-XXXX
Open this URL to continue in your web browser: https://github.com/login/device
Copied

The command waits until you approve, so an agent that runs it has to show you that code; it cannot finish the login itself.

Where gh keeps the token

gh tries the system keyring first: Keychain on a Mac, Credential Manager on Windows, and on Linux a Secret Service keyring such as GNOME Keyring, under the name gh:github.com. A server usually runs neither, and then gh writes the token in plain text to ~/.config/gh/hosts.yml (or $XDG_CONFIG_HOME/gh, or GH_CONFIG_DIR) and says so at the end of the login:

! Authentication credentials saved in plain text
Copied

--insecure-storage asks for that file on purpose. gh auth status shows which account is signed in and where its token is kept. Two things surprise people. In current releases, gh auth logout revokes the token the browser login gave gh as well as removing it from the machine, so any copy of it elsewhere stops working too; a token of another kind, such as one you pasted, is only removed locally, with a warning, and --no-revoke keeps the logout local. Older releases, Ubuntu’s 2.45 among them, only removed it locally. And GH_TOKEN, when set, “takes precedence over previously stored credentials”, per gh’s environment reference, so a forgotten export in a shell profile silently replaces whatever you just logged in with; gh auth login refuses to run at all while it is set:

The value of the GH_TOKEN environment variable is being used for authentication.
To have GitHub CLI store credentials instead, first clear the value from the environment.
Copied

gh auth setup-git, and the workflow scope

gh auth setup-git makes gh git’s credential helper for every host gh is logged in to, so git push over HTTPS asks gh instead of prompting for a password. With no host logged in, it stops with “You are not logged into any GitHub hosts. Run gh auth login to authenticate.” It is the fix for the could not read Username for 'https://github.com' error on a machine where gh is already signed in.

The browser login’s scopes do not include workflow, which GitHub requires to “add and update GitHub Actions workflow files”. So a push that touches .github/workflows/ through gh’s token is refused. As users report it, the push fails with a [remote rejected] line that says GitHub is “refusing to allow” an OAuth App, or a Personal Access Token where you pasted one, “to create or update workflow” files without the workflow scope. The fix a gh maintainer gives in gh’s issue tracker adds the scope to the stored login:

gh auth refresh -h github.com -s workflow
Copied

It sends you through the browser sign-in again, and keeps the scopes the login already had. On an agent’s machine, think first about whether the agent should be able to change what your CI runs.

The commands you will use most

All of these act on the repository of the folder you are in; add -R OWNER/REPO to point anywhere else.

CommandWhat it does
gh repo clone OWNER/REPO
Clones over your chosen protocol; for a fork, adds the parent as upstream
gh pr create --fill
Opens a pull request from the current branch, title and body from its commits; offers to push the branch first if needed
gh pr create --title "…" --body-file notes.md --draft
The same with no prompts, as a draft; --base picks the target branch
gh pr checkout 123
Checks out pull request 123 locally; --worktree PATH puts it in its own worktree
gh pr view --comments
The current branch’s pull request with its comments and reviews; --web opens it in a browser
gh pr checks --watch
The pull request’s CI checks, refreshed until they finish; exit code 8 means still pending
gh run watch --exit-status
Follows an Actions run to the end and fails if the run did
gh run view --log-failed
Prints only the log of the failed steps
gh issue create --title "…" --body "…"
Opens an issue; --assignee "@me" assigns it to you

Without --title and --body (or --fill), gh pr create and gh issue create prompt for them, which is where an agent’s run hangs. gh pr create --dry-run prints what it would open, but its manual warns it “may still push git changes”.

gh for a coding agent on a server

Coding agents run gh as your Linux user, with whatever sign-in that user has. Claude Code’s /ship command, for example, needs GitHub CLI 2.63 or later signed in, and its docs say plainly that “Claude uses your gh and git sign-ins, so its work on GitHub appears under your name”; the guide to Claude Code and git covers its pull request flow, and the guide to Codex and GitHub Codex’s. With the browser login, that sign-in has the repo scope, which GitHub describes as “full access to public and private repositories”: every repository of yours and of every organisation you belong to, open to anything that can steer the agent, a planted instruction in an issue included.

A fine-grained personal access token narrows that to the repositories you pick and the permissions the agent’s work needs. As of December 2026, the API rows below are from GitHub’s permissions reference; that reference lists API calls, not git itself, and Contents is the permission that covers the repository’s files and branches:

The agent shouldRepository permission
Clone and pull
Contents: read
Push branches
Contents: read and write
Open pull requests (gh pr create)
Pull requests: read and write
Open issues
Issues: read and write
Read Actions runs and logs (gh run list, gh run view)
Actions: read
Re-run a failed run
Actions: read and write
Change files in .github/workflows/
Workflows: read and write, only if you want that

GitHub can pre-fill the form from a link. This one asks for a 30-day token named coding-agent with the first five rows above; replace OWNER with your username or the organisation that owns the repositories, and drop any permission you do not want:

https://github.com/settings/personal-access-tokens/new?name=coding-agent&target_name=OWNER&expires_in=30&contents=write&pull_requests=write&issues=write&actions=read&metadata=read
Copied

No link parameter chooses repositories, so on the form select Only select repositories and tick the agent’s ones before generating. expires_in takes 1 to 366 days, per GitHub’s token page, which also lists what fine-grained tokens cannot do yet: reach more than one owner at once, call the Checks API, or contribute to public repositories where you are not a member, which rules out pull requests from a fork to someone else’s project.

That Checks gap shows up in gh. The gh run watch manual says it “does not support authenticating via fine grained PATs”, because no such token can be given checks:read. An agent can still list runs and read failed logs with gh run view; following a run live needs the broader login.

gh’s manual recommends GH_TOKEN for fine-grained tokens rather than --with-token, which it says can behave confusingly when gh reaches for something outside the token’s scope. This keeps the token off the screen and out of your shell history, then points git at gh as well:

printf 'Paste the token, then press Enter: '
read -rs GH_TOKEN
export GH_TOKEN
gh auth setup-git
gh auth status
Copied

Start the agent from that same shell, inside tmux if it should outlive your SSH session: the variable lives only in that shell and what it starts, and is gone after a reboot, so nothing is written to disk. gh counts a host with a token in the environment as logged in, which is why setup-git accepts it (a reading of gh’s source, not a documented promise); git then gets the token from gh whenever it runs inside that environment. If you would rather the token survive restarts, the guide to GitHub access for a coding agent on a server stores it with --with-token and compares it with a per-repository deploy key, and the guide to securing a VPS for Claude Code and Codex covers keeping one agent away from another’s credentials.

Whichever way the token arrives, the agent can read it: anything that lets gh work lets gh auth token or echo $GH_TOKEN print it. Narrow scopes and an expiry are what limit the damage, which is the case for the fine-grained token over the login.

Run Claude Code, Codex, OpenCode, Pi, Hermes or OpenClaw on an always-on developer pod.

A developer pod is a permanent cloud computer (VPS), set up and secured for you, with your pick of Claude Code, Codex, OpenCode, Pi, Hermes and OpenClaw installed. Run more agents at once than your laptop can hold, and keep them working when it’s closed. From $24 a month, built in about ten minutes.