Easiest way to manually download a crate from crates.io

This might be an unusual question, but I didn't see a simple way (yet) to just download a crate in a certain version from crates.io. For those crates that have a repository, I could go to the repository, but there is no guarantee that the versions are identical. I could also create a hello world program using the crate (by including it in Cargo.toml under [Dependencies]), enter cargo build, and then check my .cargo/registry/src/ directory, I guess.

But isn't there a simpler/direct way? Maybe like… a download button? :sweat_smile: Or something like cargo download (which doesn't seem to exist). :thinking:

Perhaps it's there, and I just haven't noticed it yet?

2 Likes

According to the Cargo book, every crate can be downloaded as a tarball via the download link, configured at the registry root. For crates.io, configuration is the default one specified in book, i.e. direct link would be like https://crates.io/api/v1/crates/serde/1.0.0/download.

3 Likes

Thanks! I just made myself this tiny shell script:

#!/bin/sh
mkdir tmp-download || exit 1
cd tmp-download || exit 1
curl -L https://crates.io/api/v1/crates/$1/$2/download | tar -xf -
mv $1-$2 ..
cd .. || exit 1
rmdir tmp-download

It might still be nice to have a way to download and add a crate to Cargo's local registry such that it won't be re-downloaded later, when I have a project depending on that version. I guess I could achieve that with a shell script as well (which creates a Cargo project, adds a dependency, builds the crate, and deletes everything). But I guess there is no such command to make that easier/faster?

Try cargo install. As far as I remember, it will also download library crates for you.

Doesn't work. I get:

% cargo install libc
    Updating crates.io index
error: there is nothing to install in `libc v0.2.107`, because it has no binaries
`cargo install` is only for installing programs, and can't be used with libraries.
To use a library crate, add it as a dependency in a Cargo project instead.

Yes, but has it not populated your local registry despite the error? I believe it did last time I tried this.

1 Like

cargo vendor can be used to downloads all dependencies.

Oh nice, cargo install indeed does populate my local registry, so I can use that when I want to peek into some crate's source code (assuming I trust the crate, as I think some installation code may be executed).

Ah, this puts a copy of the crates in a selected directory (or a vendor subdirectory) of my project directory.

How about this?

Install cargo-edit, if you did not do that already:

cargo install cargo-edit

create your project and add the crates at the version number you need:

cargo new myproject
cd myproject
cargo add serde@1.0.100
cargo add serde_json@1.0.69
cargo update -p serde --precise 1.0.100
cargo update -p serde_json --precise 1.0.69

now download

cargo fetch

or

cargo vendor vendor

GG

1 Like

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.