国内网络环境下,直接从官方源下载 Node.js 包、Git 以及通过 npm/Homebrew 安装依赖时速度较慢甚至超时。本文介绍如何使用国内镜像源加速这些工具的安装与使用。
安装 Node.js
Windows
推荐使用清华大学镜像站下载 Node.js 安装包:
- 访问 清华大学 Node.js 镜像
- 下载
node-v24.0.0-x64.msi安装包 - 双击运行安装,一路默认即可
或者直接访问 Node.js 官网,选择左侧 LTS 版本下载。
安装完成后打开命令行验证:
node -v
npm -v
macOS
推荐使用 Homebrew 安装(Homebrew 镜像配置见下文):
brew install node
或使用 nvm 管理多版本:
curl -o- https://github.com/nvm-sh/nvm/raw/main/install.sh | bash
source ~/.zshrc
nvm install --lts
Linux
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt-get install -y nodejs
安装 Git
Windows
推荐从清华大学镜像站下载:
- 下载 Git-2.53.0.3-64-bit.exe(清华镜像)
- 双击安装,保持默认选项即可
安装完成后验证:
git --version
macOS
macOS 自带 Git,也可通过 Homebrew 安装最新版:
brew install git
Linux (Debian/Ubuntu)
sudo apt-get update
sudo apt-get install -y git
配置 npm 腾讯云镜像
npm 默认从 registry.npmjs.org 下载包,国内访问较慢。切换到腾讯云镜像可以显著提升速度。
全局设置镜像源
npm config set registry https://mirrors.cloud.tencent.com/npm/
验证配置
npm config get registry
应显示:https://mirrors.cloud.tencent.com/npm/
临时使用(不修改全局配置)
在安装命令后加 --registry 参数:
npm install -g @anthropic-ai/claude-code --registry https://mirrors.cloud.tencent.com/npm/
恢复官方源
npm config set registry https://registry.npmjs.org/
macOS Homebrew 镜像加速
Homebrew 默认从 GitHub 拉取,国内速度较慢。推荐使用清华大学镜像源。
设置 Homebrew 镜像
将以下内容追加到 ~/.zshrc:
{
echo ''
echo '# Homebrew 清华镜像'
echo 'export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"'
echo 'export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"'
echo 'export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"'
echo 'export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"'
echo 'export HOMEBREW_PIP_INDEX_URL="https://pypi.tuna.tsinghua.edu.cn/simple"'
} >> ~/.zshrc
使配置生效:
source ~/.zshrc
验证
brew update
如果更新速度明显加快,说明镜像已生效。
首次安装 Homebrew
如果尚未安装 Homebrew,可使用清华镜像加速安装:
export HOMEBREW_BREW_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/brew.git"
export HOMEBREW_CORE_GIT_REMOTE="https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/homebrew-core.git"
export HOMEBREW_API_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles/api"
export HOMEBREW_BOTTLE_DOMAIN="https://mirrors.tuna.tsinghua.edu.cn/homebrew-bottles"
/bin/bash -c "$(curl -fsSL https://mirrors.tuna.tsinghua.edu.cn/git/homebrew/install/raw/HEAD/install.sh)"
恢复官方源
unset HOMEBREW_API_DOMAIN
unset HOMEBREW_BOTTLE_DOMAIN
unset HOMEBREW_BREW_GIT_REMOTE
unset HOMEBREW_CORE_GIT_REMOTE
unset HOMEBREW_PIP_INDEX_URL
git -C "$(brew --repo)" remote set-url origin https://github.com/Homebrew/brew
pip 镜像加速(可选)
如果使用 Python SDK,推荐配置 pip 腾讯云镜像:
pip config set global.index-url https://mirrors.cloud.tencent.com/pypi/simple
Git Clone 加速(可选)
对于 GitHub 上的仓库,可以使用镜像加速克隆:
# 使用 GitHub 代理
git clone https://gh.llkk.cc/https://github.com/用户名/仓库名.git
加速地址来自第三方 GitHub Proxy,请确认项目名和版本号一致。
