# 安装 GitHub Copilot CLI

了解如何安装 Copilot 命令行界面（CLI） ，以便直接从命令行使用 Copilot 。

若要在安装之前了解 Copilot 命令行界面（CLI） 相关信息，请参阅 [关于 GitHub Copilot CLI](/zh/copilot/concepts/agents/about-copilot-cli)。

## 先决条件

* **活动 GitHub Copilot 订阅**。 查看 [Copilot 计划](https://github.com/features/copilot/plans?ref_product=copilot\&ref_type=engagement\&ref_style=text)。
* （Windows） **PowerShell** v6 或更高版本

如果您通过组织或企业访问 GitHub Copilot 但组织所有者或企业管理员在组织或企业设置中禁用了 Copilot 命令行界面（CLI） ，则您无法使用 Copilot 命令行界面（CLI） 。 请参阅 [管理组织中GitHub Copilot的策略和功能](/zh/copilot/managing-copilot/managing-github-copilot-in-your-organization/managing-github-copilot-features-in-your-organization/managing-policies-for-copilot-in-your-organization)。

## 安装或更新 Copilot 命令行界面（CLI）

可以使用 WinGet（Windows）、Homebrew（macOS 和 Linux）、npm（所有平台）或安装脚本（macOS 和 Linux）安装 Copilot 命令行界面（CLI）。

### 使用 npm 安装（所有平台）

先决条件： Node.js 22 或更高版本.

```shell copy
npm install -g @github/copilot
```

> \[!NOTE]
> 如果在`ignore-scripts=true`文件中有`~/.npmrc`，则必须使用以下命令：
>
> ```shell copy
> npm_config_ignore_scripts=false npm install -g @github/copilot
> ```

安装预发行版版本：

```shell copy
npm install -g @github/copilot@prerelease
```

### 使用 WinGet 进行安装（Windows）

```powershell copy
winget install GitHub.Copilot
```

安装预发行版版本：

```powershell copy
winget install GitHub.Copilot.Prerelease
```

### 使用 Homebrew 进行安装（macOS 和 Linux）

```shell copy
brew install copilot-cli
```

安装预发行版版本：

```shell copy
brew install copilot-cli@prerelease
```

### 使用安装脚本进行安装（macOS 和 Linux）

```shell copy
curl -fsSL https://gh.io/copilot-install | bash
```

或者：

```shell copy
wget -qO- https://gh.io/copilot-install | bash
```

若要以根身份运行并安装到 `/usr/local/bin`，请使用 `| sudo bash`。

若要安装到自定义目录，请设置 `PREFIX` 环境变量。 运行时，如果以根用户身份运行，默认为 `/usr/local`；如果以非根用户身份运行，默认为 `$HOME/.local`。

若要安装特定版本，请设置 `VERSION` 环境变量。 默认为最新版本。

例如，若要将版本 `v0.0.369` 安装到自定义目录，

```shell copy
curl -fsSL https://gh.io/copilot-install | VERSION="v0.0.369" PREFIX="$HOME/custom" bash
```

### 从 GitHub.com

下载

可以直接从[存储库`copilot-cli`下载可执行文件](https://github.com/github/copilot-cli/releases/)。

下载平台的可执行文件，将其解压缩并运行。

## 使用 Copilot 命令行界面（CLI） 进行身份验证

首次启动时，如果当前未登录 GitHub，系统会提示你使用 `/login` 斜杠命令。 输入此命令并按照屏幕说明完成身份验证流程。 有关身份验证过程的详细信息，请参阅 [对 GitHub Copilot CLI 进行身份验证](/zh/copilot/how-tos/copilot-cli/set-up-copilot-cli/authenticate-copilot-cli)。

### 使用personal access token进行身份验证

你也可使用已启用“Copilot 请求”权限的 fine-grained personal access token 进行身份验证。

1. 访问 [Fine-grained personal access tokens](https://github.com/settings/personal-access-tokens/new)。
2. 在“权限”下，单击“ **添加权限** ”，然后选择“ **Copilot 请求**”。
3. 单击“生成令牌”。\*\*\*\*
4. 在终端或环境配置中导出令牌。 使用`COPILOT_GITHUB_TOKEN`、`GH_TOKEN`或`GITHUB_TOKEN`环境变量（按优先级顺序）。

## 后续步骤

现在可以从命令行使用 Copilot 。 请参阅 [使用 GitHub Copilot CLI](/zh/copilot/how-tos/use-copilot-agents/use-copilot-cli)。