Setup local cargo registry

I'm trying to set up a local cargo registry so I don't have to download the dependencies because I have them beforehand. I have downloaded the index and each .crate file through get-all-crates. But at the time I want to use these downloaded files without network access, I don't achieve to do so, what is the way to do so?

I have already tried to use cargo-local-registry to set up a local registry, apart from all the other means that are explained through the registries wiki.

As soon as I set the net.offline option to false in .cargo/config.toml for a project, it will not use the index I provide to. Concretely I have tried to set registries.my-registry = { index = "file:///path/to/crates.io-index" } and registry.default = "my-registry", but when running cargo add syn I get the next error: error: the crate syn could not be found in registry index.. The next step would be to use the crate files I have downloaded, but I cannot even query the local index.

Thanks in advance

Ubuntu Manpage: apt-offline - Offline APT Package manager?

This has nothing to do, I'm looking for cargo to use the already downloaded crates by enabling a local registry. There is not documentation about how to do so.

Could you copy the names of the archives to a file and base the comparison on those names?

To base which comparison? I already have all the packages I need downloaded without belonging to CARGO_HOME, I want them to be used directly without having to access online resources. I also accept to include them in CARGO_HOME, but there is no documentation about how to modify in a consistent way this registry and index.

I didn't think that cargo used a non-crate specific manifest file, I think the dependencies are declares in the manifest and matched to the version number which is the name of the file in the archive.

I don't think you are understanding the issue. Cargo does fetch the dependencies of a crate from a default registry (crates.io), and this can be changed. Despite the dependencies are declared in the Manifest file, they are downloaded using a registry index. Therefore, if I want to build a project without fetching the dependencies (because I already fetched them through another method but they are not included in the registry from CARGO_HOME), I should provoke cargo to either fetch these dependencies from my local registry, but I'm not finding any way.

It seems surprising that you can't just point to a local registry and have it work.

If the crates kept in .cargo are up to the version you are wanting to archive and that is reflected in the manifest should that not work offline? Then you might only need to check the names of the archive files when setting up a new environment.

Please, stop responding to this post unless you manage to have a local registry working. For the rest of the users in this forum, please reach out if you know how to address the setup of local registries or how to include the downloaded index and corresponding crates in an existing CARGO_HOME.

I'm not sure if it's ever possible to make the local registry available with only file:// URLs, but I've built a small server to be used in this case. It's obviously non-optimal, but, well, it worked, at least back then.

instead of change the default registry, did you try to replace the source of the crates-io registry? I don't know whether it works offline though.

[source.my-source]
local-registry = "path/to/registry"
# besides `local-registry`, other source types can be used, e.g. registry/git/directory

[source.crates-io]
replace-with = "my-source"

you can read this section of the manual:

https://doc.rust-lang.org/cargo/reference/source-replacement.html

2 Likes

Thanks for the comment, this one worked in fact. If anyone has the same problem in the future, to reproduce my solution, run the next:

Create a folder that corresponds to the registry (e.g., /path/to/registry, we map the contents of the registry into the own registry: ln -s /path/to/crates.io-index/ /path/to/registry/index and fd . /path/to/downloaded/crates --extension=crate --exec ln -s {} /path/to/registry/. The contents of .cargo/config.toml within the crate are:

[net]
offline = true

[source]
[source.mirror]
local-registry = "/path/to/registry"

[source.crates-io]
replace-with = "mirror"
2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.