Pub 安装与配置 #

安装概述 #

Pub 是 Dart SDK 的一部分,因此安装 Dart SDK 即可获得 Pub。根据你的使用场景,可以选择以下安装方式:

场景 推荐方式
Flutter 开发 安装 Flutter SDK(包含 Dart SDK)
Dart 开发 单独安装 Dart SDK
Web 开发 安装 Dart SDK
多版本管理 使用版本管理工具

安装 Dart SDK #

Windows #

使用 Chocolatey #

powershell
# 安装 Chocolatey(如果尚未安装)
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

# 安装 Dart SDK
choco install dart-sdk

# 安装特定版本
choco install dart-sdk --version 3.2.0

使用安装包 #

  1. 访问 Dart SDK 官方下载页面
  2. 下载 Windows 安装包(.exe)
  3. 运行安装程序,按照提示完成安装
  4. 安装程序会自动配置环境变量

手动安装 #

powershell
# 下载并解压
Invoke-WebRequest -Uri "https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.0/sdk/dartsdk-windows-x64-release.zip" -OutFile "dart-sdk.zip"
Expand-Archive -Path "dart-sdk.zip" -DestinationPath "C:\dart-sdk"

# 配置环境变量
$env:Path += ";C:\dart-sdk\dart-sdk\bin"

# 永久配置
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\dart-sdk\dart-sdk\bin", "User")

macOS #

使用 Homebrew #

bash
# 安装 Homebrew(如果尚未安装)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# 安装 Dart SDK
brew tap dart-lang/dart
brew install dart

# 安装特定版本
brew install dart@3.2

# 升级 Dart SDK
brew upgrade dart

手动安装 #

bash
# 下载并解压
curl -O https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.0/sdk/dartsdk-macos-x64-release.zip
unzip dartsdk-macos-x64-release.zip -d ~/dart-sdk

# 配置环境变量
echo 'export PATH="$PATH:$HOME/dart-sdk/dart-sdk/bin"' >> ~/.zshrc
source ~/.zshrc

Linux #

使用 apt(Debian/Ubuntu) #

bash
# 添加 Dart 仓库
sudo apt-get update
sudo apt-get install apt-transport-https
wget -qO- https://dl-ssl.google.com/linux/linux_signing_key.pub | sudo gpg --dearmor -o /usr/share/keyrings/dart.gpg
echo 'deb [signed-by=/usr/share/keyrings/dart.gpg arch=amd64] https://storage.googleapis.com/download.dartlang.org/linux/debian stable main' | sudo tee /etc/apt/sources.list.d/dart_stable.list

# 安装 Dart SDK
sudo apt-get update
sudo apt-get install dart

使用 dnf(Fedora) #

bash
# 添加 Dart 仓库
sudo dnf config-manager --add-repo https://storage.googleapis.com/download.dartlang.org/linux/fedora/dart_stable.repo

# 安装 Dart SDK
sudo dnf install dart

手动安装 #

bash
# 下载并解压
curl -O https://storage.googleapis.com/dart-archive/channels/stable/release/3.2.0/sdk/dartsdk-linux-x64-release.zip
unzip dartsdk-linux-x64-release.zip -d ~/dart-sdk

# 配置环境变量
echo 'export PATH="$PATH:$HOME/dart-sdk/dart-sdk/bin"' >> ~/.bashrc
source ~/.bashrc

安装 Flutter SDK #

如果你进行 Flutter 开发,Flutter SDK 已经包含了 Dart SDK:

Windows #

powershell
# 使用 Chocolatey
choco install flutter

# 或手动安装
# 1. 下载 Flutter SDK: https://docs.flutter.dev/get-started/install/windows
# 2. 解压到目标目录
# 3. 配置环境变量

macOS #

bash
# 使用 Homebrew
brew install flutter

# 或手动安装
git clone https://github.com/flutter/flutter.git -b stable
echo 'export PATH="$PATH:$HOME/flutter/bin"' >> ~/.zshrc
source ~/.zshrc

