Overview
Pub provides commands for:- Installing and managing dependencies
- Publishing packages to pub.dev
- Running executables from packages
- Managing global packages
Usage
Core Commands
get
Install or update dependencies:- Reads
pubspec.yaml - Resolves dependencies
- Downloads packages to
.pub-cache - Creates/updates
pubspec.lock - Generates
.dart_tool/package_config.json
add
Add a new dependency to your project:remove
Remove a dependency:upgrade
Upgrade dependencies to the latest compatible versions:outdated
Check for outdated dependencies:downgrade
Downgrade to the lowest allowed versions:Publishing
publish
Publish a package to pub.dev:Publishing Checklist
Before publishing:- Update version in
pubspec.yaml - Update
CHANGELOG.md - Run tests:
dart test - Check formatting:
dart format --output=none --set-exit-if-changed . - Analyze code:
dart analyze - Dry run:
dart pub publish --dry-run - Publish:
dart pub publish
Global Packages
global activate
Install a package globally:global run
Run a globally activated package:global deactivate
Deactivate a global package:global list
List globally activated packages:Other Commands
deps
Show dependency graph:cache
Manage the package cache:login/logout
Manage pub.dev authentication:token
Manage authentication tokens:Configuration
pubspec.yaml
The package configuration file:pubspec.yaml
Dependency Types
Regular Dependencies
Dev Dependencies
Used only during development:Dependency Overrides
Force specific versions:Version Constraints
pubspec.lock
Lock file with exact versions:pubspec.lock
pubspec.lock for applications, but not for libraries.
Environment Variables
PUB_CACHE
Set the package cache location:PUB_HOSTED_URL
Use an alternative package repository:Best Practices
1. Commit pubspec.lock
2. Use Version Constraints
3. Regular Updates
4. Clean Development
CI/CD Integration
GitHub Actions
.github/workflows/dart.yml
Cache Dependencies
.github/workflows/dart.yml
Troubleshooting
Clear Cache
Repair Cache
Resolve Conflicts
Version Solve Failed
Examples
New Project Setup
Publishing a Package
Using Global Tools
See Also
- pub.dev - Official package repository
- Package Layout Conventions - How to structure packages
- Publishing Packages - Publishing guide
- Dependency Management - Package management guide