How to Install Prometheus on Ubuntu 24.04, 22.04 or 20.04

This guide will demonstrate how to install Prometheus on Ubuntu 24.04, 22.04, or 20.04 LTS Linux releases utilizing the command-line terminal.

Prometheus stands out as a powerful, open-source monitoring solution, enabling organizations to collect and process metrics in real-time. This system’s design focuses on reliability and scalability, addressing the complex monitoring needs of today’s dynamic service-oriented architectures. With its efficient time series database and a query language (PromQL), Prometheus offers a robust framework for gauging application performance and system health. The tool’s architecture supports multi-dimensional data collection and querying, making it a vital asset for DevOps teams and system administrators.

Key Technical Features and Highlights of Prometheus:

  • Efficient Time Series Database: Optimized for high-velocity time series data storage and retrieval.
  • Powerful Query Language (PromQL): Enables precise and complex queries for data analysis and visualization.
  • Multi-Dimensional Data Model: Supports the collection of metrics with rich labels, enhancing query flexibility.
  • Flexible Alerting: Integrates seamlessly with Alertmanager for comprehensive alerting and notification workflows.
  • Service Discovery: Automatically discovers targets in various environments, simplifying dynamic monitoring setups.
  • Scalable and Reliable: Designed to scale horizontally and ensure reliability, even in the most demanding environments.
  • Rich Visualization Options: Offers built-in support for Grafana, allowing for the creation of detailed and informative dashboards.
  • Community and Ecosystem: Benefits from a vast ecosystem of exporters and integrations, enabling monitoring of a wide array of services and applications.

With Prometheus, you gain unparalleled visibility into your systems, making it easier to identify and resolve issues before they impact your operations. The following sections will guide you through the practical steps to harness the power of Prometheus on your Ubuntu system.

Let’s dive into the technical how-to.

Update Ubuntu Before Prometheus Installation

Before proceeding to install Prometheus, run a quick update and upgrade in your terminal to ensure your system packages are up-to-date to avoid any conflicts with the following command:

sudo apt update && sudo apt upgrade

Create Prometheus User and Group

To properly configure Prometheus, you must create a dedicated system user and group for the software. This can be done using the following commands in the terminal:

First, create the “prometheus” system group using the following command:

sudo groupadd --system prometheus

The –system flag ensures that the group is created as a system group with an ID number below 1000.

Next, create the “prometheus” system user using the following command:

sudo useradd -s /sbin/nologin --system -g prometheus prometheus

This command creates a system user with the username “prometheus,” assigns the “prometheus” group as the primary group for the user, and sets the user’s login shell to /sbin/nologin to prevent interactive login.

Create Directories for Prometheus

For Prometheus to properly store its data and configuration files, you must create several directories on your Ubuntu system.

First, create the data directory for Prometheus using the following command:

sudo mkdir /var/lib/prometheus

This command creates the directory /var/lib/prometheus, where Prometheus will store its data.

Next, create the primary configuration files directory for Prometheus using the following command:

for i in rules rules.d files_sd; do sudo mkdir -p /etc/prometheus/${i}; done

This command creates the /etc/prometheus directory and several sub-directories (rules, rules.d, and files_sd) where Prometheus will store its configuration files.

Download Prometheus

Since Prometheus is unavailable in Ubuntu’s default repositories, you must download it from the official website. This can be done using the following commands in the terminal:

mkdir -p /tmp/prometheus && cd /tmp/prometheus
curl -s https://api.github.com/repos/prometheus/prometheus/releases/latest | grep browser_download_url | grep linux-amd64 | cut -d '"' -f 4 | wget -qi -

These commands create a temporary directory (/tmp/prometheus) and navigate to it, then use curl and grep to locate the latest release of Prometheus on the official GitHub repository. The cut command extracts the download link for the Linux AMD64 package, which is then passed to wget to download and save the package in the current directory.

If the curl command does not work, you can install it using the following command:

sudo apt install curl -y

Once the curl is installed, you can retry the above command to download the latest release of Prometheus.

Extract Prometheus Archive

Once you have downloaded the latest version of Prometheus for your Ubuntu system, you can extract the software using the following command:

tar xvfz prometheus-*.*.*.linux-amd64.tar.gz

This command extracts the downloaded package into the current directory (/tmp/).

Next, navigate to the extracted directory using the following command:

cd prometheus*/

This command changes your current working directory to the prometheus directory created during the extraction.

Next, move the binary files to the /usr/local/bin/ directory using the following command:

sudo mv prometheus promtool /usr/local/bin/

This command moves the prometheus and promtool binaries to the /usr/local/bin/ directory typically included in the system’s PATH variable.

To verify the installation of Prometheus, you can use the following commands:

prometheus --version
promtool --version

Example output of the versions in the Ubuntu terminal:

