forked from ollama/ollama
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
#!/bin/bash | ||
|
||
|
||
TEMP_DIR=$(mktemp -d) | ||
|
||
status() { echo ">>> $*" >&2; } | ||
error() { echo "${red}ERROR:${plain} $*"; exit 1; } | ||
warning() { echo "${red}WARNING:${plain} $*"; } | ||
cleanup() { rm -rf $TEMP_DIR; } | ||
available() { command -v $1 >/dev/null; } | ||
|
||
BINDIR=/bin | ||
IS_WSL2=false | ||
|
||
KERN=$(uname -r) | ||
case "$KERN" in | ||
*icrosoft*WSL2 | *icrosoft*wsl2) IS_WSL2=true;; | ||
*icrosoft) error "Microsoft WSL1 is not currently supported. Please use WSL2 with 'wsl --set-version <distro> 2'" ;; | ||
*) ;; | ||
esac | ||
|
||
|
||
install_success() { | ||
status 'The Ollama API is now available at 127.0.0.1:11434.' | ||
status 'Install complete. Run "ollama" from the command line.' | ||
} | ||
trap install_success EXIT | ||
|
||
# Everything from this point onwards is optional. | ||
|
||
configure_systemd() { | ||
if ! id ollama >/dev/null 2>&1; then | ||
status "Creating ollama user..." | ||
$SUDO useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama | ||
fi | ||
if getent group render >/dev/null 2>&1; then | ||
status "Adding ollama user to render group..." | ||
$SUDO usermod -a -G render ollama | ||
fi | ||
if getent group video >/dev/null 2>&1; then | ||
status "Adding ollama user to video group..." | ||
$SUDO usermod -a -G video ollama | ||
fi | ||
|
||
status "Adding current user to ollama group..." | ||
$SUDO usermod -a -G ollama $(whoami) | ||
|
||
status "Creating ollama systemd service..." | ||
cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null | ||
[Unit] | ||
Description=Ollama Service | ||
After=network-online.target | ||
[Service] | ||
ExecStart=$BINDIR/ollama serve | ||
User=ollama | ||
Group=ollama | ||
Restart=always | ||
RestartSec=3 | ||
Environment="PATH=$PATH" | ||
[Install] | ||
WantedBy=default.target | ||
EOF | ||
SYSTEMCTL_RUNNING="$(systemctl is-system-running || true)" | ||
case $SYSTEMCTL_RUNNING in | ||
running|degraded) | ||
status "Enabling and starting ollama service..." | ||
$SUDO systemctl daemon-reload | ||
$SUDO systemctl enable ollama | ||
|
||
start_service() { $SUDO systemctl restart ollama; } | ||
trap start_service EXIT | ||
;; | ||
*) | ||
warning "systemd is not running" | ||
if [ "$IS_WSL2" = true ]; then | ||
warning "see https://learn.microsoft.com/en-us/windows/wsl/systemd#how-to-enable-systemd to enable it" | ||
fi | ||
;; | ||
esac | ||
} | ||
|
||
if available systemctl; then | ||
configure_systemd | ||
fi | ||
|
||
# WSL2 only supports GPUs via nvidia passthrough | ||
# so check for nvidia-smi to determine if GPU is available | ||
if [ "$IS_WSL2" = true ]; then | ||
if available nvidia-smi && [ -n "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then | ||
status "Nvidia GPU detected." | ||
fi | ||
install_success | ||
exit 0 | ||
fi | ||
|
||
# Don't attempt to install drivers on Jetson systems | ||
if [ -f /etc/nv_tegra_release ] ; then | ||
status "NVIDIA JetPack ready." | ||
install_success | ||
exit 0 | ||
fi | ||
|
||
# Install GPU dependencies on Linux | ||
if ! available lspci && ! available lshw; then | ||
warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies." | ||
exit 0 | ||
fi | ||
|
||
check_gpu() { | ||
# Look for devices based on vendor ID for NVIDIA and AMD | ||
case $1 in | ||
lspci) | ||
case $2 in | ||
nvidia) available lspci && lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;; | ||
amdgpu) available lspci && lspci -d '1002:' | grep -q 'AMD' || return 1 ;; | ||
esac ;; | ||
lshw) | ||
case $2 in | ||
nvidia) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[10DE\]' || return 1 ;; | ||
amdgpu) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[1002\]' || return 1 ;; | ||
esac ;; | ||
nvidia-smi) available nvidia-smi || return 1 ;; | ||
esac | ||
} | ||
|
||
if check_gpu nvidia-smi; then | ||
status "NVIDIA GPU installed." | ||
exit 0 | ||
fi | ||
|
||
if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then | ||
install_success | ||
warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode." | ||
exit 0 | ||
fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
#!/bin/bash | ||
sudo rm -rfv /etc/systemd/system/ollama.service |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters