Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build a project using Cargo in an offline environment?

Tags:

linux

macos

rust

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?

like image 830
习明昊 Avatar asked Aug 28 '15 09:08

习明昊


2 Answers

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:

  1. 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.

  2. 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.

like image 162
Tenders McChiken Avatar answered Oct 09 '22 00:10

Tenders McChiken


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.

like image 43
Matthieu M. Avatar answered Oct 09 '22 01:10

Matthieu M.