← Back to Blog

Cargo "failed to download" Error from crates.io Behind Proxy: Analyzing config.toml

We analyze the reasons for the "failed to download" error in Cargo behind a corporate or regional proxy and show a working configuration for config.toml, environment variables, and CI/CD.

📅November 30, 2026

If you see error: failed to download from registry or an infinite hang at the Updating crates.io index stage while building a Rust project, the reason is almost always the same — Cargo cannot get through the corporate firewall, regional restrictions, or the configured proxy server in the system. In this article, we will discuss how to correctly specify the proxy in config.toml, through environment variables and in CI/CD, as well as the nuances of HTTP, HTTPS, and SOCKS5.

Why Cargo reports failed to download

Cargo accesses two types of resources: the crates.io index (git or sparse protocol) and the actual crate files, which are distributed via CDN. If your network is behind a corporate proxy, VPN gateway, or the provider blocks access to foreign CDNs by IP, the request hangs or times out. In the log, this appears as: failed to download from `https://static.crates.io/...`, or spurious network error.

It is important to understand: Cargo does not automatically read the system proxy settings on Windows/macOS in most versions. It looks either at environment variables or the configuration file. If neither is set up, and the network requires a proxy to go outside — the request will simply hang until it times out. A common separate reason is a corporate MITM proxy with its own TLS certificate, which causes Cargo to complain about certificate verify failed even after correctly configuring the proxy address.

Setting up the proxy via environment variables

The quickest way to test the hypothesis is to set the standard environment variables that the curl library, used internally by Cargo for HTTP transport, understands:

export http_proxy="http://user:[email protected]:8080"
export https_proxy="http://user:[email protected]:8080"
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy

cargo build --release

In Windows PowerShell, the equivalent command looks like this:

$env:HTTPS_PROXY = "http://user:[email protected]:8080"
$env:HTTP_PROXY = "http://user:[email protected]:8080"
cargo build

If the build goes through after this — the problem is indeed with network access, and you can move on to a permanent setup through the configuration file, so you don't have to set the variables in every new terminal session.

Specifying the proxy in config.toml

Cargo reads settings from two places: the global ~/.cargo/config.toml (in older versions, the file is simply named config without an extension) and the local .cargo/config.toml at the root of the project. The local file takes precedence over the global one. The basic section looks like this:

[http]
proxy = "http://user:[email protected]:8080"
timeout = 60
multiplexing = false

[https]
proxy = "http://user:[email protected]:8080"

The parameter multiplexing = false often helps when the proxy does not support HTTP/2 and returns a stream error. An increased timeout is useful on slow or heavily loaded proxy servers, especially when downloading large crates like windows-sys or ring.

To check which config is actually being applied, use:

cargo config get http.proxy
cargo config get --format=json-value http

This command appeared in relatively recent versions of Cargo and shows the final value considering all levels of configuration — global, local, and environment variables, which always override the file.

SOCKS5 and Cargo/Git limitations

An important nuance: the sections [http] and [https] in config.toml work through the curl transport, which supports both HTTP(S) proxies and SOCKS5 — for the latter, use the scheme socks5:// or socks5h:// (the latter resolves DNS on the proxy side, which is important for geo-restrictions):

[http]
proxy = "socks5h://127.0.0.1:1080"

[https]
proxy = "socks5h://127.0.0.1:1080"

The problem arises when the project pulls dependencies directly from Git repositories (git = "https://..." in Cargo.toml). By default, Cargo uses a built-in implementation of git based on libgit2, which does not always correctly pick up SOCKS5 from config.toml. In this case, a separate flag is needed, which is detailed in the section on git dependencies below.

Authentication on the proxy server

If the proxy requires a username and password, the most reliable way is to embed the credentials directly in the URL: http://login:password@host:port. If the password contains special characters (@, #, /), they need to be URL-encoded — for example, %40 instead of @. Otherwise, curl will incorrectly parse the connection string and throw an authentication error, which can easily be confused with a network problem.

For proxies with IP rotation and login binding (which is typical for residential and mobile pools), the credentials are usually static, and the change of the outgoing IP occurs automatically on the provider's side — no additional settings in Cargo are required, just specify the provided host, port, username, and password once.

TLS certificate issues

Corporate proxies often perform SSL inspection, replacing the crates.io certificate with their own root certificate. Cargo (via curl) does not trust this certificate and throws SSL certificate problem: unable to get local issuer certificate. The solution is to add the corporate CA to the system store or explicitly specify the path to the certificate:

