How to Install Prometheus on Ubuntu 24.04

|
Published:
|
|

In this guide, you will learn how to install Prometheus on Ubuntu 24.04. Prometheus is an open-source systems and service monitoring system. It collects metrics from configured targets via HTTP calls at given intervals, evaluates rule expressions, displays the results, and can trigger alerts if some conditions are met.

Installing Prometheus on Ubuntu 24.04

Prometheus Components

Prometheus is made up various components;

  • The main Prometheus server which scrapes and stores time series data
  • Client libraries for instrumenting application code
  • Push gateway for supporting short-lived jobs
  • Exporters for exporting existing metrics from third-party systems as Prometheus metrics in cases where it is not feasible to instrument a given system with Prometheus metrics directly for example in services like HAProxy, StatsD, Graphite, etc.
  • Alertmanager to handle alerts.

Prometheus Releases

Prometheus and its other components are available on the default Ubuntu 24.04 repositories. However, the available versions may not be up-to-date. You can verify the available versions by running the command below;

apt-cache policy prometheus
prometheus:
  Installed: (none)
  Candidate: 2.45.3+ds-2build1
  Version table:
     2.45.3+ds-2build1 500
        500 http://de.archive.ubuntu.com/ubuntu noble/universe amd64 Packages

The current release as of this writing is v2.51.2, hence the package distributed by the default Ubuntu repos is a bit old.

Install Prometheus Using Pre-compiled Binaries

To ensure that you got the latest versions of Prometheus installed, you can use the pre-compiled binaries.

Create Prometheus System User and Group

Before you can begin the installation, you have to create Prometheus system user and group as shown below;

sudo useradd -M -r -s /bin/false prometheus

Create Prometheus Directories

Next, you need to create the directories that will be used to store Prometheus configurations files and other data.

sudo mkdir /etc/prometheus /var/lib/prometheus

Download Prometheus Binary

Next, navigate to the downloads section and grab the latest version of Prometheus on the downloads section. You simply use wget to download it.

Replace the value of the VER variable with the current release version.

VER=2.51.2
wget https://github.com/prometheus/prometheus/releases/download/v$VER/prometheus-$VER.linux-amd64.tar.gz

Install Prometheus

Once you have downloaded the binary, extract it and proceed to install it as follows.

tar xzf prometheus-$VER.linux-amd64.tar.gz

Next, copy the prometheus and promtool binaries under the extracted archive folder to /usr/local/bin directory.

sudo cp prometheus-$VER.linux-amd64/{prometheus,promtool} /usr/local/bin/

After copying, set the user and group ownership of these binaries to prometheus.

sudo chown prometheus:prometheus /usr/local/bin/{prometheus,promtool}

Next, copy the consoles and console_libraries directories to /etc/prometheus.

sudo cp -r prometheus-$VER.linux-amd64/{consoles,console_libraries} /etc/prometheus/

Create Prometheus Configuration file

The default Prometheus configuration file is located on the extracted archive folder. For the demonstration purposes, we will just copy it to Prometheus configuration directory and modify it as follows;

sudo cp prometheus-$VER.linux-amd64/prometheus.yml /etc/prometheus/

In the default configuration there is a single job, called prometheus , which scrapes the time series data exposed by the Prometheus server. The job contains a single, statically configured, target, the localhost on port 9090.

See the sample configuration contents;

cat /etc/prometheus/prometheus.yml
# my global config
global:
  scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  # scrape_timeout is set to the global default (10s).

# Alertmanager configuration
alerting:
  alertmanagers:
    - static_configs:
        - targets:
          # - alertmanager:9093

# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml"

# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
  # The job name is added as a label `job=` to any timeseries scraped from this config.
  - job_name: "prometheus"

    # metrics_path defaults to '/metrics'
    # scheme defaults to 'http'.

    static_configs:
      - targets: ["localhost:9090"]

Update Ownership of Prometheus Configurations

Next, set the user and group ownership of Prometheus configuration directory, /etc/prometheus to prometheus.

sudo chown -R prometheus:prometheus /etc/prometheus