Verifying Promtool and Prometheus versions on Ubuntu 24.04, 22.04, and 20.04.
Display of Promtool and Prometheus version outputs on Ubuntu LTS.

These commands will display the version numbers of the prometheus and promtool binaries, respectively.

Next, move the Prometheus configuration template to the /etc/prometheus/ directory using the following command:

sudo mv prometheus.yml /etc/prometheus/prometheus.yml

This command moves the prometheus.yml configuration template to the /etc/prometheus/ directory, which can be edited and customized.

Finally, move the consoles and console_libraries directories to the /etc/prometheus/ directory using the following command:

sudo mv consoles/ console_libraries/ /etc/prometheus/
cd $HOME

These directories contain additional configuration files and libraries that can be used with Prometheus.

Create Prometheus Systemd Service

By default, Prometheus does not come with a systemd service, which makes it difficult to manage and control the software. To create a systemd service for Prometheus on your Ubuntu system, use the following commands:

First, create the systemd service file using the following command:

sudo tee /etc/systemd/system/prometheus.service<<EOF
[Unit]
Description=Prometheus
Documentation=https://prometheus.io/docs/introduction/overview/
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP \$MAINPID
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 \
  --web.listen-address=0.0.0.0:9090 \
  --web.external-url=

SyslogIdentifier=prometheus
Restart=always

[Install]
WantedBy=multi-user.target
EOF

This command creates the /etc/systemd/system/prometheus.service file and defines the necessary configuration options for the systemd service.

Next, change the directory permissions for the Prometheus user and group using the following commands:

for i in rules rules.d files_sd; do sudo chown -R prometheus:prometheus /etc/prometheus/${i}; done
for i in rules rules.d files_sd; do sudo chmod -R 775 /etc/prometheus/${i}; done
sudo chown -R prometheus:prometheus /var/lib/prometheus/

These commands ensure that the prometheus user and group have the necessary permissions to access and modify the configuration and data directories.

After changing the directory permissions, reload the systemd daemon using the following command:

sudo systemctl daemon-reload

This command reloads the systemd configuration to ensure the new Prometheus service is recognized.

Finally, start the Prometheus systemd service using the following command:

sudo systemctl start prometheus

This command starts the Prometheus service on your Ubuntu system.

If you would like the Prometheus service to start automatically at system startup, use the following command:

sudo systemctl enable prometheus

This command enables the Prometheus service to start automatically at system startup.

To check the status of the Prometheus systemd service, use the following command:

systemctl status prometheus

This command displays the current status of the Prometheus service, including any error messages or warnings that may have occurred during startup or operation.

Example status of Prometheus systemd service if successful:

Successful setup of Prometheus systemd service on Ubuntu 24.04, 22.04, and 20.04.
Confirmation of Prometheus systemd service status on Ubuntu LTS.

Configure UFW Firewall For Prometheus

The UFW (Uncomplicated Firewall) is a user-friendly front-end for managing firewall rules on Linux systems. It allows or blocks traffic based on various criteria, such as port number, protocol, and IP address. Here’s how to configure UFW to allow traffic for Prometheus:

Check the current UFW status:

Check the current UFW status. Before making any changes to UFW, it’s important to check the current status using the following command:

sudo ufw status

This command will show the current UFW status and any rules that are currently in place.

Allow traffic for Prometheus:

You need to add UFW rules for the ports Prometheus uses to allow Prometheus traffic. By default, Prometheus uses port 9090 for its web interface and port 9093 for its alert manager. You can add rules for these ports using the following commands:

sudo ufw allow 9090/tcp
sudo ufw allow 9093/tcp

These commands will allow incoming TCP traffic for ports 9090 and 9093.

Enable UFW:

Once you have added the necessary rules for Prometheus, you can enable UFW using the following command:

sudo ufw enable

This command will activate UFW and apply the rules you have added.

Here are some additional examples of UFW rules for Prometheus:

  • Allow traffic from a specific IP address:
sudo ufw allow from 192.168.1.100 to any port 9090

This command will allow incoming traffic from IP address 192.168.1.100 to port 9090.

  • Allow traffic for a specific network:
sudo ufw allow from 192.168.1.0/24 to any port 9090

The above command will allow incoming traffic from the 192.168.1.0/24 network to port 9090.

  • Block traffic for a specific IP address:
sudo ufw deny from 192.168.1.100 to any port 9090

This command will block incoming traffic from IP address 192.168.1.100 to port 9090.

These are just a few examples of using UFW to configure firewall rules for Prometheus.

Access Prometheus Web UI

After installing Prometheus, you can access its web UI to view metrics and configure alerts. Open your preferred web browsers, such as Google Chrome, Mozilla Firefox, or Microsoft Edge, and in the address bar of your web browser, enter the URL for the Prometheus web UI http://localhost:9090.

http://localhost:9090

