Repository: ollama/ollama
File: scripts/install.sh (Shell)

Code before:
            install_cuda_drivers
        else
            echo "Detected CUDA version $CUDA_VERSION"
        fi
    else
        if lspci | grep -i "nvidia" >/dev/null 2>&1; then
            echo "Warning: NVIDIA GPU detected but NVIDIA-SMI is not available. Installing CUDA drivers..."
            install_cuda_drivers
        else
            echo "No NVIDIA GPU detected. Skipping driver installation."
        fi
    fi
}

download_ollama() {
    ${SUDO_CMD} mkdir -p /usr/bin
    ${SUDO_CMD} curl -fsSL -o /usr/bin/ollama "https://ollama.ai/download/latest/ollama-linux-$ARCH_SUFFIX"
}

configure_systemd() {
    if command -v systemctl >/dev/null 2>&1; then
        echo "Creating systemd service file for ollama..."
        cat <<EOF | ${SUDO_CMD} tee /etc/systemd/system/ollama.service >/dev/null
[Unit]
Description=Ollama Service
After=network.target

[Service]
ExecStart=/usr/bin/ollama serve
Restart=always
RestartSec=3
Environment="HOME=$HOME"

[Install]
WantedBy=default.target
EOF
        echo "Reloading systemd and enabling ollama service..."
        ${SUDO_CMD} systemctl daemon-reload
        ${SUDO_CMD} systemctl enable ollama
        ${SUDO_CMD} systemctl restart ollama
    else
        echo "Installation complete. Run 'ollama serve' from the command line to start the service. Use 'ollama run' to query a model."
        exit 0
    fi
}

main() {
    check_os
    determine_architecture
    check_sudo
    download_ollama

Review comment (suggestion):
It's not super important for this service but generally you'd want `network-online.target`. `network.target` doesn't mean networking or connectivity is available and is intended for services to _configure_ networking. Since ollama listen on loopback by default, this shouldn't be an issue but to avoid future problems, I'd suggest using  `network-online.target` instead.

Code after:
        else
            echo "Warning: NVIDIA GPU detected but NVIDIA-SMI is not available. Installing CUDA drivers..."
            install_cuda_drivers
        fi
    else
        echo "No NVIDIA GPU detected. Skipping driver installation."
    fi
}

download_ollama() {
    $SUDO_CMD mkdir -p /usr/bin
    $SUDO_CMD curl -fsSL -o /usr/bin/ollama "https://ollama.ai/download/latest/ollama-linux-$ARCH_SUFFIX"
}

configure_systemd() {
    if command -v systemctl >/dev/null 2>&1; then
        $SUDO_CMD useradd -r -s /bin/false -m -d /home/ollama ollama 2>/dev/null 

        echo "Creating systemd service file for ollama..."
        cat <<EOF | $SUDO_CMD tee /etc/systemd/system/ollama.service >/dev/null
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
ExecStart=/usr/bin/ollama serve
User=ollama
Group=ollama
Restart=always
RestartSec=3
Environment="HOME=/home/ollama"

[Install]
WantedBy=default.target
EOF
        echo "Reloading systemd and enabling ollama service..."
        if [ "$(systemctl is-system-running || echo 'not running')" = 'running' ]; then 
            $SUDO_CMD systemctl daemon-reload
            $SUDO_CMD systemctl enable ollama
            $SUDO_CMD systemctl restart ollama
        fi
    else
        echo "Run 'ollama serve' from the command line to start the service."
    fi
}

main() {
    check_os
    determine_architecture
    check_sudo
    download_ollama