1CARGO-ADD(1) General Commands Manual CARGO-ADD(1)
2
3
4
6 cargo-add - Add dependencies to a Cargo.toml manifest file
7
9 cargo add [options] crate...
10 cargo add [options] --path path
11 cargo add [options] --git url [crate...]
12
14 This command can add or modify dependencies.
15
16 The source for the dependency can be specified with:
17
18 • crate@version: Fetch from a registry with a version constraint of
19 "version"
20
21 • --path path: Fetch from the specified path
22
23 • --git url: Pull from a git repo at url
24
25 If no source is specified, then a best effort will be made to select
26 one, including:
27
28 • Existing dependencies in other tables (like dev-dependencies)
29
30 • Workspace members
31
32 • Latest release in the registry
33
34 When you add a package that is already present, the existing entry will
35 be updated with the flags specified.
36
37 Upon successful invocation, the enabled (+) and disabled (-) features
38 <https://doc.rust-lang.org/cargo/reference/features.md> of the
39 specified dependency will be listed in the command's output.
40
42 Source options
43 --git url
44 Git URL to add the specified crate from
45 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-dependencies-from-git-repositories>.
46
47 --branch branch
48 Branch to use when adding from git.
49
50 --tag tag
51 Tag to use when adding from git.
52
53 --rev sha
54 Specific commit to use when adding from git.
55
56 --path path
57 Filesystem path
58 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#specifying-path-dependencies>
59 to local crate to add.
60
61 --registry registry
62 Name of the registry to use. Registry names are defined in Cargo
63 config files
64 <https://doc.rust-lang.org/cargo/reference/config.html>. If not
65 specified, the default registry is used, which is defined by the
66 registry.default config key which defaults to crates-io.
67
68 Section options
69 --dev
70 Add as a development dependency
71 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#development-dependencies>.
72
73 --build
74 Add as a build dependency
75 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#build-dependencies>.
76
77 --target target
78 Add as a dependency to the given target platform
79 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#platform-specific-dependencies>.
80 </dl>
81
82 Dependency options
83 --dry-run
84 Don't actually write the manifest
85
86 --rename name
87 Rename
88 <https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#renaming-dependencies-in-cargotoml>
89 the dependency.
90
91 --optional
92 Mark the dependency as optional
93 <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>.
94
95 --no-optional
96 Mark the dependency as required
97 <https://doc.rust-lang.org/cargo/reference/features.html#optional-dependencies>.
98
99 --no-default-features
100 Disable the default features
101 <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>.
102
103 --default-features
104 Re-enable the default features
105 <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>.
106
107 --features features
108 Space or comma separated list of features to activate
109 <https://doc.rust-lang.org/cargo/reference/features.html#dependency-features>.
110 When adding multiple crates, the features for a specific crate may
111 be enabled with package-name/feature-name syntax. This flag may be
112 specified multiple times, which enables all specified features.
113
114 Display Options
115 -v, --verbose
116 Use verbose output. May be specified twice for "very verbose"
117 output which includes extra output such as dependency warnings and
118 build script output. May also be specified with the term.verbose
119 config value
120 <https://doc.rust-lang.org/cargo/reference/config.html>.
121
122 -q, --quiet
123 Do not print cargo log messages. May also be specified with the
124 term.quiet config value
125 <https://doc.rust-lang.org/cargo/reference/config.html>.
126
127 --color when
128 Control when colored output is used. Valid values:
129
130 • auto (default): Automatically detect if color support is
131 available on the terminal.
132
133 • always: Always display colors.
134
135 • never: Never display colors.
136
137 May also be specified with the term.color config value
138 <https://doc.rust-lang.org/cargo/reference/config.html>.
139
140 Manifest Options
141 --manifest-path path
142 Path to the Cargo.toml file. By default, Cargo searches for the
143 Cargo.toml file in the current directory or any parent directory.
144
145 -p spec, --package spec
146 Add dependencies to only the specified package.
147
148 --frozen, --locked
149 Either of these flags requires that the Cargo.lock file is
150 up-to-date. If the lock file is missing, or it needs to be updated,
151 Cargo will exit with an error. The --frozen flag also prevents
152 Cargo from attempting to access the network to determine if it is
153 out-of-date.
154
155 These may be used in environments where you want to assert that the
156 Cargo.lock file is up-to-date (such as a CI build) or want to avoid
157 network access.
158
159 --offline
160 Prevents Cargo from accessing the network for any reason. Without
161 this flag, Cargo will stop with an error if it needs to access the
162 network and the network is not available. With this flag, Cargo
163 will attempt to proceed without the network if possible.
164
165 Beware that this may result in different dependency resolution than
166 online mode. Cargo will restrict itself to crates that are
167 downloaded locally, even if there might be a newer version as
168 indicated in the local copy of the index. See the cargo-fetch(1)
169 command to download dependencies before going offline.
170
171 May also be specified with the net.offline config value
172 <https://doc.rust-lang.org/cargo/reference/config.html>.
173
174 Common Options
175 +toolchain
176 If Cargo has been installed with rustup, and the first argument to
177 cargo begins with +, it will be interpreted as a rustup toolchain
178 name (such as +stable or +nightly). See the rustup documentation
179 <https://rust-lang.github.io/rustup/overrides.html> for more
180 information about how toolchain overrides work.
181
182 --config KEY=VALUE or PATH
183 Overrides a Cargo configuration value. The argument should be in
184 TOML syntax of KEY=VALUE, or provided as a path to an extra
185 configuration file. This flag may be specified multiple times. See
186 the command-line overrides section
187 <https://doc.rust-lang.org/cargo/reference/config.html#command-line-overrides>
188 for more information.
189
190 -h, --help
191 Prints help information.
192
193 -Z flag
194 Unstable (nightly-only) flags to Cargo. Run cargo -Z help for
195 details.
196
198 See the reference
199 <https://doc.rust-lang.org/cargo/reference/environment-variables.html>
200 for details on environment variables that Cargo reads.
201
203 • 0: Cargo succeeded.
204
205 • 101: Cargo failed to complete.
206
208 1. Add regex as a dependency
209
210 cargo add regex
211
212 2. Add trybuild as a dev-dependency
213
214 cargo add --dev trybuild
215
216 3. Add an older version of nom as a dependency
217
218 cargo add nom@5
219
220 4. Add support for serializing data structures to json with derives
221
222 cargo add serde serde_json -F serde/derive
223
225 cargo(1), cargo-remove(1)
226
227
228
229 CARGO-ADD(1)