This URL assumes that you have installed Prometheus on the same system where you are accessing the web UI. If you have installed Prometheus on a remote system, replace “localhost” with the IP address or hostname of the remote system.

Initial Prometheus dashboard view on Ubuntu 24.04, 22.04, and 20.04.
Initial setup of the Prometheus dashboard on Ubuntu LTS versions.

When you open Prometheus for the first time, you may be overwhelmed by the information and options available. Here are some general tips and customizations to help you get started with Prometheus:

  • Take some time to explore the various tabs and panels in the Prometheus web UI. The “Graph” and “Alerts” tabs are the most commonly used, but many other features and options are available.
  • Familiarize yourself with the Prometheus query language (PromQL). This language allows you to filter and aggregate metrics in various ways and is a powerful tool for analyzing your data.
  • Customize the Prometheus configuration file to suit your needs. This file includes options for service discovery, target configuration, and runtime configuration, among others.
  • Use the Prometheus documentation and community resources to learn more about the system and its features. The Prometheus website includes detailed documentation, tutorials, examples, and a community forum and Slack channel for discussion and support.

Next, here are specific things to explore in the Prometheus web UI:

  • The “Targets” tab shows the status and health of the targets Prometheus monitors.
  • The “Rules” tab allows you to create and manage alerting rules based on specific conditions in your metrics.
  • The “Console” tab provides a command-line interface for querying metrics using PromQL.
  • The “Status” tab shows the overall status and health of the Prometheus server.

Lastly, here are some customizations you can make to the Prometheus configuration file:

  • Specify different scraping intervals for different targets or jobs.
  • Use different service discovery methods, such as DNS or file-based discovery.
  • Configure various options for the alert manager, such as email or Slack notification channels.

Additional Tips for Prometheus

Example Prometheus Configurations

Monitoring a Kubernetes cluster:

This configuration file scrapes metrics from the Kubernetes API server and pods in the kube-system namespace. It includes relabeling rules to keep only the endpoints and pods with a label matching “myapp.”

global:
  scrape_interval: 30s

scrape_configs:
  - job_name: 'kubernetes-apiservers'
    kubernetes_sd_configs:
      - role: endpoints
        namespaces:
          names: ['kube-system']
        api_server: 'https://kubernetes.default.svc.cluster.local'
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          cert_file: /var/run/secrets/kubernetes.io/serviceaccount/client.crt
          key_file: /var/run/secrets/kubernetes.io/serviceaccount/client.key
    relabel_configs:
      - source_labels: [__meta_kubernetes_namespace, __meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
        action: keep
        regex: default;kubernetes;https

  - job_name: 'kubernetes-pods'
    kubernetes_sd_configs:
      - role: pod
        namespaces:
          names: ['kube-system']
        api_server: 'https://kubernetes.default.svc.cluster.local'
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
          cert_file: /var/run/secrets/kubernetes.io/serviceaccount/client.crt
          key_file: /var/run/secrets/kubernetes.io/serviceaccount/client.key
    relabel_configs:
      - source_labels: [__meta_kubernetes_pod_label_app]
        action: keep
        regex: myapp

Monitoring a web application:

This configuration file scrapes metrics from a web application running on my-webapp.example.com:8080. It includes relabeling rules to set the instance label based on the target IP address and port number.

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: 'my-webapp'
    metrics_path: /metrics
    static_configs:
      - targets: ['my-webapp.example.com:8080']
    relabel_configs:
      - source_labels: [__address__]
        action: replace
        target_label: instance
        regex: (.*):8080
      - source_labels: [__address__]
        action: replace
        target_label: __address__
        regex: (.*):8080

Monitoring a database:

This configuration file scrapes metrics from a database running on my-database.example.com:5432. It includes relabeling rules to set the instance label based on the target IP address and port number.

global:
  scrape_interval: 30s

scrape_configs:
  - job_name: 'my-database'
    metrics_path: /metrics
    static_configs:
      - targets: ['my-database.example.com:5432']
    relabel_configs:
      - source_labels: [__address__]
        action: replace
        target_label: instance
        regex: (.*):5432
      - source_labels: [__address__]
        action: replace
        target_label: __address__
        regex: (.*):5432

Conclusion

We’ve journeyed through the steps to install Prometheus on Ubuntu, specifically for versions 24.04, 22.04, and 20.04. This guide aimed to simplify what might seem like a complex process, breaking it down into manageable, bite-sized pieces. Now that Prometheus is up and running on your system, you’re set to dive into monitoring and metrics collection with greater ease. Remember, the real power of Prometheus comes into play as you explore its querying capabilities and integrate it with other tools for comprehensive monitoring solutions. Keep experimenting, stay curious, and most importantly, don’t hesitate to reach out to the community for tips and tricks. Happy monitoring!

Leave a Comment