opam-repository.git安装加速
参考资料
opam-repository.git安装加速
opam-repository.git 安装加速
初次配置 OCaml 开发环境时,`opam init` 会从 GitHub 克隆 `ocaml/opam-repository` 仓库。这个仓库包含大量包定义和历史提交,体积不小;在国内网络下直连 GitHub,经常出现克隆缓慢、中途超时甚至失败的情况。下面按“影响从小到大”的顺序给出几种可组合使用的加速思路。
一、先确认瓶颈在哪
加速前先判断慢的是哪一步,避免做无用功:
| 步骤 | 内容 | 常见表现 |
|---|---|---|
| 克隆仓库 | 拉取 `opam-repository.git` | 长时间卡在 `Cloning` |
| 拉取源码 | 下载各包 `.tar.gz` 源码包 | 编译阶段卡在 `Fetching` |
| 依赖解析 | opam 本地计算 | 一般很快 |
如果卡在第一步克隆仓库,用下面的镜像或本地克隆方案;如果卡在下载源码,则要另外配置源码包镜像。
二、使用国内 Git 镜像仓库
最直接的办法是把仓库地址换成国内镜像。以裸初始化后单独添加仓库为例: bash opam init --bare -y
使用镜像地址添加默认仓库(地址以镜像站当前文档为准)
opam repo add default https://mirrors.tuna.tsinghua.edu.cn/git/opam-repository.git 也可以在初始化时直接指定仓库与地址: bash opam init default https://mirrors.tuna.tsinghua.edu.cn/git/opam-repository.git --bare -y 常见可选的镜像站包括清华 TUNA、中科大 USTC、上海交大等,它们通常都提供 `git/opam-repository.git` 形式的镜像。镜像地址和可用性会变化,使用前请到对应镜像站确认最新路径。
如果镜像不可用,再切回官方源: bash opam repo set-url default https://github.com/ocaml/opam-repository.git
三、给 Git 配置代理或 URL 改写
如果你的机器能通过代理访问 GitHub,可以直接让 Git 走代理: bash git config --global http.proxy http://127.0.0.1:7890 git config --global https.proxy http://127.0.0.1:7890 或者只针对 GitHub 做 URL 改写,把请求转发到加速网关: bash git config --global url."https://你的加速网关/https://github.com/".insteadOf "https://github.com/" 这种方式对所有 Git 操作生效,包括 opam 在后台调用的 clone。用完记得清理: bash git config --global --unset http.proxy git config --global --unset https.proxy
四、本地浅克隆 + file:// 仓库(最稳)
如果镜像和代理都不稳定,可以先用浅克隆把仓库拉下来,再让 opam 直接引用本地目录。这种方式绕开了 opam 反复联网克隆的问题: bash
浅克隆,只取最新快照,体积和耗时都大幅下降
git clone --depth 1 https://github.com/ocaml/opam-repository.git ~/opam-repository
opam init --bare -y
用本地路径作为仓库源
opam repo add default file://$HOME/opam-repository 浅克隆的代价是没有历史版本,只保留当前最新状态——对日常安装使用足够。需要更新时进入目录 `git pull` 即可,后续增量很小。
五、源码包下载的额外加速
仓库克隆只是第一步,真正编译时 opam 还会下载各包的源码压缩包,同样可能慢。可以关注这几点:
- opam 会把下载内容缓存在 `~/.opam
时间:2026-10-07
来源:https://mirror.ciilii.com/
