How to Setup Prometheus and Grafana on Ubuntu 22.04

Introduction

Prometheus and Grafana are the most popular open-source monitoring tools.
Prometheus collects and stores server metrics.
Grafana visualizes the metrics in dashboards.

Benefits of Prometheus + Grafana

  • Monitor CPU, memory, disk, and network usage.
  • Real-time alerting when resources are overloaded.
  • Beautiful dashboards for reporting.
  • Highly scalable for cloud and Kubernetes environments.
  • Free and open-source with large community support.

In this guide, we will set up Prometheus, Node Exporter (for server metrics), and Grafana step by step on Ubuntu 22.04.

Step 1: Update System

sudo apt update && sudo apt upgrade -y

Step 2: Install Prometheus

sudo useradd --no-create-home --shell /bin/false prometheus
sudo mkdir /etc/prometheus /var/lib/prometheus

Download Prometheus:

wget https://github.com/prometheus/prometheus/releases/download/v2.47.0/prometheus-2.47.0.linux-amd64.tar.gz
tar xvf prometheus-2.47.0.linux-amd64.tar.gz
cd prometheus-2.47.0.linux-amd64

Move binaries:

sudo cp prometheus /usr/local/bin/
sudo cp promtool /usr/local/bin/
sudo cp -r consoles/ console_libraries/ /etc/prometheus/

Step 3: Configure Prometheus

Create config file /etc/prometheus/prometheus.yml:

global:
  scrape_interval: 15s

scrape_configs:
  - job_name: "prometheus"
    static_configs:
      - targets: ["localhost:9090"]

  - job_name: "node_exporter"
    static_configs:
      - targets: ["localhost:9100"]

Step 4: Create Prometheus Service

Create systemd service file:

sudo nano /etc/systemd/system/prometheus.service

Add:

[Unit]
Description=Prometheus
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
  --config.file=/etc/prometheus/prometheus.yml \
  --storage.tsdb.path=/var/lib/prometheus/ \
  --web.listen-address=:9090

[Install]
WantedBy=multi-user.target

Start service:

sudo systemctl daemon-reload
sudo systemctl enable prometheus
sudo systemctl start prometheus

Access Prometheus UI at: http://server-ip:9090

Step 5: Install Node Exporter (for Server Metrics)

Node Exporter collects CPU, memory, disk, and network metrics.

wget https://github.com/prometheus/node_exporter/releases/download/v1.6.1/node_exporter-1.6.1.linux-amd64.tar.gz
tar xvf node_exporter-1.6.1.linux-amd64.tar.gz
cd node_exporter-1.6.1.linux-amd64
sudo cp node_exporter /usr/local/bin/

Create systemd service:

sudo nano /etc/systemd/system/node_exporter.service

Add:

[Unit]
Description=Node Exporter
Wants=network-online.target
After=network-online.target

[Service]
User=prometheus
ExecStart=/usr/local/bin/node_exporter

[Install]
WantedBy=multi-user.target

Enable and start:

sudo systemctl daemon-reload
sudo systemctl enable node_exporter
sudo systemctl start node_exporter

Now metrics are available at: http://server-ip:9100/metrics

Step 6: Install Grafana

sudo apt install -y apt-transport-https software-properties-common
wget -q -O - https://packages.grafana.com/gpg.key | sudo apt-key add -
echo "deb https://packages.grafana.com/oss/deb stable main" | sudo tee /etc/apt/sources.list.d/grafana.list
sudo apt update
sudo apt install -y grafana

Enable and start Grafana:

sudo systemctl enable grafana-server
sudo systemctl start grafana-server

Access Grafana at: http://server-ip:3000
(Default login: admin / admin)

Step 7: Connect Grafana to Prometheus

Login to Grafana → Go to Configuration → Data Sources → Add Prometheus:

  • URL: http://localhost:9090
  • Save & Test

Step 8: Import Server Monitoring Dashboard

Go to Grafana → Create → Import → Enter Dashboard ID: 1860 (famous Node Exporter Full Dashboard).
This will show CPU, memory, disk, and network usage in real time.

Best Practices

  • Keep Prometheus data on a fast disk (SSD).
  • Use alerts with email/Slack integration.
  • Install exporters for databases (MySQL, PostgreSQL) for app-level monitoring.
  • Secure Grafana with SSL and strong admin password.

Conclusion

We have successfully set up Prometheus + Grafana with Node Exporter on Ubuntu 22.04.
Now we can monitor server health, performance, and resource usage in real time.
This setup is widely used in production and is an essential skill for Linux and DevOps engineers.

Leave a Reply

Your email address will not be published. Required fields are marked *