[http]
proxy = "http://proxy.example.com:8080"
cainfo = "/etc/ssl/certs/corp-ca-bundle.pem"
check-revoke = false

Disabling certificate verification completely (ssl-version with the unsafe mode flag) is not recommended even temporarily — you lose protection against packet tampering at the transport level, and dependencies of the Rust project are downloaded in binary form and may contain arbitrary code.

Git dependencies and git-fetch-with-cli

When the proxy is set in config.toml, but the error still occurs specifically when downloading git dependencies, switching Cargo to the system git client instead of the built-in libgit2 implementation helps — the system git reliably reads the http.proxy variables from its own config:

[net]
git-fetch-with-cli = true

After this, additionally specify the proxy in ~/.gitconfig:

[http]
    proxy = http://user:[email protected]:8080
[https]
    proxy = http://user:[email protected]:8080

This combination — a separate proxy for git and a separate one for http/https in Cargo — covers almost all cases where some dependencies download normally, while others (usually through GitHub git repositories) hang.

Proxy in Docker and CI/CD

When building Rust projects in Docker or on self-hosted runners in GitLab/GitHub Actions, the proxy needs to be explicitly passed as build arguments; otherwise, each build layer will try to reach crates.io directly and fail due to the container network timeout:

FROM rust:1.82-slim AS builder
ARG HTTP_PROXY
ARG HTTPS_PROXY
ENV http_proxy=$HTTP_PROXY
ENV https_proxy=$HTTPS_PROXY

WORKDIR /app
COPY . .
RUN cargo build --release

The image build is then started like this:

docker build \
  --build-arg HTTP_PROXY=http://user:[email protected]:8080 \
  --build-arg HTTPS_PROXY=http://user:[email protected]:8080 \
  -t my-rust-app .

In CI/CD, where builds are launched in parallel across dozens of jobs, the stability of the proxy is critical — connection drops lead to repeated rebuilds of the entire dependency cache and increased pipeline time. For such scenarios, it is wiser to use static datacenter proxies — they provide consistently low latency and high throughput, which is important when downloading crates in parallel jobs.

Which type of proxy to choose for the developer

The choice of proxy type for working with Cargo depends on the reason for the blockage. If the issue is with the corporate firewall or internal network — almost any stable HTTP(S) proxy with good throughput will suffice. However, if crates.io or GitHub is inaccessible due to regional IP restrictions, a static datacenter proxy may itself end up in a blocked subnet range — in this case, residential proxies with IPs of real users, which rarely fall under mass CDN blocks, come to the rescue.

Proxy Type When Suitable for Cargo Features
Datacenter CI/CD, internal mirrors, stable network High speed, low cost, static IP
Residential Bypassing regional CDN blocks for crates.io IPs of real providers, lower risk of blocking
Mobile Extreme cases with strict ASN filtering Highest level of trust, but more expensive and slower

For most developers who just want to build a project behind a corporate proxy, datacenter solutions are sufficient. Mobile proxies make sense to connect only if even residential IPs start hitting rate limits on the CDN crates.io side, which happens rarely but can occur during aggressive parallel downloads of dependencies in large monorepositories.

Diagnosis checklist

  • Check the availability of the proxy directly: curl -x http://host:port https://crates.io
  • Ensure that the environment variables do not conflict with config.toml — they take precedence
  • For git dependencies, enable git-fetch-with-cli = true
  • Check the path to the CA certificate if the proxy performs SSL inspection
  • Increase the timeout in the [http] section for slow proxies
  • In Docker, pass the proxy through ARG and ENV, rather than relying on host variables
  • Clear the registry cache ~/.cargo/registry if the index is corrupted after a connection drop

Conclusion

The error failed to download in Cargo is almost always resolved by properly configuring the proxy at three levels: environment variables for quick checks, config.toml for permanent configuration, and a separate git setup for dependencies from repositories. Add to this proper handling of TLS certificates and proxy passing in Docker builds — and most issues with crates.io will disappear without the need to roll back Rust versions or disable security checks.

If the reason for the blockage is not the corporate network but regional restrictions on the CDN side, the most stable solution is residential proxies: they use IPs of real users and are less likely to fall under mass filtering. For CI/CD pipelines with high load and predictable speed, datacenter proxies are better suited, and in rare cases of strict ASN filtering, mobile proxies can come to the rescue.