Homebrew is the package manager for macOS, and it makes installing and updating software incredibly simple.
Let’s get the GitHub CLI installed. Open your terminal and run:
brew install gh
This command tells Homebrew to find the gh package, download it, and install it on your system. Homebrew handles all the dependencies and puts the executable in a place where your shell can find it.
Once installed, you can verify it by checking the version:
gh --version
You should see output like gh version 2.37.0 (2023-10-26).
Now, let’s say GitHub releases a new version of the CLI. To update it, you’ll use a similar command:
brew upgrade gh
This command instructs Homebrew to check if a newer version of the gh package is available. If it is, Homebrew will download and install the update, replacing the old version. It’s that straightforward.
Before running brew upgrade gh, it’s often a good idea to update Homebrew itself to ensure you’re getting the latest package information and to fix any potential Homebrew-related issues:
brew update
This command fetches the latest package definitions from Homebrew’s repositories. It doesn’t upgrade your installed software, but it makes sure Homebrew knows about all the newest versions and available packages.
After running brew update, you can then run brew upgrade gh to get the latest GitHub CLI.
If you ever want to see what versions of gh are installed, or what files it uses, you can use:
brew list --versions gh
And to see the details of the installed package, including its dependencies and installation path:
brew info gh
Sometimes, Homebrew might get confused or have outdated information. A common troubleshooting step is to run brew doctor. This command checks your Homebrew environment for potential problems and gives you advice on how to fix them.
brew doctor
For example, brew doctor might tell you that some directories are writable by group or world, which could interfere with Homebrew’s ability to manage packages securely. It will provide specific chmod commands to correct these permissions.
If you encounter an error during installation or upgrade, such as Error: No available formula with the name "gh", it usually means Homebrew’s index is out of date. Running brew update first should resolve this. Another common error is related to network issues or corrupted downloads. In such cases, you might try brew cleanup to remove old downloads and then brew update && brew upgrade gh again.
The gh command-line tool is powerful. After installation, the next step is usually to authenticate it with your GitHub account so you can interact with your repositories directly from the terminal. This is done with:
gh auth login