Ubuntu 24.04 安装 Prometheus 3.x

在 Ubuntu 24.04 上通过官方预编译二进制安装 Prometheus 3.15.0(截至 2026-09-27 最新 3.x 版本),覆盖下载、解压、启动、curl 验证全过程。Prometheus 无官方 apt 源,官方提供 tar.gz 预编译包下载即用。所有命令与输出均在真实 Ubuntu 24.04 服务器上实测通过

PrometheusPrometheus安装部署2026/09/27

Ubuntu 24.04 安装 Prometheus 3.x

目标:在 Ubuntu 24.04 上通过官方预编译二进制安装 Prometheus 3.15.0(截至 2026-09-27 最新 3.x 版本),覆盖下载、解压、启动、curl 验证全过程。Prometheus 无官方 apt 源,官方提供 tar.gz 预编译包下载即用。所有命令与输出均在真实 Ubuntu 24.04 服务器上实测通过。

环境信息

信息项值
操作系统Ubuntu 24.04 LTS(noble)
系统架构x86_64
Prometheus 版本3.15.0(2026-09-24 发布,最新 3.x)
安装方式官方预编译 tar.gz(GitHub Releases)
下载包大小107 MB(prometheus-3.15.0.linux-amd64.tar.gz)
默认端口9090
默认配置文件./prometheus.yml(解压目录内自带)
数据目录自定义 /opt/prometheus-data(默认 data/)
systemd可用(本文以二进制手动启动演示,生产建议 systemd 托管)

前置条件

  • Ubuntu 24.04 系统(或等效 Debian/Ubuntu),root 或 sudo 权限
  • 网络可访问 github.com(下载 Releases 包)
  • 已安装 curl、wget、tar(本文档实测安装)

部署步骤

步骤 1. 安装基础依赖

apt-get update
apt-get install -y curl wget tar

💡 命令解释:curl/wget 用于下载安装包,tar 用于解压 tar.gz。若系统已有这些工具可跳过。

步骤 2. 确认最新版本号

Prometheus 官方下载页 https://prometheus.io/download/ 与 GitHub Releases 同步。实测用 GitHub API 获取最新 release:

curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep '"tag_name"'

实测输出:

  "tag_name": "v3.15.0",
  "name": "3.15.0 / 2026-09-24",

💡 说明:实测最新版本为 v3.15.0(2026-09-24 发布)。3.x 系列自 2023 年 3.0.0 发布后持续迭代,本文档所有命令均基于 3.15.0 实测。

步骤 3. 下载并解压官方 tar.gz

cd /opt
wget -q https://github.com/prometheus/prometheus/releases/download/v3.15.0/prometheus-3.15.0.linux-amd64.tar.gz
ls -lh prometheus-3.15.0.linux-amd64.tar.gz
tar xzf prometheus-3.15.0.linux-amd64.tar.gz
ls -la prometheus-3.15.0.linux-amd64/

实测输出:

-rw-r--r-- 1 root root 107M Sep 25 08:04 prometheus-3.15.0.linux-amd64.tar.gz
total 257300
drwxr-xr-x 2 1001 1001      4096 Sep 25 08:00 .
-rw-r--r-- 1 1001 1001     11357 Sep 25 08:00 LICENSE
-rw-r--r-- 1 1001 1001      4370 Sep 25 08:00 NOTICE
-rwxr-xr-x 1 1001 1001 144293003 Sep 25 08:00 prometheus
-rw-r--r-- 1 1001 1001      1128 Sep 25 08:00 prometheus.yml
-rwxr-xr-x 1 1001 1001 119146763 Sep 25 08:00 promtool

💡 命令解释:wget -q 静默下载;tar xzf 解压(x 解压、z gzip、f 指定文件);ls -la 列出解压内容。

💡 说明:解压目录含 4 个文件——prometheus(服务端二进制,约 144 MB)、promtool(命令行工具,约 119 MB)、prometheus.yml(默认配置)、LICENSE/NOTICE。若系统架构为 arm64,将 URL 中 linux-amd64 改为 linux-arm64 即可。

步骤 4. 验证版本

cd /opt/prometheus-3.15.0.linux-amd64
./prometheus --version

实测输出:

prometheus, version 3.15.0 (branch: HEAD, revision: 5241a27fe3c6983549fccc32f6e65917408c63cd)
  build user:       root@4635dd007a60
  build date:       20260925-07:33:45
  go version:       go1.27.1
  platform:         linux/amd64
  tags:             netgo,builtinassets

💡 命令解释:--version 打印版本、构建时间、Go 版本与平台信息,用于确认二进制可用。

步骤 5. 启动 Prometheus(systemd 托管)

生产环境建议由 systemd 托管,创建 /etc/systemd/system/prometheus.service:

[Unit]
Description=Prometheus Server
After=network.target

[Service]
ExecStart=/opt/prometheus-3.15.0.linux-amd64/prometheus \
  --config.file=/opt/prometheus-3.15.0.linux-amd64/prometheus.yml \
  --web.listen-address=0.0.0.0:9090 \
  --storage.tsdb.path=/opt/prometheus-data
Restart=always

