Gemini CLI 详细安装教程 2025

完整的 Google Gemini CLI 安装配置指南,支持 Windows、macOS、Linux 系统。 包含环境配置、API密钥设置、常见问题解答,让您快速上手 Gemini 命令行工具。

🚀 快速开始

⚠️ 安装前准备

在安装 Gemini CLI 之前,请确保您的系统已满足以下要求:

  • 已安装 Python 3.8+ 或 Node.js 16+
  • 拥有 Google AI Studio API 密钥
  • 具备基本的命令行操作能力
✅ 一键安装命令

如果您已满足上述要求,可以直接使用以下命令安装:

# 使用 pip 安装(推荐) pip install google-generativeai # 或使用 npm 安装 npm install -g @google/generative-ai-cli

📋 详细安装步骤

1

安装 Python 或 Node.js

Windows 系统需要先安装运行环境:

# 方法一:安装 Python(推荐) # 访问 https://python.org 下载 Python 3.8+ # 安装时勾选 "Add Python to PATH" # 方法二:安装 Node.js # 访问 https://nodejs.org 下载最新 LTS 版本

验证安装:

# Python 验证 python --version pip --version # Node.js 验证 node --version npm --version
2

安装 Gemini CLI

使用 pip 或 npm 安装 Gemini CLI:

# 使用 pip 安装(推荐) pip install google-generativeai # 或使用 npm 安装 npm install -g @google/generative-ai-cli
权限问题

如果遇到权限错误,请以管理员身份运行命令提示符。

3

配置 API 密钥

设置您的 Google AI Studio API 密钥:

# 设置环境变量 set GOOGLE_API_KEY=your_api_key_here # 或创建配置文件 echo GOOGLE_API_KEY=your_api_key_here > .env
1

安装 Python 或 Node.js

macOS 用户可以使用 Homebrew 安装:

# 安装 Python(推荐) brew install python@3.9 # 或安装 Node.js brew install node

验证安装:

# Python 验证 python3 --version pip3 --version # Node.js 验证 node --version npm --version
2

安装 Gemini CLI

# 使用 pip3 安装(推荐) pip3 install google-generativeai # 或使用 npm 安装 npm install -g @google/generative-ai-cli

如果遇到权限问题,使用 sudo:

sudo pip3 install google-generativeai # 或 sudo npm install -g @google/generative-ai-cli
3

配置 API 密钥

# 添加到 shell 配置文件 echo 'export GOOGLE_API_KEY=your_api_key_here' >> ~/.zshrc source ~/.zshrc # 或创建配置文件 echo GOOGLE_API_KEY=your_api_key_here > .env
1

安装 Python 或 Node.js

使用包管理器安装运行环境:

# Ubuntu/Debian - 安装 Python(推荐) sudo apt update sudo apt install python3 python3-pip # 或安装 Node.js curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - sudo apt-get install -y nodejs
2

安装 Gemini CLI

# 使用 pip3 安装(推荐) pip3 install google-generativeai # 或使用 npm 安装 sudo npm install -g @google/generative-ai-cli
3

配置 API 密钥

# 添加到 bash 配置 echo 'export GOOGLE_API_KEY=your_api_key_here' >> ~/.bashrc source ~/.bashrc # 或创建配置文件 echo GOOGLE_API_KEY=your_api_key_here > .env

✅ 验证安装

测试 Gemini CLI

运行以下命令验证安装是否成功:

# Python 测试脚本 python -c "import google.generativeai as genai; print('Gemini API installed successfully')" # 或使用 CLI 工具 gemini-cli "你好,请介绍一下你自己"
成功标志

如果您看到 Gemini 的回复或成功消息,说明安装配置成功!

❓ 常见问题解答

如何获取 Google AI Studio API 密钥? +

1. 访问 Google AI Studio

2. 登录您的 Google 账户

3. 点击 "Create API Key" 创建新的密钥

4. 复制密钥并妥善保存

安装时出现权限错误怎么办? +

Windows:以管理员身份运行命令提示符

macOS/Linux:使用 sudo 命令或配置用户环境

# 配置 pip 用户安装 pip install --user google-generativeai # 配置 npm 用户目录 npm config set prefix '~/.npm-global' echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
Gemini CLI 有哪些常用命令? +
# Python 脚本示例 import google.generativeai as genai genai.configure(api_key="YOUR_API_KEY") model = genai.GenerativeModel('gemini-pro') response = model.generate_content("Hello") print(response.text)
如何更新 Gemini CLI? +
# 使用 pip 更新 pip install --upgrade google-generativeai # 使用 npm 更新 npm update -g @google/generative-ai-cli
API 调用频率限制是多少? +

Google Gemini API 免费版本的调用限制:

  • 每分钟最多 60 次请求
  • 每天最多 1500 次请求
  • 如需更高限制,请升级到付费版本