Install and Configure Prometheus on Ubuntu 22.04

In this guide, we want to teach you to Install and Configure Prometheus on Ubuntu 22.04.

Prometheus is an open-source tool that provides monitoring and alerting for cloud-native systems, such as Kubernetes. It can collect and store measurements as time-series data, including a timestamp for each entry. Labels, which are optional key-value pairs, can also be collected and recorded.

Steps To Install and Configure Prometheus on Ubuntu 22.04

To complete this guide, you must log in to your server as a non-root user with sudo privileges and set up a basic firewall. To do this, you can follow this guide on Initial Server Setup with Ubuntu 22.04.

Set up Prometheus on Ubuntu 22.04

At this point, you can follow the steps below to install Prometheus on your server.

Create Prometheus system group and user

First, you need to update your local package index with the following command:

sudo apt update

Then, use the following commands to create a Prometheus system group and user:

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

Create data & configs directories for Prometheus

At this point, you need to create a directory for Prometheus to store its data. To do this, you can use the following command:

sudo mkdir /var/lib/prometheus

Now use the command below to create the Prometheus config directories on Ubuntu 22.04:

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

Download Prometheus

Here you need to visit the GitHub Prometheus release page and download the latest binary package under /tmp/prometheus directory with the following curl command:

# sudo mkdir -p /tmp/prometheus 
# sudo 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 -

When your download is completed, extract it by using the command below:

sudo tar xvf prometheus*.tar.gz

Next, switch to your Prometheus directory:

cd prometheus*/

Move the binary files to /usr/local/bin/ directory:

sudo mv prometheus promtool /usr/local/bin/

Verify your Prometheus installation by checking its version:

prometheus --version
Output
prometheus, version 2.43.0 (branch: HEAD, revision: edfc3bcd025dd6fe296c167a14a216cab1e552ee)
  build user:       root@8a0ee342e592
  build date:       20230321-12:56:07
  go version:       go1.19.7
  platform:         linux/amd64
  tags:             netgo,builtinassets

Configure Prometheus on Ubuntu 22.04

At this point, you need to move the Prometheus configuration template to the /etc directory:

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

Also, move consoles and console_libraries to the /etc/prometheus directory:

sudo mv consoles/ console_libraries/ /etc/prometheus/

Then, switch back to your home directory:

cd $HOME

Create a Prometheus systemd unit file

To manage the Prometheus service with systemd, you need to create a systemd unit file for Prometheus:

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

Add the following content to the file:

[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

When you are done, save and close the file.

Set Correct Directory Permissions

At this point, you need to change the ownership of these directories to Prometheus user and group by 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/

Start and Enable Prometheus service

Now you can start and enable Prometheus service on Ubuntu 22.04 with the following commands:

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

Verify your Prometheus service is active and running on Ubuntu 22.04:

sudo systemctl status prometheus
Output
● prometheus.service - Prometheus
     Loaded: loaded (/etc/systemd/system/prometheus.service; enabled; vendor pr>
     Active: active (running) since Wed 2023-04-19 10:06:49 UTC; 48s ago
       Docs: https://prometheus.io/docs/introduction/overview/
   Main PID: 2348 (prometheus)
      Tasks: 8 (limit: 4575)
     Memory: 17.8M
        CPU: 214ms
     CGroup: /system.slice/prometheus.service
             └─2348 /usr/local/bin/prometheus --config.file=/etc/prometheus/pro>

Configure Firewall for Prometheus

If your server has a running firewall service, you’ll need to open port 9090. To do this, run the following command:

sudo ufw allow 9090/tcp

Reload the firewall to apply the new rules:

sudo ufw reload

Access Prometheus Server

Now you can confirm that you can connect to port 9090 by accessing the Prometheus server IP address / DNS name in your web browser.

http://server-ip-or-domain-name:9090
Prometheus server ubuntu22

That’s it, you are done.

Conclusion

At this point, you have learned to Install and Configure Prometheus on Ubuntu 22.04.

Hope you enjoy it. For more articles, you can visit the orcacore blog page.

You may be interested in these articles:

Install Grafana on Ubuntu 22.04

Install Elasticsearch on Ubuntu 22.04

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Stay informed and not overwhelmed, subscribe now!