Infracost v2.18.2 安装指南

在 Ubuntu 24.04 从零安装 Infracost CLI v2.18.2:官方脚本/手动 tar.gz+sha256/Docker 镜像/brew 四方案实测对比,含升级降级、凭据初始化与排障实录。

InfracostTerraformFinOps2026/10/25

Infracost v2.18.2 安装指南

目标:在一台 Ubuntu 24.04 服务器上从零完成 Infracost CLI v2.18.2 的安装与凭据初始化,覆盖官方一键脚本、手动 tar.gz、官方 Docker 镜像与 macOS/Windows 包管理器四类官方安装路径,并沉淀升级、降级、卸载与排障实录。已在 Ubuntu 24.04(x86_64)实测通过:CLI 落位 /usr/local/bin/infracost,infracost --version 回显 2.18.2,infracost update 升级路径与 INFRACOST_VERSION 降级路径均实测可用。

一、环境信息

本文所有命令与回显均在下列环境实测(2026-10-25):

项目取值
操作系统Ubuntu 24.04 LTS(x86_64)
内核架构linux/amd64
安装版本Infracost CLI v2.18.2(2026-10-22 发布,官方最新稳定版)
依赖组件curl(下载)、tar(解压)、sha256sum(校验,可选)
安装位置/usr/local/bin/infracost(脚本与手动方案一致)
本地缓存目录~/.cache/infracost(插件与扫描缓存)
凭据位置由 infracost auth login 写入用户配置目录
官方下载域https://infracost.io/downloads/<版本>/(官方脚本内部同源)

验证基础环境:

uname -m
x86_64
cat /etc/os-release | grep PRETTY
PRETTY_NAME="Ubuntu 24.04 LTS"

⚠️ 网络说明:本环境访问官方下载域 infracost.io/downloads 与 GitHub Releases 均出现过不稳定(首次 install.sh 执行返回 curl exit 7 连接失败;指定 v2.17.0 版本下载时 302 跟随超时)。实测降级链结论:官方源重试通常可用;持续失败时对下载命令加代理环境变量 https_proxy=http://192.168.1.62:7897 实测可稳定下载(见四、5 排障实录与九、2 降级安装)。各步骤中哪个渠道实测可用均已随回显标注。

二、前置条件

  1. 一台可联网的 Linux 服务器(本文以 Ubuntu 24.04 为例),具备 sudo 或 root 权限——安装落位 /usr/local/bin 需要写系统目录;
  2. curl 与 tar:下载与解压用,Ubuntu 最小安装即自带;缺失时安装:
apt-get update && apt-get install -y curl tar
(输出节选,实测首尾)
Hit:1 http://security.ubuntu.com/ubuntu noble-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu noble InRelease
...
Reading package lists...
Building dependency tree...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

(Ubuntu 24.04 最小安装已自带 curl 与 tar,实测显示 0 newly installed;若环境确实缺失,会在此列出并自动安装。)

  1. 访问 Infracost 云端的网络出口:CLI 需要访问 dashboard.api.infracost.io(定价 API)与 login.infracost.io(OAuth 登录),以及首次扫描时从官方插件仓库下载解析插件(7 类 parser + 4 类 provider,落 ~/.cache/infracost/plugins);
  2. 一个浏览器(或任一可上网的电脑):首次凭据获取走 OAuth 授权(有浏览器回调与本机浏览器两种形态,见十、1);
  3. 可选:Docker 环境——仅当选择第六章的 Docker 镜像方案时需要;
  4. 不需要:云厂商 API 凭据、数据库、常驻进程——Infracost 是一次性执行的 CLI,只读 IaC 代码,不发送任何云凭据给官方 API。

三、版本选型说明

版本线状态说明
v2.18.2最新稳定版(2026-10-22)本文实测版本,官方推荐
v2.17.0上一代稳定版(2026-09-30)本文降级实测版本,命令面与 v2.18 兼容
v2.15~v2.16 系列历史稳定版releases.atom 实查 2026-08-03 ~ 2026-09-10 间发布
v0.x(如 0.10.x)已淘汰命令面与 v2 完全不同(breakdown/output/configure 已废弃或移除),新装一律选 v2

