How to setup Loki in Ubuntu 20.04

Sujit Patel
2 min readDec 26, 2021

--

Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by Prometheus. It is designed to be very cost effective and easy to operate. It does not index the contents of the logs, but rather a set of labels for each log stream.

A Loki-based logging stack consists of 3 components:

  • Promtail is the agent, responsible for gathering logs and sending them to Loki.
  • Loki is the main server, responsible for storing logs and processing queries.
  • Grafana for querying and displaying the logs.

In this article i’m using Loki version 2.4.1.

Install Loki

Download Loki binary according to your system and CPU architecture from here.

I’m system is Linux and CPU architecture is amd64. To check CPU architecture run below command.

#check cpu architecture
uname -a
#example for Loki on the linux operating system and amd64 architecturecurl -O -L "https://github.com/grafana/loki/releases/download/v2.4.1/loki-linux-amd64.zip"#extract the binary
unzip "loki-linux-amd64.zip"
#make sure it is executable
chmod a+x "loki-linux-amd64"
#copy binary to /usr/local/bin/
sudo cp loki-linux-amd64 /usr/local/bin/loki
#verify installation by checking version
loki --version

Now, it’s time create config file for Loki.

#create user for loki
sudo useradd --system loki
#create dir in /etc
sudo mkdir -p /etc/loki /etc/loki/logs
#default loki config file
sudo curl -o /etc/loki/loki-local-config.yaml -L "https://gist.github.com/psujit775/ceaf475fc369e25a2d04501f8a7c0a59/raw"
#change permissions
sudo chown -R loki: /etc/loki

Configure Loki to run as a service.

#Create a file called loki.service
sudo vi /etc/systemd/system/loki.service

Add the script and save.

[Unit] 
Description=Loki service
After=network.target

[Service]
Type=simple
User=loki
ExecStart=/usr/local/bin/loki -config.file /etc/loki/loki-local-config.yaml
Restart=on-failure
RestartSec=20
StandardOutput=append:/etc/loki/logs/loki.log
StandardError=append:/etc/loki/logs/loki.log

[Install]
WantedBy=multi-user.target

Run below command to start loki as a service.

sudo systemctl daemon-reload #To reload systemd
sudo systemctl start loki #to start loki
sudo systemctl status loki #to check status
sudo systemctl restart loki #to restart

You can check logs of Loki at /etc/loki/logs/loki.log

Enable Loki on system boot.

sudo systemctl enable loki.service

To Check metrics received by loki. Open browser and type below address.

http://localhost:3100/metrics

OUTPUT:

http://localhost:3100/metrics

Now Loki is successfully installed and running as service in Ubuntu 20.04.

--

--

Sujit Patel

DevOps Engineer, Linux lover, Technology and Automation enthusiast. A strong believer in continuous learning.