Linux #

bash
# 使用 Snap
sudo snap install flutter --classic

# 或手动安装
git clone https://github.com/flutter/flutter.git -b stable
echo 'export PATH="$PATH:$HOME/flutter/bin"' >> ~/.bashrc
source ~/.bashrc

版本管理工具 #

FVM(Flutter Version Manager) #

FVM 是一个流行的 Flutter/Dart 版本管理工具:

bash
# 安装 FVM
dart pub global activate fvm

# 安装 Flutter 版本
fvm install stable
fvm install 3.16.0

# 使用特定版本
fvm use 3.16.0

# 在项目中使用
fvm flutter pub get

DVM(Dart Version Manager) #

bash
# 安装 DVM
brew tap dvm-org/dvm
brew install dvm

# 安装 Dart 版本
dvm install 3.2.0

# 使用特定版本
dvm use 3.2.0

验证安装 #

检查版本 #

bash
# 检查 Dart 版本
dart --version
# Dart SDK version: 3.2.0 (stable) (Tue Nov 28 14:23:38 2023 +0000) on "macos_x64"

# 检查 Pub 版本
dart pub --version
# Pub 3.2.0

# 检查 Flutter 版本(如果安装)
flutter --version

测试 Pub 命令 #

bash
# 创建测试项目
dart create hello_world
cd hello_world

# 获取依赖
dart pub get

# 运行项目
dart run

Pub 配置 #

配置文件位置 #

Pub 的配置文件位于用户目录:

系统 配置文件路径
Windows %APPDATA%\Pub\Pub.yaml
macOS/Linux ~/.pub-cache/pub.yaml

配置镜像源 #

在中国大陆,配置镜像源可以显著提高下载速度:

临时使用 #

bash
# 设置环境变量
export PUB_HOSTED_URL=https://pub.flutter-io.cn
export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn

永久配置 #

bash
# macOS/Linux - 添加到 ~/.zshrc 或 ~/.bashrc
echo 'export PUB_HOSTED_URL=https://pub.flutter-io.cn' >> ~/.zshrc
echo 'export FLUTTER_STORAGE_BASE_URL=https://storage.flutter-io.cn' >> ~/.zshrc
source ~/.zshrc
powershell
# Windows - 使用 PowerShell
[Environment]::SetEnvironmentVariable("PUB_HOSTED_URL", "https://pub.flutter-io.cn", "User")
[Environment]::SetEnvironmentVariable("FLUTTER_STORAGE_BASE_URL", "https://storage.flutter-io.cn", "User")

常用镜像源 #

镜像源 地址
Flutter 中国 https://pub.flutter-io.cn
清华大学 https://mirrors.tuna.tsinghua.edu.cn/dart-pub
上海交大 https://mirror.sjtu.edu.cn/dart-pub

配置代理 #

bash
# 设置 HTTP 代理
export HTTP_PROXY=http://proxy.example.com:8080
export HTTPS_PROXY=http://proxy.example.com:8080

# 或者在命令中指定
HTTP_PROXY=http://proxy.example.com:8080 dart pub get

配置缓存目录 #

bash
# 查看缓存目录
dart pub cache dir
# /Users/username/.pub-cache

# 设置缓存目录
export PUB_CACHE=/custom/cache/path

Pub 缓存管理 #

缓存目录结构 #

text
.pub-cache/
├── hosted/           # 从 pub.dev 下载的包
│   └── pub.dev/
│       ├── http-1.1.0/
│       └── path-1.8.3/
├── git/              # Git 依赖
│   └── github.com-user-repo-hash/
├── global_packages/  # 全局激活的包
└── bin/              # 全局安装的可执行文件

缓存操作 #

bash
# 查看缓存目录
dart pub cache dir

# 清理缓存
dart pub cache clean

# 清理特定包的缓存
dart pub cache clean http

# 修复缓存
dart pub cache repair

离线模式 #

bash
# 使用离线模式(仅使用缓存)
dart pub get --offline