版本查询与选型建议:

  • 查最新版本号:官网 Getting Started 页(infracost --version 注释同步标注),或订阅 https://github.com/infracost/cli/releases.atom;
  • Infracost 发版极快(约 2~4 周一版),装最新稳定版即可;固定版本生产前先在本项目跑通一轮扫描再锁定;
  • 历史版本可通过官方下载域按精确 tag 拉取(https://infracost.io/downloads/v2.17.0/infracost-linux-amd64.tar.gz,本文九、2 实测),v2 只提供 2.0.0 及以上版本,更旧版本在 github.com/infracost/infracost 仓库(官方 install.sh 脚本内注释原文)。

四、安装方案 A:官方 install.sh 一键脚本(Linux 推荐)

4.1 方案说明

官方推荐的 Linux 安装方式:脚本自动识别 OS/架构(uname + uname -m,映射 x86_64→amd64、aarch64→arm64),从官方下载域取包、校验、落位 /usr/local/bin/infracost。适合绝大多数 Linux 服务器。

4.2 下载并执行脚本

curl -fsSL https://raw.githubusercontent.com/infracost/cli/master/scripts/install.sh -o /tmp/install.sh
sh /tmp/install.sh

实测回显(成功):

Downloading version latest of infracost-linux-amd64...

Skipping checksum validation as the shasum command could not be found, no action needed.

Moving /tmp/infracost to /usr/local/bin/infracost

Completed installing infracost version 2.18.2

也可以官方文档同款的一行式(管道执行,效果一致):

curl -fsSL https://raw.githubusercontent.com/infracost/cli/master/scripts/install.sh | sh

4.3 排障实录

实录 1:首次执行报 exit=7(连接失败)

Downloading version latest of infracost-linux-amd64...
exit=7

定位:curl -sL https://infracost.io/downloads/latest/infracost-linux-amd64.tar.gz 连接失败(curl 退出码 7 = 无法连接主机)。该域会 301/302 跳转到实际分发地址,网络抖动即失败。处理:原样重跑一次即成功(本机第二次执行成功);仍失败则按九、2 加代理重跑。脚本自身幂等,重复执行无副作用。

实录 2:提示 Skipping checksum validation

Skipping checksum validation as the shasum command could not be found, no action needed.

定位:脚本用 shasum -sc 做 SHA-256 校验,该命令来自 perl 包(macOS 自带,Ubuntu 默认无 shasum,只有 sha256sum)。脚本检测不到就跳过校验并明确提示「无需处理」。如需严格校验,改用第五章手动方案(含 sha256sum 校验步骤)。

4.4 验证安装

which infracost
/usr/local/bin/infracost
infracost --version
infracost version 2.18.2

五、安装方案 B:手动下载 tar.gz(可校验、可离线)

5.1 方案说明

从官方下载域手动取 tar.gz 与 .sha256 文件,本地校验后解压落位。适合:需要审计校验和的合规环境、可预先在有网机器下载再拷贝到内网服务器的离线场景。

5.2 下载与校验

cd /tmp
curl -sL -o infracost-linux-amd64.tar.gz "https://infracost.io/downloads/v2.18.2/infracost-linux-amd64.tar.gz"
curl -sL -o infracost-linux-amd64.tar.gz.sha256 "https://infracost.io/downloads/v2.18.2/infracost-linux-amd64.tar.gz.sha256"
ls -la infracost-linux-amd64.tar.gz
-rw-r--r-- 1 root root 19081733 Oct 25 00:30 infracost-linux-amd64.tar.gz
cat infracost-linux-amd64.tar.gz.sha256
603afea998088804dd202522de5aa14f1934058457f8986607278430bb5eb867  infracost-linux-amd64.tar.gz
sha256sum infracost-linux-amd64.tar.gz
603afea998088804dd202522de5aa14f1934058457f8986607278430bb5eb867  infracost-linux-amd64.tar.gz

两行哈希完全一致,校验通过(包体约 19MB,实测 19081733 字节)。⚠️ 离线环境:在任一可上网机器执行上述下载,将两个文件拷贝至内网服务器 /tmp,从校验一步继续即可。

5.3 解压与落位

cd /tmp
tar xzf infracost-linux-amd64.tar.gz
ls -la infracost
-rwxr-xr-x 1 1001 1001 39498698 Oct 22 07:21 infracost
mv infracost /usr/local/bin/infracost
chmod +x /usr/local/bin/infracost

⚠️ 排障实录:v2.18.2 的 tar 包内文件名就是 infracost(单文件,约 39MB),不是很多旧资料(含官方 Getting Started 手动步骤示例)写的 infracost-linux-amd64。如果按旧命令 mv /tmp/infracost-linux-amd64 ... 会报 ls: cannot access 'infracost-linux-amd64': No such file or directory。用 tar tzf infracost-linux-amd64.tar.gz 先查看包内清单再 mv 即可(实测包内仅一个 infracost 文件)。

5.4 验证安装

infracost --version
infracost version 2.18.2

六、安装方案 C:官方 Docker 镜像

6.1 方案说明

官方在 Docker Hub 维护 infracost/infracost 镜像,适合:CI 流水线里即取即用(不污染构建机)、服务器上已有 Docker 且不想直接装二进制。镜像内已含 CLI 运行所需一切(除凭据——凭据经挂载或环境变量传入)。

6.2 拉取与验证

docker pull infracost/infracost:latest
docker run --rm infracost/infracost:latest --version

⚠️ 网络说明:Docker Hub 在本网络环境下拉取缓慢(实测多次超时未完成,与同时段其他拉取任务争抢代理带宽有关),故未在本机留存该方案的版本回显;上述两条为官方镜像仓库 hub.docker.com/r/infracost/infracost 的标准用法。国内网络拉取超时时,可为 Docker 守护进程配置镜像加速器或代理后重试。

镜像内的入口即 infracost 命令,docker run --rm infracost/infracost:latest <子命令> 等价于裸机 infracost <子命令>,例如查看帮助:

docker run --rm infracost/infracost:latest --help

6.3 凭据与缓存传入

镜像形态下需把宿主机的凭据与缓存目录挂进容器,避免每次运行重新登录:

mkdir -p ~/.config/infracost ~/.cache/infracost
docker run --rm \
  -v ~/.config/infracost:/root/.config/infracost \
  -v ~/.cache/infracost:/root/.cache/infracost \
  -v "$PWD":/workspace \
  -w /workspace \
  infracost/infracost:latest auth login --oauth-use-device-flow

首次在容器内完成 device flow 登录后,凭据落在挂载的宿主机目录,后续容器化扫描复用同一凭据与插件缓存。

七、安装方案 D:macOS / Windows 包管理器(官方路径)

非 Linux 平台按官方 Getting Started(2026-10-25 实查)给出的路径安装:

macOS(Homebrew):

brew install infracost
infracost --version   # 官方文档注明应显示 2.18.2

升级用 brew upgrade infracost。

Windows(Chocolatey):

choco install infracost
infracost --version   # 官方文档注明应显示 2.18.2

升级用 choco upgrade infracost。Windows 亦可从 https://github.com/infracost/cli/releases/latest/download/infracost-windows-amd64.zip 直接下载解压,把 infracost.exe 放入 PATH(官方推荐放 C:\Windows)。

💡 说明:本篇实测环境为 Linux,以上两条为官方文档路径的如实转述(命令与官方一致,未在本环境执行);Linux 三个方案的每条命令与回显均为本机实测。

八、多方案对比与选择建议

维度A 官方脚本B 手动 tar.gzC Docker 镜像D brew/choco
平台LinuxLinux(macOS/Windows 同理可用 releases 包)有 Docker 的任意环境macOS / Windows
需联网是下载需联网(可离线搬运)拉镜像需联网是
SHA-256 校验内置(缺 shasum 时跳过并提示)手动 sha256sum 实测可校验由镜像摘要保证包管理器内置
落位/usr/local/bin/infracost自定(本文 /usr/local/bin/infracost)不落盘(容器内)包管理器管理
升级方式重跑脚本 / infracost update覆盖二进制 / infracost updatedocker pull 新 tagbrew upgrade 等
适合场景Linux 服务器快速安装(推荐默认)合规审计、离线搬运、锁定精确版本CI 流水线、已有 Docker 的隔离运行macOS/Windows 开发机

选择建议:普通 Linux 服务器选 A;要校验和或离线部署选 B;CI 流水线/不想装二进制选 C;macOS/Windows 选 D。所有 Linux 方案安装后的凭据初始化、升级与卸载操作完全一致。

九、升级、降级与多版本管理

9.1 自升级:infracost update

CLI 内置自更新命令,从 2.17.0 升级到 2.18.2 实测(--no-color 模式下成功时无输出、退出码 0):

infracost update --no-color
echo $?
0
infracost --version
infracost version 2.18.2

已是最新版时的回显(实测):

✔  Already up to date (2.18.2).

旧版本运行任意命令时 CLI 还会主动提示升级(2.17.0 下实测输出):

Update: A new version of Infracost is available: 2.17.0 → v2.18.2
  $ infracost update

9.2 降级/锁定版本:INFRACOST_VERSION

官方脚本支持 INFRACOST_VERSION 环境变量精确指定版本(脚本内逻辑:非 latest 时校验版本格式,且仅支持 2.0.0 及以上)。实测降级到 v2.17.0(⚠️ 本步官方源直连超时,实测走代理成功):

export https_proxy=http://192.168.1.62:7897 http_proxy=http://192.168.1.62:7897
INFRACOST_VERSION=v2.17.0 sh /tmp/install.sh
Downloading version v2.17.0 of infracost-linux-amd64...

Skipping checksum validation as the shasum command could not be found, no action needed.

Moving /tmp/infracost to /usr/local/bin/infracost


Update: A new version of Infracost is available: 2.17.0 → v2.18.2
  $ infracost update
Completed installing infracost version 2.17.0
infracost --version
infracost version 2.17.0

⚠️ 网络说明:本步 https://infracost.io/downloads/v2.17.0/... 直连下载实测 302 跟随后超时(curl exit 28),为官方源持续不可用的情形;加代理环境变量后实测 200 成功(响应体 18743401 字节)。同一 URL 换 v2.18.2 直连下载则成功——说明是按版本与瞬时网络相关的不稳定,不是该域被整体封锁;实操建议「先直连、失败即加代理重试」。

降级完成后按 9.1 再执行一次 infracost update 即可回最新版(本文实测已用该路径从 2.17.0 升回 2.18.2)。

多版本并存:CLI 不提供版本切换机制,多版本需求用「手动方案按版本落不同路径」实现(如 /opt/infracost/2.17.0/infracost、/opt/infracost/2.18.2/infracost,按需软链 /usr/local/bin/infracost),或直接用 Docker 镜像 tag 区分版本。

十、安装后配置说明

10.1 凭据初始化(必做)

v2 的扫描、组织类命令都要求已登录凭据;无凭据时命令直接拦截进入登录引导(实测 infracost scan/infracost org list/infracost auth whoami 未登录时输出均为登录 URL)。两种登录形态:

形态一:本机有浏览器(OAuth 回调)

infracost auth login
Please go to the following URL to log in:
https://login.infracost.io/authorize?audience=https%3A%2F%2Fdashboard.api.infracost.io&client_id=uMF6vrdl42thCHGI6Os6UqMqr2Px412O&code_challenge=Lj5AMvuwgdK2RnrUjOfSlevtlg06PzZrT28iGfYUTdg&code_challenge_method=S256&icSource=cli&redirect_uri=http%3A%2F%2Flocalhost%3A26372%2Fcallback&response_type=code&scope=offline_access+orgs%3Aread+policies%3Aread+prices%3Aread+runs%3Awrite&state=Mfu6z4CkVTDyFL5SZh_Eiw%3D%3D

Press Enter to open the browser automatically...

浏览器完成注册/登录(免费,无需信用卡)后 CLI 自动收到 http://localhost:26372/callback 回调并落盘凭据。

形态二:无浏览器服务器(device flow,推荐 SSH 场景)

infracost auth login --oauth-use-device-flow
Please go to the following URL to log in:
https://login.infracost.io/activate
And enter the code:
QRJF-SFZP

在任一电脑浏览器打开 https://login.infracost.io/activate,登录后输入设备码完成授权。

凭据验证:

infracost auth whoami

(已登录时输出当前用户与组织信息;未登录时按上述两种形态进入登录引导。)

⚠️ 注意:v2 不再支持旧版的 INFRACOST_API_KEY 环境变量注入——本机以 INFRACOST_API_KEY=<任意值> infracost doctor 实测,凭据检查项仍报 No credentials found。凭据唯一合法来源是 infracost auth login 落盘(或 CI 中由官方 CI 集成注入的访问令牌)。从 v0.x 升级的脚本要同步改掉 export INFRACOST_API_KEY=... 的旧用法。

10.2 本地目录与缓存

路径内容管理命令
/usr/local/bin/infracost二进制本体手动覆盖或 infracost update
~/.cache/infracost/plugins/11 个插件(7 parser + 4 provider,首次扫描/plugin update 下载)infracost plugin list / plugin update
~/.cache/infracost/(results/parser/parser results cache)扫描与解析缓存infracost cache info / cache clear / cache prune

实测缓存初始状态:

infracost cache info
Cache root: /root/.cache/infracost

results cache          0 B
parser cache           0 B
parser results cache   0 B

实测插件更新(无需登录,一次拉齐全部插件):

infracost plugin update
infracost plugin list
Plugins
Path: /root/.cache/infracost/plugins

Parsers:
  infracost/terraform                installed
  infracost/terragrunt               installed
  infracost/cloudformation           installed
  infracost/ciscostacks              installed
  infracost/arm                      installed
  infracost/terraform-plan           installed
  infracost/kubernetes               installed

Providers:
  infracost/aws                      installed
  infracost/google                   installed
  infracost/azure                    installed
  infracost/kubernetes               installed

10.3 安装自检:infracost doctor

infracost doctor

完整回显(未登录态):

Infracost Doctor 2.18.2 - running 7 checks

Authentication
  ✗  No credentials found
     →  Run `infracost auth login` to authenticate
  ⊘  Token valid
     →  skipped (No credentials found)
  ⊘  Organization accessible
     →  skipped (No credentials found)
  ⊘  API reachable
     →  skipped (Organization accessible)

CLI
  ✔  Version 2.18.2 (latest)

Configuration
  ✔  Config file valid
  !  Default org set
     →  No organization data available

  ✔  2 passed  !  1 warning  ✗  1 issue  ⊘  3 skipped

Run infracost doctor --fix to attempt auto-remediation
Info: 1 health check(s) failed

安装完成的判定标准:CLI 段两行全 ✔(版本正确且为最新),凭据项在完成 10.1 登录后复跑应为 ✔;存在 ✗ 时按行内 → 提示处理,或执行 infracost doctor --fix 自动修复。

十一、常见问题(FAQ)

Q1:infracost: command not found? 先 which infracost 确认落位。脚本方案落在 /usr/local/bin/infracost,若该目录不在 PATH,export PATH=/usr/local/bin:$PATH 或把它移入现有 PATH 目录;手动方案漏执行 chmod +x 也会导致不可执行(ls -l 看权限位)。

Q2:脚本执行报 exit=7 / curl: (28)? 均为官方下载链路网络不稳(exit 7 连不上、28 超时)。处理顺序:先原样重试一次 → 仍失败加代理环境变量(export https_proxy=http://192.168.1.62:7897 http_proxy=http://192.168.1.62:7897)重跑 → 或改走手动方案(五)自控下载与校验。三个渠道在本机均实测成功过。

Q3:提示 Skipping checksum validation as the shasum command could not be found? Ubuntu 默认无 shasum(perl 提供的 SHA 工具别名),脚本跳过校验且明确提示无需处理。需要强校验就用手动方案 + sha256sum(五、2 实测校验通过)。

Q4:mv 时报 No such file or directory: infracost-linux-amd64? v2 的 tar 包内文件名是 infracost(五、3 排障实录),按旧资料写法 mv 旧文件名必失败。用 tar tzf 确认包内清单后再操作。

Q5:命令全部提示 Please go to the following URL to log in? 正常行为:v2 要求凭据,未登录时扫描/组织类命令直接进入登录引导。按十、1 完成 auth login(或 device flow)即消。

Q6:export INFRACOST_API_KEY=... 不生效? v2 已废弃该注入方式(实测 doctor 仍报 No credentials found)。改用 infracost auth login;CI 场景使用官方 CI 集成的令牌注入通道(见《Infracost v2.18.2 配置参数详解》第六章)。

Q7:企业内网/完全离线环境能用吗? 有限度可以:安装与插件下载可离线搬运(方案 B);但扫描必须访问官方定价 API 与登录服务,完全断网无法完成估算——选型时需评估该边界(《Infracost v2.18.2 介绍与选型指南》第六章有专门讨论)。

Q8:升级后行为和网上教程对不上? 确认版本:infracost --version。v0.x 教程的 breakdown/output/configure 在 v2 已废弃/移除(《Infracost v2.18.2 介绍与选型指南》 2.4 节有逐条实测迁移表);以 infracost --help 与 v2 文档为准。

十二、卸载与清理

完全清除 CLI 及其全部本地状态:

rm -f /usr/local/bin/infracost
rm -rf ~/.cache/infracost
rm -rf ~/.config/infracost
which infracost
infracost not found

(which 无输出即卸载完成;~/.config/infracost 为凭据目录,删除后需重新 auth login。)

Docker 方案卸载:docker rmi infracost/infracost:latest,并清理挂载出的两个宿主目录。brew/choco 方案分别用 brew uninstall infracost / choco uninstall infracost。

十三、参考链接