I have a laptop with an OS X system which can access the internet, and a Linux server which can not access internet for some security reason.
I want to build a Rust project with some dependency on the Linux server. Because the Linux server can not access internet, I run the cargo build
command on the OSX laptop and download the dependency source file to the .cargo
directory and then copy these files to the Linux server and put them into /root/.cargo
directory.
I made the file structures the same, but when I run cargo build
on the Linux server, it still tries to connect to this website and the build fails. The cargo build
command always tries to connect internet although the dep source files are already in the .cargo
directory.
How can I build a Rust project with dependencies in an offline environment? Why does copying the source file of the dependencies not work?
Good News! As of Rust 1.37, you can use Cargo's own vendor
command to download and bundle your crate's dependencies in the crate itself:
First, run cargo vendor
. This will setup a new directory named vendor
in the root of your crate. It will then download dependencies from crates.io and git, and store them in this new directory.
When cargo vendor
is done downloading all the required dependencies, it will print a set of instructions that you'll need to follow. At the time of this writing, you only need to copy a few lines to .cargo/config.toml
. Note that config.toml
is relative to the root of your crate and is not the one in your home directory.
Once you're done, your crate will be completely self-contained as far as dependencies are concerned. You can couple this approach with Rust's offline installers to build Rust programs completely offline.
For Rust 1.37+ see: https://stackoverflow.com/a/65254702/147192
The short answer is: up to 1.37 (excluded), it's complicated.
The long answer is that cargo
will attempt to connect to github
in order to check the index of the crates that the Cargo.toml
file references.
I recommend you to check the cargo-vendor prototype to solve this issue (by aturon, a member of the Rust tooling subteam), and otherwise you could look at how some people created a mirror for crates.io in order to avoid the dependency on Internet.
There is a demand for Rust builds not to require Internet, and people working on it, however there is no blessed solution for now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With