Skip to main content

🚀 Installing and Running Loki (Grafana) on Linux 🖥️

Loki is a log aggregation system by Grafana, designed to store and query logs efficiently. Follow the steps below to download, install, and run Loki on your Linux system.


👥 Step 1: Download Loki Binary

curl -O -L "https://github.com/grafana/loki/releases/download/v3.4.2/loki-linux-amd64.zip"

👉 This command does the following:

  • -O → Saves the file with the same name as in the URL.
  • -L → Follows redirects (useful if the URL redirects to another location).
  • Downloads Loki v3.4.2 for Linux AMD64 architecture.

📂 Step 2: Extract the Loki Binary

unzip loki-linux-amd64.zip
sudo mv loki-linux-amd64 /usr/bin/loki

👉 This extracts the loki-linux-amd64 binary from the ZIP file.

If unzip is not installed, install it using:

  • Debian/Ubuntu: sudo apt install unzip
  • CentOS/RHEL: sudo yum install unzip
  • Arch Linux: sudo pacman -S unzip

📝 Step 3: Download Loki Configuration File

sudo mkdir -p /etc/loki
wget https://raw.githubusercontent.com/grafana/loki/v3.4.2/cmd/loki/loki-local-config.yaml -O /etc/loki/config.yaml

👉 This downloads the default Loki configuration file (loki-local-config.yaml) from the Grafana Loki GitHub repository.

🔹 Why do we need this?

  • It provides the basic settings required for running Loki.
  • You can modify it later based on your requirements.

Example config to auto-delete logs after 24 hours:

auth_enabled: false

server:
http_listen_port: 3100
grpc_listen_port: 9096
log_level: debug
grpc_server_max_concurrent_streams: 1000

common:
instance_addr: 127.0.0.1
path_prefix: /tmp/loki
storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory

schema_config:
configs:
- from: 2025-04-08
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h

compactor:
working_directory: /tmp/loki/compactor
compaction_interval: 10m
retention_enabled: true
retention_delete_delay: 2h
retention_delete_worker_count: 150
delete_request_store: filesystem

limits_config:
retention_period: 24h

ruler:
alertmanager_url: http://localhost:9093

frontend:
encoding: protobuf

# Uncomment to disable reporting:
# analytics:
# reporting_enabled: false

🚀 Step 4: Create a Systemd Service for Loki

Create a systemd service file for Loki at /etc/systemd/system/loki.service:

sudo tee /etc/systemd/system/loki.service > /dev/null <<EOF
[Unit]
Description=Loki Log Aggregation System
After=network.target

[Service]
User=root
Group=root
ExecStart=/usr/bin/loki -config.file /etc/loki/config.yaml

Restart=always
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target
EOF

📝 NOTE: If the service fails to start, it might be because Loki is already running manually. Kill it using:

ps aux | grep loki
kill <PID>

⚠️ Important

By default, the Loki configuration uses /tmp for storage:

storage:
filesystem:
chunks_directory: /tmp/loki/chunks
rules_directory: /tmp/loki/rules

You should change the path from the /tmp directory in your config.yaml file. Otherwise, logs will be deleted after the server restarts because /tmp is cleared on reboot.

storage:
filesystem:
chunks_directory: /var/lib/loki/chunks
rules_directory: /var/lib/loki/rules

Make sure the directories exist and are writable:

sudo mkdir -p /var/lib/loki/chunks /var/lib/loki/rules
sudo chown -R loki:loki /var/lib/loki

🛠️ Reload and Start Loki Service

sudo systemctl daemon-reload
sudo systemctl enable loki
sudo systemctl start loki
sudo systemctl status loki

🔍 Verify If Loki Is Running

Check Loki readiness on port 3100:

curl -s http://localhost:3100/ready

✅ If Loki is working correctly, it should return:

"ready"

🎯 Next Steps

  • ✅ Integrate Loki with Promtail to collect logs.
  • ✅ Connect it with Grafana for visualization.
  • ✅ Explore Loki’s API for querying logs.

🚀 Install Promtail on Target Machine

👥 Download and Install Promtail

curl -O -L "https://github.com/grafana/loki/releases/latest/download/promtail-linux-amd64.zip"
sudo apt install unzip
unzip promtail-linux-amd64.zip
sudo mv promtail-linux-amd64 /usr/local/bin/promtail
sudo chmod +x /usr/local/bin/promtail

⚙️ Configure Promtail

Create config file /etc/promtail/config.yaml:

sudo tee /etc/promtail/config.yaml > /dev/null <<EOF
server:
http_listen_port: 9080
grpc_listen_port: 0

positions:
filename: /var/log/positions.yaml

clients:
- url: http://<LOKI_SERVER_IP>:3100/loki/api/v1/push

scrape_configs:
- job_name: "nginx-logs"
static_configs:
- targets:
- localhost
labels:
job: "nginx-logs"
host: "nginx-server"
__path__: /var/log/nginx/access.log
EOF

🚠 Create a Systemd Service for Promtail

sudo tee /etc/systemd/system/promtail.service > /dev/null <<EOF
[Unit]
Description=Promtail Log Collector
After=network.target

[Service]
ExecStart=/usr/local/bin/promtail -config.file=/etc/promtail/config.yaml
Restart=always
User=root

[Install]
WantedBy=multi-user.target
EOF

🚠 Start and Enable Promtail

sudo systemctl daemon-reload
sudo systemctl enable promtail
sudo systemctl start promtail
sudo systemctl status promtail

👉 Promtail is now set up and running! 🎉