How to install Prometheus Server on Ubuntu 22.04

In this guide, we will learn how to install the Prometheus server on Ubuntu 22.04. Prometheus is an open-source system monitoring and alerting toolkit. Prometheus collects and stores its metrics as time-series data. Metrics information is stored with the timestamp at which it was recorded, alongside optional key-value pairs called labels.

And now you are wondering what are metrics? Metrics are numeric measurements and time-series mean that the changes are recorded over time.

Install Prometheus on Ubuntu 22.04

1. Update the system repositories.

Let us update the system first in order to make the repositories up to date.

$ sudo apt update && apt upgrade -y

2. Download and install Prometheus

Download the latest release of Prometheus into your system from the Prometheus download page.

$ wget https://github.com/prometheus/prometheus/releases/download/v2.37.0-rc.0/prometheus-2.37.0-rc.0.linux-amd64.tar.gz

After the download is complete, the next step is to extract the archive:

$ tar xvfz prometheus-*.tar.gz

For convenience, we can change the directory for the extracted archive

$ cd prometheus-*

From there we can create a configuration file directory

$ sudo mkdir -p /etc/prometheus

Then we can create the data directory

$ sudo mkdir -p /var/lib/prometheus

Let’s now move the binary files Prometheus and promtool to /usr/local/bin/

Use the following code snippet

$ sudo mv prometheus promtool /usr/local/bin/

Let’s use a Prometheus.yaml file that we can use as an example to see how Prometheus monitors your infrastructure. This file will only monitor itself for now.

$ sudo vi prometheus.yml

you will see the following contents from prometheus.yaml file

global:
  scrape_interval:     15s
  external_labels:
    monitor: 'codelab-monitor'
scrape_configs:
   scraped from this config.
  - job_name: 'prometheus'
    scrape_interval: 5s

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

Move the template configuration file prometheus.yml to /etc/prometheus/ directory

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

We have installed Prometheus. Let’s verify the version

$ prometheus --version
prometheus, version 2.37.0-rc.0 (branch: HEAD, revision: 2479fb42f0a9bb45c2e82f11efc73c0a75fc492c)
  build user:       root@b82e7804c2b2
  build date:       20220705-13:33:30
  go version:       go1.18.3
  platform:         linux/amd64

3. Create Prometheus System Group

The following code can help create the Prometheus system group.

$ sudo groupadd --system prometheus

We can then create a Prometheus system user and assign the primary group created

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

Set the ownership of Prometheus files and data directories to the Prometheus group and user.

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

$ sudo chmod -R 775 /etc/prometheus/ /var/lib/prometheus/

Lastly, we can create a systemd service to run at boot time

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

Add the following content to prometheus.service

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

[Service]
User=prometheus
Group=prometheus
Restart=always
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 \
    --web.listen-address=0.0.0.0:9090

[Install]
WantedBy=multi-user.target

Start the Prometheus service

$ sudo systemctl start prometheus

Now we can check if our Prometheus is up and running

$ sudo systemctl status prometheus
● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; disabled; vendor p>
     Active: active (running) since Sat 2022-07-09 06:36:04 UTC; 32s ago
   Main PID: 13447 (prometheus)
      Tasks: 6 (limit: 1033)
     Memory: 17.9M
        CPU: 86ms
     CGroup: /system.slice/prometheus.service
             └─13447 /usr/local/bin/prometheus --config.file=/etc/prometheus/pr>

Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.386Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.389Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.390Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.390Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.392Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.392Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.393Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.394Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.394Z caller>
Jul 09 06:36:04 localhost prometheus[13447]: ts=2022-07-09T06:36:04.395Z caller>
lines 1-20/20 (END)

Add firewall rules to allow Prometheus to run on TCP port 9090

$ ufw allow 9090/tcp

Access your server from the browser.

http://<ip address>or<hostname>:9090
nextgentips: Prometheus dashboard
nextgentips: Prometheus dashboard

Conclusion

Congratulations! you have successfully installed Prometheus on Ubuntu 22.04. To read more consult Prometheus documentation.

About Kipkoech Sang

I am a technology enthusiast who loves to share gained knowledge through offering daily tips as a way of empowering others. I am fan of Linux and all other things open source.
View all posts by Kipkoech Sang →

Leave a Reply

Your email address will not be published.