How can I build my application using cargo while offline (without internet)

I like to build my project with dependencies but whenever i use cargo build command it get stuck at following message

# cargo build
** Updating registry https://github.com/rust-lang/crates.io-index**

need help here because i an trying the build native application for linux in rust

2 Likes

If this is your first time building something, you're just going to have to let it run. The first time can be excruciatingly slow, and Cargo provides no indication as to whether it's making any progress. I think the longest I've waited is ~25 minutes.

If you've built the package before, and it's trying to update the registry... you'll just have to let it. Sometimes, Cargo decides it needs to update the registry, and you can't convince it otherwise. I used to maintain a local mirror of the index to get around this, but that's been broken or removed or something recently.

Right now, in practice, Rust isn't a language you can use without an internet connection.

I think, this is a bug. If you have already a full build and you want to rebuild only your changes, you must not be forced to have a internet connection.

@DineshD: You could work with rustc directly.

There's a command cargo fetch that you can run with an internet connection. After that, it should have everything to build that project without more downloads.

3 Likes

I actuall wanted to open a bugreport on github about this, but MS "flagged" my account, so it's invisible (https://github.com/rust-lang/cargo/issues/7234). You can see below how to use the [patch.crates-io] section to avoid connecting to the internet ever at all.

That is if you can use cargo 0.35 (rust 1.34), since 0.37 (rust 1.36) this feature broke. (I haven't tried 0.36/1.37, that might work too.)

Problem
0.37 with --offline requires network access for build but 0.35 did not for -Z offline .

Cargo.toml:

[package]
name = "hw"
version = "0.1.0"
authors = ["user@localhost"]
edition = "2018"

[dependencies]
nom = "^4"

[patch.crates-io]
nom = { path = "path/to/nom" }
memchr { path = "path/to/memchr" }

Steps

  1. $ cargo new hw
  2. Add dependencies and patches to Cargo.toml
  3. $ cargo build

Possible Solution(s)

The breaking change was introduced by #6871, the simplest would be to remove the repo check in update_index . Or maybe propagate an "advanced usage" flag there, which would be set if e.g. there is a patch in Cargo.toml, and the set flag would prevent early termination. Or maybe even a "keep going" command line flag, which would instruct cargo to only bail out if there is an actual problem.

Notes

Output of cargo version :
1.34.0 vs 1.36.0

I'm using the debian cargo packages 0.35.0-2 and 0.37.0-3 on amd64.
Also used the source packages librust-nom-dev 4.0.0-1 and librust-memchr-dev 2.2.1-1 for testing.

cargo build --offline