# Install Prometheus

## Prometheus

Prometheus handles data collection, storage, and queries. Data sources are configured as targets from which Prometheus will request metrics data. Prometheus stores this data and responds to Grafana queries against the data.

### Create System Account

Create a system account under which Prometheus can run.

<pre class="language-bash"><code class="lang-bash"><strong>sudo adduser --system prometheus --group --no-create-home
</strong></code></pre>

<figure><img src="https://971382438-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZON6xeJcH6NDhbzjDAWf%2Fuploads%2FBfrmBqNusgOuTpsSNQIo%2Fadduser-prometheus.png?alt=media&#x26;token=b2fd75f4-bd62-4d24-9027-a2b485b8648f" alt=""><figcaption><p>Results of <code>adduser</code> command</p></figcaption></figure>

### Install Prometheus

The instructions below are for version 2.42.0. [Check for newer versions of Prometheus](https://github.com/prometheus/prometheus/releases), and adapt the instructions below.

Download the latest release.

```bash
cd
wget https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.linux-amd64.tar.gz
```

<figure><img src="https://971382438-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZON6xeJcH6NDhbzjDAWf%2Fuploads%2F9k4oLYAgF2hJZjQG5sPU%2Fwget-prometheus.png?alt=media&#x26;token=f154cf71-a342-43a5-b480-73ee25b0f59a" alt=""><figcaption><p>Results of <code>wget</code> command</p></figcaption></figure>

Unpack the release.

```bash
tar xzvf prometheus-2.42.0.linux-amd64.tar.gz
```

<figure><img src="https://971382438-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FZON6xeJcH6NDhbzjDAWf%2Fuploads%2FVHvRTuxU0lwi2R4aXLut%2Ftar-prometheus.png?alt=media&#x26;token=079dcc12-add3-44d6-bd17-38a45dcdba64" alt=""><figcaption><p>Results of <code>tar</code> command</p></figcaption></figure>

Copy multiple executable files to `/usr/local/bin`.

```bash
cd prometheus-2.42.0.linux-amd64
sudo cp -t /usr/local/bin/ prometheus promtool
```

Create additional required directories.

```bash
sudo mkdir -p /etc/prometheus/console_libraries \
    /etc/prometheus/consoles \
    /etc/prometheus/files_sd \
    /etc/prometheus/rules \
    /etc/prometheus/rules.d \
    /var/lib/prometheus
```

Change ownership on the `/var/lib/prometheus` directory so the prometheus service can access it.

```
sudo chown prometheus:prometheus /var/lib/prometheus
```

### Configure Startup

Create a systemd service file to configure automatic startup of the Prometheus service.

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

Insert the following into the Prometheus systemd service file.

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

[Service]
User=prometheus
Group=prometheus
Type=simple
Restart=always
RestartSec=5
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 \
    --storage.tsdb.retention.time=32d
ExecReload=kill -HUP $MAINPID
        
[Install]
WantedBy=multi-user.target
```

{% hint style="info" %}
Data retention is set to 32 days in the default configuration. The number of days can be increased by changing the value assigned to the `storage.tsdb.retention.time` flag in the last line of the `ExecStart` configuration above.
{% endhint %}

Save the file and exit the editor.

### Optional: Clean Up Installation Files

Remove the Prometheus archive and installation directory.

```bash
cd
rm prometheus-2.42.0.linux-amd64.tar.gz
rm -rf prometheus-2.42.0.linux-amd64
```

### Notes

{% hint style="info" %}
Prometheus will listen on port 9090. This is the port for the Prometheus admin UI.
{% endhint %}

{% hint style="warning" %}
We have not yet configured nor started Prometheus.
{% endhint %}
