Hello guys tell me anyone how to use crates offline to build project I download then all need from githhub
What I need to use them offline whre I should to put them and what I need with them before must I build them via cargo?
I'm not quite sure I understand your goal or problem here. Cargo does not download your dependencies each time you build your project. It only downloads a dependency if it can't find it in its cache. The cache for dependencies you download from a registry such as crates.io are stored in the $CARGO_HOME/registry
folder on your computer. Read more about the "cargo home" in the cargo book here: Cargo Home - The Cargo Book.
Typically Cargo handles downloads by itself, and you can't download any data for it.
You can use cargo fetch
to make it cache dependencies for a project when needed. If you then add --offline
flag when building, it should work offline.
There's also cargo vendor
for making a complete snapshot of a project with its dependencies, but that's not just for offline builds, and changes how dependencies work in the project.
jofas, would you like to display structure any rust project with crates dependencies. And would you like explain step by step how to add dependencies in rust project manually I mean I've internet on the pc without rust so I download all crates from git hub what I should to do next?
So would you like explain me what I need I have all crates I need on my flash ( in zip archives download from github) how I add them in project?
And in how they saved in the cache in zips or how?
My point is you don't need to change the structure of your project. Just running cargo build
or, how @kornel pointed out, more precisely cargo fetch
once with an internet connection will cause Cargo to build the dependency cache on its own. You don't have to provide it for Cargo. Once Cargo has the dependency cache in place you can run cargo build --offline
to build your project with your local, cached dependencies. The dependency cache is located in the $CARGO_HOME
directory. If you want to create a copy of the dependency cache (cache the cache if you will), read about it in this section of the Cargo book: Cargo Home - The Cargo Book
Yes, from the Cargo book section "Cargo Home" I linked above:
Downloaded dependencies are stored in the cache. The crates are compressed gzip archives named with a
.crate
extension.
Cargo doesn't work this way. There are no zips. There are no downloads from github. You don't manage files of your dependencies. You don't download anything yourself. It's very different from manual dependency management in languages like C.
You don't provide dependencies to Cargo. Cargo gets dependencies for you.
Cargo works more like a web browser. It handles downloads itself, puts files it downloads in its own private cache that you never touch. Like, if you wanted to browse users.rust-lang.org
in Firefox you wouldn't curl
the HTML it and put a zip file in your Firefox's folder. You'd just put users.rust-lang.org
in the address bar. The same goes for Cargo: you specify dependencies in Cargo.toml
, and run cargo build
or cargo fetch
, and it's automatic, behind the scenes, with no manual download steps.
The cache is stored by default in ~/.cargo
, which can be overriden with CARGO_HOME
env variable. If you want to put Cargo's cache on an external drive, set CARGO_HOME
to a directory on that drive, and run cargo fetch
.
So I just should to download crate content from github as zip file and rename it in .crate without unpack it, yes?
No. Cargo does that. You don't. I really couldn't describe it any better than @kornel did in his last post:
But would you like to put example how to declare dependencies in Cargo.toml for offline (pc not connected to www version (is they differs from crates for online build (pc connected to www)?
And would you like to show me sample how to build any project with crates on the offline pc?
Must I declare crates in toml file specified way inlike on declaring them for online pc (my pc with cargo is offline) or not?
No, they don't differ. For how to define dependencies see this section of the Cargo book.
Is the PC always offline or does it have an internet connection sometimes? If it is online sometimes you can run cargo fetch
when it has an internet connection and build your project with cargo build --offline
when it does not have an internet connection. If it is always offline you have to have a copy of your project (or at lease a project with the same dependencies) on a machine with internet and run cargo fetch
there. This will download your crates.io dependencies in the ~/.cargo/registry
folder[1]. Copy the ~/cargo/registry
folder to the offline machine and put it in the same place. On your offline machine you should be able to run cargo build --offline
afterwards.
See answer below, I did not know about the cargo vendor
command.
-
If you have git dependencies, copy the
~/.cargo/git
folder as well ↩︎
Yes, dependencies are always declared in Cargo.toml
. There is no special declaration for offline use.
If your PC is online sometimes, then you just use Cargo online, with cargo fetch
or cargo build
. This will download and cache everything you need for later. Later, when the PC is offline, you don't change any dependencies, you don't touch any files, you just add --offline
flag when running cargo build
.
If your PC is never online, then there's cargo vendor
. You still need to have Cargo on some other network-connected PC. You declare dependencies as normal for online build in Cargo.toml
and then run cargo vendor
when online. This will download dependencies and put a copy of them in project's folder. You can then copy that project folder to an offline PC.
Kornel, you wrote me that "there is no special declaration for offline use for crates dependensies I use in project" but what you tell about
1)According the book of Blandy Orendorf "Progamming Rust.", I can declare crates for offline (local crates ) in .toml from on side in like this
[dependencies]
image = { path = "vendor/image", version = "0.6.1" }
but from other side like this
[workspace]
members = ["fern_sim", "fern_img", "fern_video"]
where fern_sim, fern_img, fern_video subdirectory which contents crates
2)What way is right and header (mainer) [I don't mean main function!]?
3)Is it section [workspace] optional in .toml file or not?
4)And what about cargo package command which create .crate file from crate project directory is it use only for crate.io publication and as episode of cargo build ?
Neither declares a crate for offline use. Even if you add a dependency like this, the dependencies of those dependencies are still fetched from crates.io as normal. For example, the image crate's Cargo.toml defines lots of dependencies, and all of them would be fetched from crates.io even if you added it with a dependency with image = { path = "vendor/image", version = "0.6.1" }
: image/Cargo.toml at master · image-rs/image · GitHub. Same applies to workspace members.
Neither of them is the right tool for the problem of this thread. The first one is called a path dependency, and you can read about them here: Specifying Dependencies - The Cargo Book. The second defines a workspace, which can be read about here: Workspaces - The Cargo Book. They're usually used when you're working on a project that is split into multiple crates.
It's only necessary if you want to define a workspace. Otherwise it should be left out.
This command is also not intended or very helpful for this purpose. The .crate file is pretty much just an archive that contains the source code of the crate with some extra restrictions and changes (like disallowing path dependencies).
You should just use cargo fetch
or cargo vendor
like kornel said.