Skip to content

Instantly share code, notes, and snippets.

@paulo-amaral
Last active April 8, 2025 01:47
Show Gist options
  • Save paulo-amaral/b274a51b12b7d4118f8739ba689b219d to your computer and use it in GitHub Desktop.
Save paulo-amaral/b274a51b12b7d4118f8739ba689b219d to your computer and use it in GitHub Desktop.
script to install metabase and all packages in Debian 12
#!/bin/bash
## Script to install metabase on Debian 12
## Author: Paulo Amaral
## Note: Please change db, password in the environment part of the script
# Define colors
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
MB_PATH_DEFAULT="/opt/metabase"
printf "${GREEN}Hello, $USER. This script will configure, install, and run Metabase on port localhost:3000.${NC}\n"
# Function to check if a package is installed
is_installed() {
dpkg-query -W -f='${Status}' $1 2>/dev/null | grep -c "ok installed"
}
# Function to show progress
show_progress() {
printf "${YELLOW}Installing $1...${NC}"
while ps | grep $2 > /dev/null; do
printf "."
sleep 1
done
printf "${GREEN}done!${NC}\n"
}
# Update package list
printf "${YELLOW}Updating package list...${NC}\n"
sudo apt update -qy
# Check and install necessary packages
for package in curl apt rsyslog postgresql nginx default-jdk; do
if [ $(is_installed $package) -eq 0 ]; then
printf "${YELLOW}Installing $package...${NC}\n"
sudo apt install -qy $package &
show_progress $package $!
else
printf "${GREEN}$package is already installed.${NC}\n"
fi
done
# Prompt for Metabase installation directory
printf "Enter your directory to install Metabase (e.g., '/opt/metabase') and press [ENTER]: "
read MB_PATH
MB_PATH="${MB_PATH:-$MB_PATH_DEFAULT}"
printf "${GREEN}Selected directory will be $MB_PATH${NC}\n"
# Fetch the latest Metabase version using GitHub API
printf "${YELLOW}Fetching the latest Metabase version...${NC}\n"
LATEST_VERSION=$(curl -s https://api.github.com/repos/metabase/metabase/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')
printf "${GREEN}Latest Metabase version is $LATEST_VERSION${NC}\n"
# Create Metabase directory
printf "${YELLOW}Creating Metabase directory...${NC}\n"
sudo mkdir -p $MB_PATH
# Create Metabase user and group
printf "${YELLOW}Creating Metabase user and group...${NC}\n"
sudo groupadd -r metabase
sudo useradd -r -s /bin/false -g metabase metabase
sudo chown -R metabase:metabase $MB_PATH
# Create log file and set permissions
printf "${YELLOW}Setting up log file and permissions...${NC}\n"
sudo touch /var/log/metabase.log
sudo chown metabase:metabase /var/log/metabase.log
# Create environment file
printf "${YELLOW}Creating environment file...${NC}\n"
sudo touch /etc/default/metabase
sudo chmod 640 /etc/default/metabase
bash -c "cat <<EOL > /etc/default/metabase
MB_DB_TYPE=postgres
MB_DB_DBNAME=metabase
MB_DB_PORT=5432
MB_DB_USER=metabase
MB_DB_PASS=metabase
MB_DB_HOST=localhost
EOL"
# Check if Metabase is already installed
if [ -f "$MB_PATH/metabase.jar" ]; then
printf "${GREEN}Metabase is already installed at $MB_PATH/metabase.jar. Skipping download.${NC}\n"
else
# Fetch the latest Metabase version using GitHub API
printf "${YELLOW}Fetching the latest Metabase version...${NC}\n"
LATEST_VERSION=$(curl -s https://api.github.com/repos/metabase/metabase/releases/latest | grep -oP '"tag_name": "\K(.*)(?=")')
printf "${GREEN}Latest Metabase version is $LATEST_VERSION${NC}\n"
# Download Metabase
printf "${YELLOW}Downloading Metabase...${NC}\n"
cd $MB_PATH
sudo curl -LO https://downloads.metabase.com/$LATEST_VERSION/metabase.jar
sudo chown metabase:metabase metabase.jar
fi
# Create systemd service file for Metabase
printf "${YELLOW}Creating systemd service file...${NC}\n"
sudo bash -c "cat <<EOL > /etc/systemd/system/metabase.service
[Unit]
Description=Metabase server
After=syslog.target
After=network.target
[Service]
WorkingDirectory=$MB_PATH
ExecStart=/usr/bin/java --add-opens java.base/java.nio=ALL-UNNAMED -jar $MB_PATH/metabase.jar
EnvironmentFile=/etc/default/metabase
User=metabase
Type=simple
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=metabase
SuccessExitStatus=143
TimeoutStopSec=120
Restart=always
[Install]
WantedBy=multi-user.target
EOL"
# Configure rsyslog for Metabase
touch /etc/rsyslog.d/metabase.conf
printf "${YELLOW}Configuring rsyslog for Metabase...${NC}\n"
sudo bash -c "cat <<EOL > /etc/rsyslog.d/metabase.conf
if \$programname == 'metabase' then /var/log/metabase.log
& stop
EOL"
# Restart rsyslog service
printf "${YELLOW}Restarting rsyslog service...${NC}\n"
sudo systemctl restart rsyslog.service
# Reload systemd manager configuration
printf "${YELLOW}Reloading systemd manager configuration...${NC}\n"
sudo systemctl daemon-reload
# Start and enable Metabase service
printf "${YELLOW}Starting and enabling Metabase service...${NC}\n"
sudo systemctl start metabase.service
sudo systemctl enable metabase.service
# Configure PostgreSQL (basic setup)
printf "${YELLOW}Configuring PostgreSQL...${NC}\n"
sudo -u postgres psql -c "CREATE USER metabase WITH PASSWORD 'metabase';"
sudo -u postgres psql -c "CREATE DATABASE metabase OWNER metabase;"
# Configure Nginx (basic setup)
printf "${YELLOW}Configuring Nginx...${NC}\n"
touch /etc/nginx/sites-available/metabase
cat > /etc/nginx/sites-available/metabase <<\EOF
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:3000;
proxy_set_header Host \$host;
proxy_set_header X-Real-IP \$remote_addr;
proxy_set_header X-Forwarded-For \$proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto \ $scheme;
}
}
EOF
sudo ln -s /etc/nginx/sites-available/metabase /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl restart nginx
printf "${GREEN}Metabase installation and configuration complete.${NC}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment