1080*80 ad

Install Prometheus on Ubuntu

Master Your Monitoring: A Complete Guide to Installing Prometheus on Ubuntu

In today’s complex IT environments, robust monitoring isn’t a luxury—it’s a necessity. Gaining deep visibility into your systems’ performance is crucial for maintaining reliability, troubleshooting issues, and making informed decisions. Prometheus, a leading open-source monitoring and alerting toolkit, has become the industry standard for collecting and analyzing time-series data.

This comprehensive guide will walk you through every step of installing and configuring Prometheus on an Ubuntu server. By following these instructions, you will have a powerful, production-ready monitoring foundation for your infrastructure.

Prerequisites

Before we begin, ensure you have the following:

  • An Ubuntu server (this guide is tested on 22.04 LTS but should work on other recent versions).
  • A non-root user with sudo privileges.

Step 1: Update Your System

First, it’s always a best practice to update your server’s package index and upgrade existing packages to their latest versions. This ensures a clean and secure starting point.

Open your terminal and run the following commands:

sudo apt update
sudo apt upgrade -y

Step 2: Create a Dedicated Prometheus User

For security purposes, Prometheus should not run as the root user. We will create a dedicated system user and group specifically for the Prometheus service. This follows the principle of least privilege, limiting the service’s access to only what it needs.

Create a prometheus group:

sudo groupadd --system prometheus

Now, create the prometheus system user, add it to the group, and set its shell to /bin/false so it cannot be used for a direct login:

sudo useradd -s /bin/false -r -g prometheus prometheus

Step 3: Download and Install Prometheus

Prometheus is distributed as a pre-compiled binary. We’ll download the latest stable release directly from the official website.

  1. Navigate to the /tmp directory, which is a good place for temporary downloads.

    cd /tmp
    
  2. Visit the official Prometheus downloads page to find the URL for the latest stable version for Linux (linux-amd64). Copy the link address and use wget to download it.

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

    (Note: Check the downloads page for the absolute latest version and update the URL accordingly.)

  3. Once the download is complete, extract the archive:

    tar xvf prometheus-*.linux-amd64.tar.gz
    
  4. This creates a directory named something like prometheus-2.53.0.linux-amd64. Now, we’ll move the essential binary files, prometheus and promtool, to /usr/local/bin, a standard location for user-installed executables.

    cd prometheus-*.linux-amd64
    sudo mv prometheus /usr/local/bin/
    sudo mv promtool /usr/local/bin/
    

Step 4: Configure Prometheus Directories and Files

Prometheus needs a directory for its configuration files and another for its data.

  1. First, create the main configuration directory at /etc/prometheus.

    sudo mkdir /etc/prometheus
    
  2. Next, move the consoles and console_libraries directories, along with the main configuration file prometheus.yml, into this new directory.

    sudo mv consoles /etc/prometheus
    sudo mv console_libraries /etc/prometheus
    sudo mv prometheus.yml /etc/prometheus
    
  3. Crucially, we must set the correct ownership for these files and directories to the prometheus user we created earlier.

    sudo chown -R prometheus:prometheus /etc/prometheus/
    sudo chown prometheus:prometheus /usr/local/bin/prometheus
    sudo chown prometheus:prometheus /usr/local/bin/promtool
    
  4. Finally, create the data directory where Prometheus will store its time-series database and set its ownership.

    sudo mkdir /var/lib/prometheus
    sudo chown prometheus:prometheus /var/lib/prometheus/
    

Step 5: Create the systemd Service File

To manage Prometheus as a standard system service (allowing it to start on boot, restart automatically, etc.), we need to create a systemd unit file.

  1. Create a new service file using a text editor like nano:

    sudo nano /etc/systemd/system/prometheus.service
    
  2. Paste the following configuration into the file. This tells systemd how to run, manage, and secure the Prometheus process.

    [Unit]
    Description=Prometheus Time Series Collection and Processing Server
    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
    
    • User and Group: Specifies that the service runs as our dedicated prometheus user.
    • ExecStart: Defines the command to start Prometheus, pointing to our configuration file and data directory.
  3. Save the file and exit the editor (in nano, press CTRL+X, then Y, then Enter).

Step 6: Start and Enable the Prometheus Service

Now that our service is defined, we can start it and enable it to launch automatically on system boot.

  1. Reload the systemd daemon to make it aware of the new service file.

    sudo systemctl daemon-reload
    
  2. Start the Prometheus service.

    sudo systemctl start prometheus
    
  3. Enable the service to start on boot.

    sudo systemctl enable prometheus
    
  4. Finally, check the status to ensure it’s running correctly.

    sudo systemctl status prometheus
    

    You should see an output indicating the service is active (running). If not, review the previous steps for any errors.

Step 7: Verify Installation and Configure the Firewall

Prometheus exposes a web interface on port 9090. You can access it from your browser, but first, you may need to allow traffic through your firewall.

  1. If you are using UFW (Uncomplicated Firewall), allow access to port 9090:

    sudo ufw allow 9090/tcp
    
  2. Now, open your web browser and navigate to your server’s IP address followed by the port number:

    http://your_server_ip:9090

You should see the Prometheus web UI. From here, you can explore metrics, view service discovery status, and build queries using PromQL.

Next Steps

Congratulations! You have successfully installed and configured a running instance of Prometheus on your Ubuntu server. This is the first step in building a powerful observability stack.

Your next steps should include:

  • Configuring Targets: Edit the /etc/prometheus/prometheus.yml file to add more services and endpoints for Prometheus to scrape metrics from.
  • Installing Exporters: Deploy exporters like the Node Exporter to gather detailed system metrics from your servers.
  • Visualizing Data: Integrate Prometheus with a visualization tool like Grafana to create beautiful, insightful dashboards.

Source: https://kifarunix.com/install-prometheus-on-ubuntu-18-04/

900*80 ad

      1080*80 ad