How to install and configure GitHub CLI (gh)?
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
Today we are going to discuss, how to install and configure GitHub CLI (gh).
What is GitHub CLI (gh)?
GitHub CLI also known as gh is GitHub on the command line. It brings pull requests, issues, and other GitHub concepts to the terminal next to where you are already working with git and your code.
Prerequisites:
- One Windows or Linux system with git
- One active GitHub account with Personal Access Token (PAT)
GitHub CLI (gh) Installation:
How to install GitHub CLI (gh) on MAC OS?
1 2 3 4 5 6 7 8 9 10 11 12 13 |
############################# ## GitHub CLI Installation ## ############################# ## ------ ## MAC OS ## ------ ## Using homebrew brew install gh ## Using MacPorts sudo port install gh |
How to install GitHub CLI (gh) on Windows OS?
1 2 3 4 5 6 7 8 9 10 |
## ---------- ## Windows OS ## ---------- ## Using chocolety choco install gh ## Using scoop scoop bucket add github-gh https://github.com/cli/scoop-gh.git scoop install gh |
How to install GitHub CLI (gh) on Ubuntu and Debian OS?
1 2 3 4 5 6 7 8 9 10 11 |
## -------------------- ## Ubuntu and Debian OS ## -------------------- curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | sudo dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/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 |
How to install GitHub CLI (gh) on RHEL, CentOS, and Fedora OS?
1 2 3 4 5 6 7 |
## -------------------------- ## RHEL, CentOS and Fedora OS ## -------------------------- sudo dnf config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo sudo dnf install gh |
How to install GitHub CLI (gh) on AWS EC2 instance with Amazon Linux 2 OS?
1 2 3 4 5 6 7 8 9 |
## -------------- ## Amazon Linux 2 ## -------------- ## Download the package wget https://github.com/cli/cli/releases/download/v2.5.2/gh_2.5.2_linux_386.rpm ## Install the package sudo rpm -i gh_2.5.2_linux_386.rpm |
How to install GitHub CLI (gh) on openSUSE OS?
1 2 3 4 5 6 7 8 9 |
## ----------- ## openSUSE OS ## ----------- sudo zypper addrepo https://cli.github.com/packages/rpm/gh-cli.repo sudo zypper ref sudo zypper install gh |
How to install GitHub CLI (gh) from source?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
## ----------------- ## Build from source ## ----------------- ## Install go https://go.dev/doc/install ## Clone source file git clone https://github.com/cli/cli.git gh-cli cd gh-cli ## Build on Linux # installs to '/usr/local' by default; sudo may be required make install # or, install to a different location make install prefix=/path/to/gh ## Build on Windows # build the `bin\gh.exe` binary go run script\build.go |
How to install GitHub CLI (gh) on Linux OS (other distributions)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
## ------------------------- ## Other Linux Distributions ## ------------------------- ## Arch Linux OS sudo pacman -S github-cli ## FreeBSD pkg install gh ## OpenBDS pkg_add github-cli ## Funtoo OS emerge -av github-cli ## Gentoo OS emerge -av github-cli ## Nix/NixOS OS nix-env -iA nixos.gitAndTools.gh ## Alpine Linux OS apk add github-cli ## Void OS sudo xbps-install github-cli |
Validate the GitHub CLI installation:
1 2 3 4 5 6 7 8 9 10 11 |
## ----------------------------------------- ## Validate the GitHub CLI (gh) installation ## ----------------------------------------- ## get gh version gh --version # or gh ## If returns any error then installation was not successful |
GitHub CLI (gh) authentication:
In order to interact with your GitHub account using gh, you first need to authenticate GitLab CLI (gh) to GitHub using your GitHub Personal Access Token (PAT). Alternatively, you can also use “gh auth login” command for browser based authentication.
How to authenticate GitHub CLI (gh)?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
############################### ## Authenticate gh to GitHub ## ############################### ## -------------------------------- ## Using environment variable (PAT) ## -------------------------------- ## Set environment variable export GITHUB_TOKEN=' ## Check if gh was successfully authenticated gh repo list ## should return all the repository in your GitHub account. ## --------------------------- ## Using gh auth login command ## --------------------------- gh auth login ## Check if gh was successfully authenticated gh repo list ## should return all the repository in your GitHub account. |
GitHub CLI (gh) configuration:
You can use “gh config” command or environment variables to configured different options for GitHub CLI (gh).
How to configure GitHub CLI (gh) using “git config”?
You can set below configuration options for GitHub CLI (gh) using “gh config set” command.
- git_protocol: the protocol to use for git clone and push operations (default: “https”)
- editor: the text editor program to use for authoring text
- prompt: toggle interactive prompting in the terminal (default: “enabled”)
- pager: the terminal pager program to send standard output to
- http_unix_socket: the path to a Unix socket through which to make an HTTP connection
- browser: the web browser to use for opening URLs
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
############################### ## Configure GitHub CLI (gh) ## ############################### ## --------------- ## Using gh config ## --------------- gh config set editor vim ## sets default editor gh config get editor ## returns vim gh config list ## list all config ## By default, gh stores the config file in below location cat $HOME/.config/gh/config.yml |
How to configure GitHub CLI (gh) using environment variables?
GitHub CLI (gh) supports following environment variable to configure itself:
- GH_TOKEN, GITHUB_TOKEN (in order of precedence): an authentication token for github.com API requests. Setting this avoids being prompted to authenticate and takes precedence over previously stored credentials.
- GH_ENTERPRISE_TOKEN, GITHUB_ENTERPRISE_TOKEN (in order of precedence): an authentication token for API requests to GitHub Enterprise. When setting this, also set GH_HOST.
- GH_HOST: specify the GitHub hostname for commands that would otherwise assume the “github.com” host when not in a context of an existing repository.
- GH_REPO: specify the GitHub repository in the “[HOST/]OWNER/REPO” format for commands that otherwise operate on a local repository.
- GH_EDITOR, GIT_EDITOR, VISUAL, EDITOR (in order of precedence): the editor tool to use for authoring text.
- GH_BROWSER, BROWSER (in order of precedence): the web browser to use for opening links.
- DEBUG: set to any value to enable verbose output to standard error. Include values “api” or “oauth” to print detailed information about HTTP requests or authentication flow.
- GH_PAGER, PAGER (in order of precedence): a terminal paging program to send standard output to, e.g. “less”.
- GLAMOUR_STYLE: the style to use for rendering Markdown.
- NO_COLOR: set to any value to avoid printing ANSI escape sequences for color output.
- CLICOLOR: set to “0” to disable printing ANSI colors in output.
- CLICOLOR_FORCE: set to a value other than “0” to keep ANSI colors in output even when the output is piped.
- GH_FORCE_TTY: set to any value to force terminal-style output even when the output is redirected. When the value is a number, it is interpreted as the number of columns available in the viewport. When the value is a percentage, it will be applied against the number of columns available in the current viewport.
- GH_NO_UPDATE_NOTIFIER: set to any value to disable update notifications. By default, gh checks for new releases once every 24 hours and displays an upgrade notice on standard error if a newer version was found.
- GH_CONFIG_DIR: the directory where gh will store configuration files. Default: “$XDG_CONFIG_HOME/gh” or “$HOME/.config/gh”.
Example:
1 2 3 4 5 6 |
## ------------------------------------- ## Using GitHub CLI environment variable ## ------------------------------------- ## Set default editor to vim export GH_EDITOR='vim' |
Hope you have enjoyed this article. To get more details in GitHub CLI, please refer the below documentation
https://cli.github.com/manual/