Once that is done, similarly set the user and group ownership of Prometheus data directory, /var/lib/prometheus/ to prometheus.

sudo chown -R prometheus:prometheus /var/lib/prometheus

Running Prometheus on Ubuntu 24.04

At the very least, Prometheus is now set and is ready to run. However, at this point we do not have the Prometheus service configuration file and hence, we can run it in standalone mode by executing the command below;

sudo prometheus --config.file=/etc/prometheus/prometheus.yml
ts=2024-05-06T18:28:06.470Z caller=main.go:573 level=info msg="No time or size retention was set so using the default time retention" duration=15d
ts=2024-05-06T18:28:06.470Z caller=main.go:617 level=info msg="Starting Prometheus Server" mode=server version="(version=2.51.2, branch=HEAD, revision=b4c0ab52c3e9b940ab803581ddae9b3d9a452337)"
ts=2024-05-06T18:28:06.470Z caller=main.go:622 level=info build_context="(go=go1.22.2, platform=linux/amd64, user=root@b63f02a423d9, date=20240410-14:05:54, tags=netgo,builtinassets,stringlabels)"
ts=2024-05-06T18:28:06.470Z caller=main.go:623 level=info host_details="(Linux 6.8.0-31-generic #31-Ubuntu SMP PREEMPT_DYNAMIC Sat Apr 20 00:40:06 UTC 2024 x86_64 monitor01 (none))"
ts=2024-05-06T18:28:06.470Z caller=main.go:624 level=info fd_limits="(soft=1048576, hard=1048576)"
ts=2024-05-06T18:28:06.470Z caller=main.go:625 level=info vm_limits="(soft=unlimited, hard=unlimited)"
ts=2024-05-06T18:28:06.472Z caller=web.go:568 level=info component=web msg="Start listening for connections" address=0.0.0.0:9090
ts=2024-05-06T18:28:06.473Z caller=main.go:1129 level=info msg="Starting TSDB ..."
ts=2024-05-06T18:28:06.473Z caller=tls_config.go:313 level=info component=web msg="Listening on" address=[::]:9090
ts=2024-05-06T18:28:06.473Z caller=tls_config.go:316 level=info component=web msg="TLS is disabled." http2=false address=[::]:9090
ts=2024-05-06T18:28:06.475Z caller=head.go:616 level=info component=tsdb msg="Replaying on-disk memory mappable chunks if any"
ts=2024-05-06T18:28:06.475Z caller=head.go:698 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=4.781µs
ts=2024-05-06T18:28:06.475Z caller=head.go:706 level=info component=tsdb msg="Replaying WAL, this may take a while"
ts=2024-05-06T18:28:06.477Z caller=head.go:778 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
ts=2024-05-06T18:28:06.477Z caller=head.go:815 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=25.788µs wal_replay_duration=2.127592ms wbl_replay_duration=121ns total_replay_duration=2.307237ms
ts=2024-05-06T18:28:06.479Z caller=main.go:1150 level=info fs_type=EXT4_SUPER_MAGIC
ts=2024-05-06T18:28:06.480Z caller=main.go:1153 level=info msg="TSDB started"
ts=2024-05-06T18:28:06.480Z caller=main.go:1335 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
ts=2024-05-06T18:28:06.480Z caller=main.go:1372 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=431.686µs db_storage=6.221µs remote_storage=2.143µs web_handler=210ns query_engine=1.335µs scrape=147.474µs scrape_sd=16.851µs notify=20.341µs notify_sd=6.502µs rules=2.285µs tracing=9.136µs
ts=2024-05-06T18:28:06.480Z caller=main.go:1114 level=info msg="Server is ready to receive web requests."
ts=2024-05-06T18:28:06.480Z caller=manager.go:163 level=info component="rule manager" msg="Starting rule manager..."

By default, it will be listening on port 9090/TCP.

Open another terminal and confirm;