# 预下载依赖
dart pub get

全局包管理 #

激活全局包 #

bash
# 激活包(使其命令全局可用)
dart pub global activate webdev
dart pub global activate stagehand

# 激活特定版本
dart pub global activate webdev 2.7.4

# 从 Git 仓库激活
dart pub global activate --source git https://github.com/user/package.git

# 从本地路径激活
dart pub global activate --source path /path/to/package

查看全局包 #

bash
# 列出已激活的全局包
dart pub global list
# webdev 2.7.4
# stagehand 4.1.0

运行全局包 #

bash
# 方式一:直接运行
webdev serve

# 方式二:通过 pub run
dart pub global run webdev serve

停用全局包 #

bash
# 停用全局包
dart pub global deactivate webdev

IDE 配置 #

VS Code #

  1. 安装 Dart 扩展:

    • 打开 VS Code
    • Cmd+Shift+X(macOS)或 Ctrl+Shift+X(Windows/Linux)
    • 搜索 “Dart”
    • 安装 Dart 官方扩展
  2. 配置 Dart SDK 路径:

    json
    // settings.json
    {
      "dart.sdkPath": "/path/to/dart-sdk"
    }
    
  3. 配置 Flutter SDK 路径(如果使用 Flutter):

    json
    {
      "dart.flutterSdkPath": "/path/to/flutter"
    }
    

IntelliJ IDEA / Android Studio #

  1. 安装 Dart 插件:

    • 打开 Settings/Preferences
    • 进入 Plugins
    • 搜索 “Dart”
    • 安装并重启
  2. 配置 Dart SDK:

    • 打开 Settings/Preferences
    • 进入 Languages & Frameworks > Dart
    • 指定 Dart SDK 路径

环境变量详解 #

主要环境变量 #

变量 描述 示例
PUB_HOSTED_URL Pub 仓库镜像地址 https://pub.flutter-io.cn
PUB_CACHE 缓存目录 /custom/cache/path
HTTP_PROXY HTTP 代理 http://proxy:8080
HTTPS_PROXY HTTPS 代理 http://proxy:8080
NO_PROXY 不使用代理的地址 localhost,127.0.0.1

Flutter 相关环境变量 #

变量 描述 示例
FLUTTER_STORAGE_BASE_URL Flutter 存储镜像 https://storage.flutter-io.cn
FLUTTER_ROOT Flutter SDK 路径 /path/to/flutter

常见问题 #

1. 找不到 dart 命令 #

问题:运行 dart 命令时提示 “command not found”

解决方案

bash
# 检查 PATH 配置
echo $PATH

# 添加 Dart SDK 到 PATH
export PATH="$PATH:/path/to/dart-sdk/bin"

# 永久配置
echo 'export PATH="$PATH:/path/to/dart-sdk/bin"' >> ~/.zshrc

2. 网络超时 #

问题:下载依赖时超时

解决方案

bash
# 使用镜像源
export PUB_HOSTED_URL=https://pub.flutter-io.cn

# 或配置代理
export HTTP_PROXY=http://proxy:8080

3. 权限问题 #

问题:无法写入缓存目录

解决方案

bash
# macOS/Linux
sudo chown -R $(whoami) ~/.pub-cache

# 或更改缓存目录
export PUB_CACHE=~/my-cache

4. 版本冲突 #

问题:Dart SDK 版本与项目要求不匹配

解决方案

bash
# 检查项目要求的 SDK 版本
# pubspec.yaml
environment:
  sdk: '>=3.0.0 <4.0.0'

# 升级 Dart SDK
brew upgrade dart  # macOS
choco upgrade dart-sdk  # Windows

5. 缓存损坏 #

问题:依赖解析失败或运行时错误

解决方案

bash
# 清理并重新获取
dart pub cache clean
dart pub get

下一步 #

现在你已经完成了 Pub 的安装和配置,接下来学习 基本命令 开始使用 Pub 管理项目!

最后更新:2026-03-28