CoolFace
Apppublic

jaothan/DockerGenAI_Streamlit

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
install_ollama.sh244 linesDownload Raw Back to root
1#!/bin/sh
2# This script installs Ollama on Linux.
3# It detects the current operating system architecture and installs the appropriate version of Ollama.
4
5set -eu
6
7status() { echo ">>> $*" >&2; }
8error() { echo "ERROR $*"; exit 1; }
9warning() { echo "WARNING: $*"; }
10
11TEMP_DIR=$(mktemp -d)
12cleanup() { rm -rf $TEMP_DIR; }
13trap cleanup EXIT
14
15available() { command -v $1 >/dev/null; }
16require() {
17    local MISSING=''
18    for TOOL in $*; do
19        if ! available $TOOL; then
20            MISSING="$MISSING $TOOL"
21        fi
22    done
23
24    echo $MISSING
25}
26
27[ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.'
28
29case "$(uname -m)" in
30    x86_64) ARCH="amd64" ;;
31    aarch64|arm64) ARCH="arm64" ;;
32    *) error "Unsupported architecture: $ARCH" ;;
33esac
34
35SUDO=
36if [ "$(id -u)" -ne 0 ]; then
37    # Running as root, no need for sudo
38    if ! available sudo; then
39        error "This script requires superuser permissions. Please re-run as root."
40    fi
41
42    SUDO="sudo"
43fi
44
45NEEDS=$(require curl awk grep sed tee xargs)
46if [ -n "$NEEDS" ]; then
47    status "ERROR: The following tools are required but missing:"
48    for NEED in $NEEDS; do
49        echo "  - $NEED"
50    done
51    exit 1
52fi
53
54status "Downloading ollama..."
55curl --fail --show-error --location --progress-bar -o $TEMP_DIR/ollama "https://ollama.ai/download/ollama-linux-$ARCH"
56
57for BINDIR in /usr/local/bin /usr/bin /bin; do
58    echo $PATH | grep -q $BINDIR && break || continue
59done
60
61status "Installing ollama to $BINDIR..."
62$SUDO install -o0 -g0 -m755 -d $BINDIR
63$SUDO install -o0 -g0 -m755 $TEMP_DIR/ollama $BINDIR/ollama
64
65install_success() { status 'Install complete. Run "ollama" from the command line.'; }
66trap install_success EXIT
67
68# Everything from this point onwards is optional.
69
70configure_systemd() {
71    if ! id ollama >/dev/null 2>&1; then
72        status "Creating ollama user..."
73        $SUDO useradd -r -s /bin/false -m -d /usr/share/ollama ollama
74    fi
75
76    status "Creating ollama systemd service..."
77    cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
78[Unit]
79Description=Ollama Service
80After=network-online.target
81
82[Service]
83ExecStart=$BINDIR/ollama serve
84User=ollama
85Group=ollama
86Restart=always
87RestartSec=3
88Environment="HOME=/usr/share/ollama"
89Environment="PATH=$PATH"
90
91[Install]
92WantedBy=default.target
93EOF
94    SYSTEMCTL_RUNNING="$(systemctl is-system-running || true)"
95    case $SYSTEMCTL_RUNNING in
96        running|degraded)
97            status "Enabling and starting ollama service..."
98            $SUDO systemctl daemon-reload
99            $SUDO systemctl enable ollama
100
101            start_service() { $SUDO systemctl restart ollama; }
102            trap start_service EXIT
103            ;;
104    esac
105}
106
107if available systemctl; then
108    configure_systemd
109fi
110
111if ! available lspci && ! available lshw; then
112    warning "Unable to detect NVIDIA GPU. Install lspci or lshw to automatically detect and install NVIDIA CUDA drivers."
113    exit 0
114fi
115
116check_gpu() {
117    case $1 in
118        lspci) available lspci && lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;;
119        lshw) available lshw && $SUDO lshw -c display -numeric | grep -q 'vendor: .* \[10DE\]' || return 1 ;;
120        nvidia-smi) available nvidia-smi || return 1 ;;
121    esac
122}
123
124if check_gpu nvidia-smi; then
125    status "NVIDIA GPU installed."
126    exit 0
127fi
128
129if ! check_gpu lspci && ! check_gpu lshw; then
130    warning "No NVIDIA GPU detected. Ollama will run in CPU-only mode."
131    exit 0
132fi
133
134# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
135# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
136# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
137# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
138install_cuda_driver_yum() {
139    status 'Installing NVIDIA repository...'
140    case $PACKAGE_MANAGER in
141        yum)
142            $SUDO $PACKAGE_MANAGER -y install yum-utils
143            $SUDO $PACKAGE_MANAGER-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
144            ;;
145        dnf)
146            $SUDO $PACKAGE_MANAGER config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
147            ;;
148    esac
149
150    case $1 in
151        rhel)
152            status 'Installing EPEL repository...'
153            # EPEL is required for third-party dependencies such as dkms and libvdpau
154            $SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
155            ;;
156    esac
157
158    status 'Installing CUDA driver...'
159
160    if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
161        $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
162    fi
163
164    $SUDO $PACKAGE_MANAGER -y install cuda-drivers
165}
166
167# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
168# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
169install_cuda_driver_apt() {
170    status 'Installing NVIDIA repository...'
171    curl -fsSL -o $TEMP_DIR/cuda-keyring.deb https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-keyring_1.1-1_all.deb
172
173    case $1 in
174        debian)
175            status 'Enabling contrib sources...'
176            $SUDO sed 's/main/contrib/' < /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/contrib.list > /dev/null
177            ;;
178    esac
179
180    status 'Installing CUDA driver...'
181    $SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
182    $SUDO apt-get update
183
184    [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
185    DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
186}
187
188if [ ! -f "/etc/os-release" ]; then
189    error "Unknown distribution. Skipping CUDA installation."
190fi
191
192. /etc/os-release
193
194OS_NAME=$ID
195OS_VERSION=$VERSION_ID
196
197PACKAGE_MANAGER=
198for PACKAGE_MANAGER in dnf yum apt-get; do
199    if available $PACKAGE_MANAGER; then
200        break
201    fi
202done
203
204if [ -z "$PACKAGE_MANAGER" ]; then
205    error "Unknown package manager. Skipping CUDA installation."
206fi
207
208if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
209    case $OS_NAME in
210        centos|rhel) install_cuda_driver_yum 'rhel' $OS_VERSION ;;
211        rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
212        fedora) install_cuda_driver_yum $OS_NAME $OS_VERSION ;;
213        amzn) install_cuda_driver_yum 'fedora' '35' ;;
214        debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
215        ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
216        *) exit ;;
217    esac
218fi
219
220if ! lsmod | grep -q nvidia; then
221    KERNEL_RELEASE="$(uname -r)"
222    case $OS_NAME in
223        centos|rhel|rocky|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
224        fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;
225        debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
226        *) exit ;;
227    esac
228
229    NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
230    if [ -n "$NVIDIA_CUDA_VERSION" ]; then
231        $SUDO dkms install $NVIDIA_CUDA_VERSION
232    fi
233
234    if lsmod | grep -q nouveau; then
235        status 'Reboot to complete NVIDIA CUDA driver install.'
236        exit 0
237    fi
238
239    $SUDO modprobe nvidia
240fi
241
242
243status "NVIDIA CUDA drivers installed."
244