etc.git


etc

~ scripts and random things
git clone https://github.com/djnnvx/etc.git
log | files | refs | archive

ollama-server-setup.sh (3528B)

      1 #!/bin/bash
      2 set -euo pipefail
      3 
      4 # --- CONFIGURATION ---
      5 # The target Tailscale IP is used for documentation purposes.
      6 # The UI will listen on all interfaces (0.0.0.0) but is accessed via Tailscale.
      7 TAILSCALE_IP=$(tailscale ip -4)
      8 WEBUI_PORT="8080" # The UI runs inside the host network on this port
      9 OLLAMA_PORT="11434"
     10 
     11 # Models to download and install
     12 MODELS=(
     13     "deepseek-r1:8b"
     14     "nomic-embed-text"
     15 )
     16 # ---------------------
     17 
     18 echo "Starting AI Server Setup..."
     19 
     20 install_docker() {
     21     if command -v docker &> /dev/null; then
     22         echo "Docker is already installed."
     23         sudo usermod -aG docker "$USER" || true
     24         return 0
     25     fi
     26 
     27     echo "Installing Docker..."
     28     curl -fsSL https://get.docker.com -o get-docker.sh
     29     sh get-docker.sh
     30     rm get-docker.sh
     31 
     32     sudo usermod -aG docker "$USER" || true
     33 
     34     echo "Docker installation complete. You must log out and log back in for the 'docker' command to work without 'sudo'."
     35 }
     36 
     37 configure_systemd() {
     38     echo "Creating systemd overrides for dependencies and network settings..."
     39 
     40     SSH_DROPIN_DIR="/etc/systemd/system/ssh.service.d"
     41     sudo mkdir -p "$SSH_DROPIN_DIR"
     42     sudo cat <<EOF > "$SSH_DROPIN_DIR/override.conf"
     43 [Unit]
     44 After=tailscaled.service
     45 Wants=tailscaled.service
     46 EOF
     47     echo "SSH systemd override created."
     48 
     49     OLLAMA_DROPIN_DIR="/etc/systemd/system/ollama.service.d"
     50     sudo mkdir -p "$OLLAMA_DROPIN_DIR"
     51     sudo cat <<EOF > "$OLLAMA_DROPIN_DIR/override.conf"
     52 [Service]
     53 Environment="OLLAMA_HOST=0.0.0.0"
     54 EOF
     55     echo "Ollama systemd override created."
     56 }
     57 
     58 setup_webui() {
     59     echo "Setting up Open WebUI container and systemd service..."
     60 
     61     sudo docker stop open-webui 2>/dev/null || true
     62     sudo docker rm open-webui 2>/dev/null || true
     63 
     64     sudo docker run -d \
     65       --network=host \
     66       -e OLLAMA_BASE_URL=http://127.0.0.1:$OLLAMA_PORT \
     67       -v open-webui:/app/backend/data \
     68       --name open-webui \
     69       --restart always \
     70       ghcr.io/open-webui/open-webui:main
     71 
     72     WEBUI_SERVICE_PATH="/etc/systemd/system/docker.open-webui.service"
     73     sudo cat <<EOF > "$WEBUI_SERVICE_PATH"
     74 [Unit]
     75 Description=Open WebUI Docker Container
     76 Requires=docker.service
     77 After=docker.service
     78 
     79 [Service]
     80 Restart=always
     81 ExecStart=/usr/bin/docker start -a open-webui
     82 ExecStop=/usr/bin/docker stop open-webui
     83 
     84 [Install]
     85 WantedBy=multi-user.target
     86 EOF
     87 
     88     echo "Open WebUI container started and service unit created."
     89 }
     90 
     91 install_models() {
     92     echo "Downloading Ollama models..."
     93 
     94     sudo systemctl restart ollama
     95 
     96     for MODEL in "${MODELS[@]}"; do
     97         echo "Pulling model $MODEL..."
     98         sudo ollama pull "$MODEL"
     99         if [ $? -eq 0 ]; then
    100             echo "Model $MODEL installed."
    101         else
    102             echo "ERROR: Failed to install model $MODEL."
    103         fi
    104     done
    105 }
    106 
    107 # --- Main Execution Flow ---
    108 
    109 install_docker
    110 configure_systemd
    111 
    112 echo "Reloading systemd daemon and starting services..."
    113 sudo systemctl daemon-reload
    114 sudo systemctl restart ollama
    115 sudo systemctl enable docker.open-webui.service
    116 sudo systemctl start docker.open-webui.service
    117 sudo systemctl restart ssh
    118 
    119 setup_webui
    120 install_models
    121 
    122 echo "--- SETUP COMPLETE ---"
    123 echo " "
    124 echo "Ollama and Open WebUI are installed and configured to start automatically."
    125 echo " "
    126 echo "1. Access the Web UI:"
    127 echo "   http://$TAILSCALE_IP:$WEBUI_PORT"
    128 echo " "
    129 echo "2. IMPORTANT: If you just installed Docker, you must log out and log back in for your user to run Docker commands without 'sudo'."
    130 echo "3. The first user to sign up on the UI will be the administrator."