How to Install Prometheus on Ubuntu Server

Prometheus is an open-source monitoring system that helps you collect and analyze metrics from various sources. In this tutorial, we will learn how to install Prometheus on Ubuntu Server with step-by-step instructions.

Prerequisites

Before you proceed with the installation, you must have the following prerequisites in place:

  • A server running Ubuntu 20.04 (or any latest version) with a minimum of 2GB RAM
  • A non-root user with sudo privileges

Step 1: Update the System

Before installing Prometheus, it is recommended to update your server's software packages to the latest versions using the following commands:

sudo apt update
sudo apt upgrade

Step 2: Download Prometheus

The first step to installing Prometheus on Ubuntu Server is to download the latest stable version from the Prometheus website at https://prometheus.io/download/.

cd /tmp
curl -O https://github.com/prometheus/prometheus/releases/download/v2.28.1/prometheus-2.28.1.linux-amd64.tar.gz

Step 3: Extract the downloaded file

Use the following command to extract the downloaded Prometheus file:

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

Step 4: Configure Prometheus

Next, you need to create a configuration file for Prometheus. You can use the default configuration file provided in the extracted folder or create a new one.

sudo nano /etc/prometheus/prometheus.yml

Use the following configuration as a base and update it as per your needs:

global:
  scrape_interval: 10s
  evaluation_interval: 10s

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

Step 5: Run Prometheus

Use the following commands to create a user and group for Prometheus and move the extracted directory to the /opt folder.

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

sudo cp /tmp/prometheus-2.28.1.linux-amd64/prometheus /usr/local/bin/
sudo cp /tmp/prometheus-2.28.1.linux-amd64/promtool /usr/local/bin/
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool

sudo cp -r /tmp/prometheus-2.28.1.linux-amd64/consoles /etc/prometheus/
sudo cp -r /tmp/prometheus-2.28.1.linux-amd64/console_libraries /etc/prometheus/

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

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

Add the following contents to the file:

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

[Service]
Type=simple
User=prometheus
Group=prometheus
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

Use the following commands to start and enable the Prometheus service.

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

Step 6: Access Prometheus

Once Prometheus is running, you can access its web interface by entering the following URL in your web browser:

http://your_server_ip:9090

Conclusion

Congratulations! You have successfully installed and configured Prometheus on an Ubuntu server. You can now use it to monitor various metrics for your applications and systems.