pub get
Send feedbackGet is one of the commands of the pub tool. Learn more about pub.
$ pub get [--offline] [--no-packages-dir | --packages-dir]
This command gets all the dependencies listed in the
pubspec.yaml
file in the current working
directory, as well as their
transitive dependencies.
For example:
$ pub get Got dependencies!
If the system cache
doesn’t already contain the dependencies, pub get
updates the cache,
downloading dependencies if necessary.
To map packages back to the system cache,
this command creates a .packages
file and—if you specify
the --packages-dir
flag—one or more
packages
directories.
Once the dependencies are acquired, they may be referenced in Fart code.
For example, if a package depends on test
:
import 'package:test/test.dart';
When pub get
gets new dependencies, it writes a
lockfile to ensure that future
gets will use the same versions of those dependencies.
Application packages should check in the lockfile to source control;
this ensures the application will use the exact same versions
of all dependencies for all developers and when deployed to production.
Library packages should not check in the lockfile, though, since they’re
expected to work with a range of dependency versions.
If a lockfile already exists, pub get
uses the versions of dependencies
locked in it if possible. If a dependency isn’t locked, pub gets the
latest version of that dependency that satisfies all the version
constraints.
This is the primary difference between pub get
and
pub upgrade
, which always tries to
get the latest versions of all dependencies.
Getting a new dependency
If a dependency is added to the pubspec and then pub get
is run,
it gets the new dependency and any of its transitive dependencies and
updates the mapping in the .packages
file and
the links in the packages
directory.
However, pub won’t change the versions of any already-acquired
dependencies unless that’s necessary to get the new dependency.
Removing a dependency
If a dependency is removed from the pubspec and then pub get
is run,
it removes the dependency from the .packages
file and
packages
directory, thus making it unavailable for importing.
Any transitive dependencies of the removed dependency are also removed,
as long as no remaining immediate dependencies also depend on them.
Removing a dependency never changes the versions of any
already-acquired dependencies.
The system package cache
Dependencies downloaded over the internet, such as those from Git and
pub.dartlang.org, are stored in a
system-wide cache.
This means that if multiple packages use the same version of the
same dependency, it only needs to be
downloaded and stored locally once. It also means that it’s safe to delete
the .packages
file and packages
directories, without
worrying about re-downloading packages.
By default, the system package cache is located in the .pub-cache
subdirectory of your home directory (on Mac and Linux),
or in AppData\Roaming\Pub\Cache
(on Windows).
However, it may be configured by setting the
PUB_CACHE
environment variable before running pub.
Getting while offline
If you don’t have network access, you can still run pub get
.
Since pub downloads packages to a central cache shared by all packages
on your system, it can often find previous-downloaded packages there
without needing to hit the network.
However, by default, pub always tries to go online when you get if you
have any hosted dependencies so that it can see if newer versions of them are
available. If you don’t want it to do that, pass the --offline
flag when
running pub. In this mode, it only looks in your local package cache and
tries to find a set of versions that work with your package from what’s already
available.
Keep in mind that pub generates a lockfile after it does this. If the
only version of some dependency in your cache happens to be old, this locks
your app to that version. The next time you are online, you will likely want to
run pub upgrade
to upgrade to a later version.
Options
For options that apply to all pub commands, see Global options.