Cyber Month Deal - up to 36% OFF

How to Install Prometheus on Ubuntu 22.04

Published on May 16, 2023 Updated on Apr 3, 2024

Do you experience unreliable software and infrastructure performance? Do you seek a solution to improve the efficiency of your system? A monitoring tool might be what you seek!

One such tool for monitoring system performance is Prometheus. It is an open-source software that can be used for alerting and monitoring. This article will guide you through a step-by-step process of installing and configuring Prometheus on Ubuntu 22.04.

What is Prometheus?

Prometheus monitoring solution is a free and open-source solution for monitoring metrics, events, and alerts. It collects and records metrics from servers, containers, and applications. In addition to providing a flexible query language (PromQL), and powerful visualization tools, it also provides an alerting mechanism that sends notifications when needed.

System Requirements/Prerequisites

This tutorial will be hands-on. If you'd like to follow along, be sure you have the following:

  • Basic knowledge of the Linux command-line interface (CLI);
  • Ubuntu 22.04 server;
  • A user account with sudo privileges.

How to install Prometheus on Ubuntu 22.04

In this section, you'll go through the steps on how to install Prometheus on Ubuntu 22.04.

Step 1 - Update System Packages

You should first update your system's package list to ensure that you are using the most recent packages. To accomplish this, issue the following command:

sudo apt update

Step 2 - Create a System User for Prometheus

Now create a group and a system user for Prometheus. To create a group and then add a user to the group, run the following command:

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

add group and user

This will create a system user and group named "prometheus" for Prometheus with limited privileges, reducing the risk of unauthorized access.

Step 3 - Create Directories for Prometheus

To store configuration files and libraries for Prometheus, you need to create a few directories. The directories will be located in the /etc and the /var/lib directory respectively. Use the commands below to create the directories:

sudo mkdir /etc/prometheus
sudo mkdir /var/lib/prometheus

create directories for Prometheus

Step 4 - Download Prometheus and Extract Files

To download the latest update, go to the Prometheus official downloads site and copy the download link for Linux Operating System. Download using wget and the link you copied like so:

wget https://github.com/prometheus/prometheus/releases/download/v2.43.0/prometheus-2.43.0.linux-amd64.tar.gz

You should see it being downloaded.

download Prometheus

After the download has been completed, run the following command to extract the contents of the downloaded file:

tar vxf prometheus*.tar.gz

extract Prometheus file

Step 5- Navigate to the Prometheus Directory

After extracting the files, navigate to the newly extracted Prometheus directory using the following command:

cd prometheus*/

change directory

Changing to the Prometheus directory allows for easier management and configuration of the installation. Subsequent steps will be performed within the context of the Prometheus directory.

Configuring Prometheus on Ubuntu 22.04

With Prometheus downloaded and extracted on Ubuntu 22.04, you can go on to configure it. Configuring Prometheus could involve specifying the metrics to be collected, defining targets to scrape metrics from, and configuring alerting rules and recording rules. You will learn how to set up Prometheus on Ubuntu 22.04 in this section. You would just be concentrating on the default setup that enables you to access Prometheus through your web browser.

Step 1 - Move the Binary Files & Set Owner

First, you need to move some binary files (prometheus and promtool) and change the ownership of the files to the "prometheus" user and group. You can do this with the following commands:

sudo mv prometheus /usr/local/bin
sudo mv promtool /usr/local/bin
sudo chown prometheus:prometheus /usr/local/bin/prometheus
sudo chown prometheus:prometheus /usr/local/bin/promtool

move binary files and set owner

Step 2 - Move the Configuration Files & Set Owner

Next, move the configuration files and set their ownership so that Prometheus can access them. To do this, run the following commands:

sudo mv consoles /etc/prometheus
sudo mv console_libraries /etc/prometheus
sudo mv prometheus.yml /etc/prometheus
sudo chown prometheus:prometheus /etc/prometheus
sudo chown -R prometheus:prometheus /etc/prometheus/consoles
sudo chown -R prometheus:prometheus /etc/prometheus/console_libraries
sudo chown -R prometheus:prometheus /var/lib/prometheus

move configuration files and set owner

The prometheus.yml file is the main Prometheus configuration file. It includes settings for targets to be monitored, data scraping frequency, data processing, and storage. You can set alerting rules and notification conditions in the file. You don't need to modify this file for this demonstration but feel free to open it in an editor to take a closer look at its contents.

sudo nano /etc/prometheus/prometheus.yml

Here's the default content of the Prometheus file:

Prometheus file content

Prometheus file content

Prometheus file content

Step 3 - Create Prometheus Systemd Service

Now, you need to create a system service file for Prometheus. Create and open a prometheus.service file with the Nano text editor using:

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

Include these settings to the file, save, and exit:

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

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

[Install]
WantedBy=multi-user.target

Prometheus systemd service file

Prometheus systemd service file

The "systems" service file for Prometheus defines how Prometheus should be managed as a system service on Ubuntu. It includes the service configuration, such as the user and group it should run as. It also includes the path to the Prometheus binary and the Prometheus configuration file location. Additionally, the file can be used to set storage locations for metrics data and pass additional command-line options to the Prometheus binary when it starts.

Step 4 - Reload Systemd

You need to reload the system configuration files after saving the prometheus.service file so that changes made are recognized by the system. Reload the system configuration files using the following:

sudo systemctl daemon-reload

Reload systemd

Step 5 - Start Prometheus Service

Next, you want to enable and start your Prometheus service. Do this using the following commands:

sudo systemctl enable prometheus
sudo systemctl start prometheus

Start Prometheus service

Step 5 - Check Prometheus Status

After starting the Prometheus service, you may confirm that it is running or if you have encountered errors using:

sudo systemctl status prometheus

Sample output: Check Prometheus status

Access Prometheus Web Interface

Prometheus runs on port 9090 by default so you need to allow port 9090 on your firewall, Do that using the command:

sudo ufw allow 9090/tcp

Allow port 9090

With Prometheus running successfully, you can access it via your web browser using localhost:9090 or <ip_address>:9090

Access Prometheus web interface

Conclusion

Prometheus is an excellent monitoring solution. In this article, you've learned how to install and configure Prometheus on Ubuntu 22.04 but you can achieve more with this tool. Prometheus lets you set up alerting, add exporters, or create custom dashboards. Why not try one or more of these?

Cloud VPS - Cheaper Each Month

Start with $9.99 and pay $0.5 less until your price reaches $6 / month.

Share this article

Related Articles

Published on Aug 21, 2019 Updated on Jun 15, 2023

Can DDoS attacks harm your server and how to prevent them?

DDoS attacks can cause real harm to your system. Learn some practical examples of how to lower the risk and how to prevent ddos attacks.

Read More
Published on Nov 28, 2019 Updated on Aug 8, 2023

Streaming Servers: Everything You Need To Know

What are Live Streaming servers. How do they work? Why do you need a stable and fast network? We will try to cover these topics on our article.

Read More
Published on Jul 8, 2022 Updated on Oct 4, 2023

How to Install and Configure Apache Reverse Proxy Server With SSL/TLS Encryption

This step-by-step guide will explain what a reverse proxy is and show you how to install Apache reverse proxy server with SSL/TLS encryption.

Read More
We use cookies to ensure seamless user experience for our website. Required cookies - technical, functional and analytical - are set automatically. Please accept the use of targeted cookies to ensure the best marketing experience for your user journey. You may revoke your consent at any time through our Cookie Policy.
build: 06ac5732e.831