sudo ss -altnp
State             Recv-Q            Send-Q                       Local Address:Port                       Peer Address:Port            Process            
LISTEN            0                 128                                0.0.0.0:22                              0.0.0.0:*                                  
LISTEN            0                 128                                   [::]:22                                 [::]:*                                  
LISTEN            0                 4096                                     *:9090                                  *:*

You can stop the process above by pressing CTRL+C.

Create Prometheus Systemd Service File

To be able to run prometheus as a service, you can create a systemd service configuration file as shown below;

sudo tee /etc/systemd/system/prometheus.service << 'EOF'
[Unit]
Description=Prometheus Time Series Collection and Processing Server
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
Group=prometheus
Type=simple
ExecStart=/usr/local/bin/prometheus \
    --config.file /etc/prometheus/prometheus.yml \
    --storage.tsdb.path /var/lib/prometheus/ \
    --web.console.templates=/etc/prometheus/consoles \
    --web.console.libraries=/etc/prometheus/console_libraries

[Install]
WantedBy=multi-user.target
EOF

Next, reload systemd configuration files;

sudo systemctl daemon-reload

Start and enable Prometheus to run on system boot.

sudo systemctl enable --now  prometheus

To check the status of Prometheus service;

systemctl status prometheus
● prometheus.service - Prometheus Time Series Collection and Processing Server
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; preset: enabled)
     Active: active (running) since Mon 2024-05-06 18:31:10 UTC; 11s ago
   Main PID: 3075 (prometheus)
      Tasks: 8 (limit: 4614)
     Memory: 15.8M (peak: 16.0M)
        CPU: 45ms
     CGroup: /system.slice/prometheus.service
             └─3075 /usr/local/bin/prometheus --config.file /etc/prometheus/prometheus.yml --storage.tsdb.path /var/lib/prometheus/ --web.console.templates=/etc/prometheus/consoles --web.console.libraries=/etc/>

May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.659Z caller=head.go:698 level=info component=tsdb msg="On-disk memory mappable chunks replay completed" duration=2.277µs
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.659Z caller=head.go:706 level=info component=tsdb msg="Replaying WAL, this may take a while"
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.659Z caller=head.go:778 level=info component=tsdb msg="WAL segment loaded" segment=0 maxSegment=0
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.659Z caller=head.go:815 level=info component=tsdb msg="WAL replay completed" checkpoint_replay_duration=19.565µs wal_replay_duration=254.14µs w>
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.667Z caller=main.go:1150 level=info fs_type=EXT4_SUPER_MAGIC
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.667Z caller=main.go:1153 level=info msg="TSDB started"
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.667Z caller=main.go:1335 level=info msg="Loading configuration file" filename=/etc/prometheus/prometheus.yml
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.668Z caller=main.go:1372 level=info msg="Completed loading of configuration file" filename=/etc/prometheus/prometheus.yml totalDuration=424.117>
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.668Z caller=main.go:1114 level=info msg="Server is ready to receive web requests."
May 06 18:31:10 monitor01 prometheus[3075]: ts=2024-05-06T18:31:10.668Z caller=manager.go:163 level=info component="rule manager" msg="Starting rule manager..."

Access Prometheus Web Interface

You can now access Prometheus using the address, http://<server_IP>:9090. This will take you to Prometheus’s built-in expression browser.

Install Prometheus on Ubuntu 24.04

The scraped metrics can be viewed under, http://<server_IP>:9090/metrics.

scraped metrics prometheus

To check the status of your node, navigate to Status > Targets.

prometheus targets status

Well, that is just about it on installing Prometheus on Ubuntu 24.04.

Want to learn how to monitor Linux hosts with Prometheus Node Exporter? Check the link below;

Integrate Prometheus with Grafana for Monitoring

Monitor Linux System Metrics with Prometheus Node Exporter

Monitoring Gitlab Metrics with Prometheus and Grafana

Monitor OpenVPN Connections with Prometheus and Grafana

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
Kifarunix
Linux Certified Engineer, with a passion for open-source technology and a strong understanding of Linux systems. With experience in system administration, troubleshooting, and automation, I am skilled in maintaining and optimizing Linux infrastructure.

Leave a Comment