[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now prometheus
systemctl status prometheus

启动日志(关键部分):

time=2026-09-26T21:30:08.317Z level=INFO source=main.go:933 msg="Starting Prometheus Server" mode=server version="(version=3.15.0, ...)"
time=2026-09-26T21:30:08.448Z level=INFO source=tls_config.go:381 msg="Listening on" component=web address=[::]:9090
time=2026-09-26T21:30:08.452Z level=INFO source=main.go:1523 msg="TSDB started"
time=2026-09-26T21:30:08.453Z level=INFO source=main.go:1484 msg="Server is ready to receive web requests."

💡 命令解释:--config.file 指定配置文件;--web.listen-address=0.0.0.0:9090 监听所有网卡 9090 端口(远程访问必需,默认只监听本机);--storage.tsdb.path 指定 TSDB 数据目录;systemctl enable --now 注册开机自启并立即启动。

💡 说明:日志显示 Listening on [::]:9090、TSDB started、Server is ready to receive web requests. 即启动成功。无 systemd 的极简环境可手动后台启动:nohup ./prometheus --config.file=prometheus.yml > /opt/prometheus.log 2>&1 &(生产仍建议 systemd 托管);如需 HTTP 热加载加 --web.enable-lifecycle(见常见问题)。

步骤 6. curl 验证 /metrics 与 UI

curl -s http://localhost:9090/metrics | head -10
curl -s http://localhost:9090/metrics | wc -l
curl -s http://localhost:9090/

实测输出:

# HELP go_gc_cleanups_executed_cleanups_total ...
# TYPE go_gc_cleanups_executed_cleanups_total counter
go_gc_cleanups_executed_cleanups_total 0
# HELP go_gc_cleanups_queued_cleanups_total ...
...
(共 1108 行指标)
<a href="/query">Found</a>.

💡 命令解释:/metrics 是 Prometheus 自监控暴露端点(Prometheus 默认配置抓取自身);/ 是 Web UI 根路径。

💡 说明:/metrics 返回 1108 行 Go/Prometheus 运行时指标;/ 返回 Found 即 302 重定向到 /query(3.x 全新 React UI,可用浏览器访问 http://<IP>:9090/query 打开查询页面)。

步骤 7. 验证抓取链路

Prometheus 默认配置会抓取自身,用 HTTP API 验证:

curl -s "http://localhost:9090/api/v1/query?query=up"

实测输出:

{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"up","instance":"localhost:9090","job":"prometheus"},"value":[1790458430.393,"1"]}]}}

💡 说明:up 是 Prometheus 对每个抓取目标自动生成的指标(1=抓取成功,0=失败)。返回 job="prometheus", instance="localhost:9090", value="1" 说明自监控链路已打通。

配置说明

安装后核心配置在解压目录的 prometheus.yml(3.x 默认配置,完整参数见《Prometheus 3.x 配置参数详解》):

# my global config
global:
  scrape_interval: 15s          # 抓取间隔
  evaluation_interval: 15s      # 规则评估间隔

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets: []

# Load rules once and periodically evaluate them
rule_files:
  # - "first_rules.yml"

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]
    scrape_native_histograms: true   # 3.x 默认开启原生直方图(2.x 无此行)

注意事项

  • Prometheus 无官方 apt 源,只能用官方 tar.gz 预编译包安装;不要使用第三方 PPA
  • 远程访问必须加 --web.listen-address=0.0.0.0:9090,默认只监听本机回环地址,外部无法访问
  • 数据目录权限:运行用户需对 --storage.tsdb.path 目录有读写权限
  • 3.x 默认 --web.enable-lifecycle 与 --web.enable-admin-api 均关闭,如需 HTTP 热加载/管理 API 需显式开启(实测见常见问题)
  • 生产环境建议锁定具体版本号(如 v3.15.0),不要追 latest

常见问题

问题 1:外部无法访问 9090 端口

原因: 未加 --web.listen-address=0.0.0.0:9090 时默认只监听 127.0.0.1,外部无法访问。

解决: 启动时显式加 --web.listen-address=0.0.0.0:9090:

./prometheus --config.file=prometheus.yml --web.listen-address=0.0.0.0:9090

问题 2:/-/reload 返回 403

原因: 3.x 默认关闭 lifecycle 热加载(实测未加 flag 时 curl -X POST /-/reload 返回 403)。

解决: 启动加 --web.enable-lifecycle,然后即可用 curl -X POST http://localhost:9090/-/reload 热加载配置。

问题 3:admin API 报 admin APIs disabled

原因: 实测 3.15 中 --web.enable-admin-api 仍存在且默认关闭,调用 /api/v1/admin/* 返回 {"status":"error","errorType":"unavailable","error":"admin APIs disabled"}。

解决: 启动加 --web.enable-admin-api(如用 tsdb 快照等管理功能)。

问题 4:抓取含 UTF-8 指标名的目标报 expected metric name after HELP

原因: Prometheus 3.x 虽默认启用 UTF-8 指标名,但实测经典 text/OpenMetrics 抓取路径仍拒绝非 ASCII 名称(详见《Prometheus 3.x 配置参数详解》常见问题)。

解决: 抓取目标保持 ASCII 指标名;UTF-8 名称请走 OTLP 接入(--web.enable-otlp-receiver)。

卸载方式

# 停止进程
pkill -f "prometheus --config"
# 删除解压目录与数据
rm -rf /opt/prometheus-3.15.0.linux-amd64
rm -rf /opt/prometheus-data
# 删除下载包与日志
rm -f /opt/prometheus-3.15.0.linux-amd64.tar.gz /opt/prometheus.log

参考链接


本文档由 www.quzuo.net 平台开发