install.sh 13.5 KB
Newer Older
1
2
3
4
5
6
#!/bin/sh
# This script installs Ollama on Linux.
# It detects the current operating system architecture and installs the appropriate version of Ollama.

set -eu

7
8
red="$( (/usr/bin/tput bold || :; /usr/bin/tput setaf 1 || :) 2>&-)"
plain="$( (/usr/bin/tput sgr0 || :) 2>&-)"
9

10
status() { echo ">>> $*" >&2; }
11
12
error() { echo "${red}ERROR:${plain} $*"; exit 1; }
warning() { echo "${red}WARNING:${plain} $*"; }
13

14
15
16
TEMP_DIR=$(mktemp -d)
cleanup() { rm -rf $TEMP_DIR; }
trap cleanup EXIT
17

Michael Yang's avatar
Michael Yang committed
18
19
available() { command -v $1 >/dev/null; }
require() {
20
21
    local MISSING=''
    for TOOL in $*; do
Michael Yang's avatar
Michael Yang committed
22
        if ! available $TOOL; then
23
            MISSING="$MISSING $TOOL"
24
        fi
25
26
27
    done

    echo $MISSING
28
29
}

30
[ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.'
31

32
33
ARCH=$(uname -m)
case "$ARCH" in
34
35
36
37
    x86_64) ARCH="amd64" ;;
    aarch64|arm64) ARCH="arm64" ;;
    *) error "Unsupported architecture: $ARCH" ;;
esac
38

Jeffrey Morgan's avatar
Jeffrey Morgan committed
39
40
IS_WSL2=false

41
42
KERN=$(uname -r)
case "$KERN" in
43
    *icrosoft*WSL2 | *icrosoft*wsl2) IS_WSL2=true;;
44
    *icrosoft) error "Microsoft WSL1 is not currently supported. Please use WSL2 with 'wsl --set-version <distro> 2'" ;;
45
46
47
    *) ;;
esac

48
VER_PARAM="${OLLAMA_VERSION:+?version=$OLLAMA_VERSION}"
49

50
SUDO=
51
52
if [ "$(id -u)" -ne 0 ]; then
    # Running as root, no need for sudo
Michael Yang's avatar
Michael Yang committed
53
    if ! available sudo; then
Michael Yang's avatar
Michael Yang committed
54
        error "This script requires superuser permissions. Please re-run as root."
55
    fi
56
57

    SUDO="sudo"
58
fi
59

Michael Yang's avatar
Michael Yang committed
60
61
NEEDS=$(require curl awk grep sed tee xargs)
if [ -n "$NEEDS" ]; then
Michael Yang's avatar
Michael Yang committed
62
    status "ERROR: The following tools are required but missing:"
Michael Yang's avatar
Michael Yang committed
63
64
    for NEED in $NEEDS; do
        echo "  - $NEED"
Michael Yang's avatar
Michael Yang committed
65
66
    done
    exit 1
67
68
fi

69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# Function to download and extract with fallback from zst to tgz
download_and_extract() {
    local url_base="$1"
    local dest_dir="$2"
    local filename="$3"

    # Check if .tar.zst is available
    if curl --fail --silent --head --location "${url_base}/${filename}.tar.zst${VER_PARAM}" >/dev/null 2>&1; then
        # zst file exists - check if we have zstd tool
        if ! available zstd; then
            error "This version requires zstd for extraction. Please install zstd and try again:
  - Debian/Ubuntu: sudo apt-get install zstd
  - RHEL/CentOS/Fedora: sudo dnf install zstd
  - Arch: sudo pacman -S zstd"
        fi

        status "Downloading ${filename}.tar.zst"
        curl --fail --show-error --location --progress-bar \
            "${url_base}/${filename}.tar.zst${VER_PARAM}" | \
            zstd -d | $SUDO tar -xf - -C "${dest_dir}"
        return 0
    fi

    # Fall back to .tgz for older versions
    status "Downloading ${filename}.tgz"
    curl --fail --show-error --location --progress-bar \
        "${url_base}/${filename}.tgz${VER_PARAM}" | \
        $SUDO tar -xzf - -C "${dest_dir}"
}

99
100
101
for BINDIR in /usr/local/bin /usr/bin /bin; do
    echo $PATH | grep -q $BINDIR && break || continue
done
102
OLLAMA_INSTALL_DIR=$(dirname ${BINDIR})
103

104
105
106
107
if [ -d "$OLLAMA_INSTALL_DIR/lib/ollama" ] ; then
    status "Cleaning up old version at $OLLAMA_INSTALL_DIR/lib/ollama"
    $SUDO rm -rf "$OLLAMA_INSTALL_DIR/lib/ollama"
fi
Daniel Hiltgen's avatar
Daniel Hiltgen committed
108
status "Installing ollama to $OLLAMA_INSTALL_DIR"
109
$SUDO install -o0 -g0 -m755 -d $BINDIR
Michael Yang's avatar
Michael Yang committed
110
$SUDO install -o0 -g0 -m755 -d "$OLLAMA_INSTALL_DIR/lib/ollama"
111
download_and_extract "https://ollama.com/download" "$OLLAMA_INSTALL_DIR" "ollama-linux-${ARCH}"
Michael Yang's avatar
Michael Yang committed
112

113
114
115
if [ "$OLLAMA_INSTALL_DIR/bin/ollama" != "$BINDIR/ollama" ] ; then
    status "Making ollama accessible in the PATH in $BINDIR"
    $SUDO ln -sf "$OLLAMA_INSTALL_DIR/ollama" "$BINDIR/ollama"
Daniel Hiltgen's avatar
Daniel Hiltgen committed
116
117
fi

118
119
120
# Check for NVIDIA JetPack systems with additional downloads
if [ -f /etc/nv_tegra_release ] ; then
    if grep R36 /etc/nv_tegra_release > /dev/null ; then
121
        download_and_extract "https://ollama.com/download" "$OLLAMA_INSTALL_DIR" "ollama-linux-${ARCH}-jetpack6"
122
    elif grep R35 /etc/nv_tegra_release > /dev/null ; then
123
        download_and_extract "https://ollama.com/download" "$OLLAMA_INSTALL_DIR" "ollama-linux-${ARCH}-jetpack5"
124
125
126
127
    else
        warning "Unsupported JetPack version detected.  GPU may not be supported"
    fi
fi
128

129
install_success() {
Josh's avatar
Josh committed
130
    status 'The Ollama API is now available at 127.0.0.1:11434.'
131
132
    status 'Install complete. Run "ollama" from the command line.'
}
133
134
135
trap install_success EXIT

# Everything from this point onwards is optional.
136
137

configure_systemd() {
138
139
    if ! id ollama >/dev/null 2>&1; then
        status "Creating ollama user..."
140
        $SUDO useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama
141
    fi
142
143
144
145
    if getent group render >/dev/null 2>&1; then
        status "Adding ollama user to render group..."
        $SUDO usermod -a -G render ollama
    fi
146
147
148
149
    if getent group video >/dev/null 2>&1; then
        status "Adding ollama user to video group..."
        $SUDO usermod -a -G video ollama
    fi
150

151
152
153
    status "Adding current user to ollama group..."
    $SUDO usermod -a -G ollama $(whoami)

154
    status "Creating ollama systemd service..."
155
    cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
156
157
158
159
160
[Unit]
Description=Ollama Service
After=network-online.target

[Service]
161
ExecStart=$BINDIR/ollama serve
162
163
164
165
User=ollama
Group=ollama
Restart=always
RestartSec=3
Michael Yang's avatar
Michael Yang committed
166
Environment="PATH=$PATH"
167
168
169
170

[Install]
WantedBy=default.target
EOF
171
    SYSTEMCTL_RUNNING="$(systemctl is-system-running || true)"
Michael Yang's avatar
Michael Yang committed
172
173
174
175
176
    case $SYSTEMCTL_RUNNING in
        running|degraded)
            status "Enabling and starting ollama service..."
            $SUDO systemctl daemon-reload
            $SUDO systemctl enable ollama
177
178
179

            start_service() { $SUDO systemctl restart ollama; }
            trap start_service EXIT
Michael Yang's avatar
Michael Yang committed
180
            ;;
181
182
183
184
185
186
        *)
            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
            ;;
Michael Yang's avatar
Michael Yang committed
187
    esac
188
189
}

Michael Yang's avatar
Michael Yang committed
190
if available systemctl; then
191
    configure_systemd
192
193
fi

194
195
196
# WSL2 only supports GPUs via nvidia passthrough
# so check for nvidia-smi to determine if GPU is available
if [ "$IS_WSL2" = true ]; then
197
    if available nvidia-smi && [ -n "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
198
199
200
201
202
203
        status "Nvidia GPU detected."
    fi
    install_success
    exit 0
fi

204
205
206
207
208
209
210
# 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

211
# Install GPU dependencies on Linux
Michael Yang's avatar
Michael Yang committed
212
if ! available lspci && ! available lshw; then
213
    warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
Michael Yang's avatar
Michael Yang committed
214
215
216
    exit 0
fi

217
check_gpu() {
218
    # Look for devices based on vendor ID for NVIDIA and AMD
219
    case $1 in
220
        lspci)
221
222
223
224
            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 ;;
225
        lshw)
226
            case $2 in
227
228
                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 ;;
229
            esac ;;
Michael Yang's avatar
Michael Yang committed
230
        nvidia-smi) available nvidia-smi || return 1 ;;
231
    esac
232
233
}

234
235
236
237
238
if check_gpu nvidia-smi; then
    status "NVIDIA GPU installed."
    exit 0
fi

239
if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
240
    install_success
241
242
243
244
245
    warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode."
    exit 0
fi

if check_gpu lspci amdgpu || check_gpu lshw amdgpu; then
246
    download_and_extract "https://ollama.com/download" "$OLLAMA_INSTALL_DIR" "ollama-linux-${ARCH}-rocm"
247
248

    install_success
249
    status "AMD GPU ready."
250
251
252
    exit 0
fi

253
CUDA_REPO_ERR_MSG="NVIDIA GPU detected, but your OS and Architecture are not supported by NVIDIA.  Please install the CUDA driver manually https://docs.nvidia.com/cuda/cuda-installation-guide-linux/"
254
255
256
257
258
259
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
install_cuda_driver_yum() {
    status 'Installing NVIDIA repository...'
260
    
261
262
    case $PACKAGE_MANAGER in
        yum)
263
            $SUDO $PACKAGE_MANAGER -y install yum-utils
Daniel Hiltgen's avatar
Daniel Hiltgen committed
264
265
            if curl -I --silent --fail --location "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-$1$2.repo" >/dev/null ; then
                $SUDO $PACKAGE_MANAGER-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-$1$2.repo
266
267
268
            else
                error $CUDA_REPO_ERR_MSG
            fi
269
270
            ;;
        dnf)
Daniel Hiltgen's avatar
Daniel Hiltgen committed
271
272
            if curl -I --silent --fail --location "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-$1$2.repo" >/dev/null ; then
                $SUDO $PACKAGE_MANAGER config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-$1$2.repo
273
274
275
            else
                error $CUDA_REPO_ERR_MSG
            fi
276
277
278
279
280
281
282
            ;;
    esac

    case $1 in
        rhel)
            status 'Installing EPEL repository...'
            # EPEL is required for third-party dependencies such as dkms and libvdpau
283
            $SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
284
285
286
287
288
289
            ;;
    esac

    status 'Installing CUDA driver...'

    if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
290
        $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
291
    fi
292
293

    $SUDO $PACKAGE_MANAGER -y install cuda-drivers
294
295
296
297
298
299
}

# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
install_cuda_driver_apt() {
    status 'Installing NVIDIA repository...'
Daniel Hiltgen's avatar
Daniel Hiltgen committed
300
301
    if curl -I --silent --fail --location "https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-keyring_1.1-1_all.deb" >/dev/null ; then
        curl -fsSL -o $TEMP_DIR/cuda-keyring.deb https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m | sed -e 's/aarch64/sbsa/')/cuda-keyring_1.1-1_all.deb
302
303
304
    else
        error $CUDA_REPO_ERR_MSG
    fi
305
306
307
308

    case $1 in
        debian)
            status 'Enabling contrib sources...'
309
            $SUDO sed 's/main/contrib/' < /etc/apt/sources.list | $SUDO tee /etc/apt/sources.list.d/contrib.list > /dev/null
310
311
312
            if [ -f "/etc/apt/sources.list.d/debian.sources" ]; then
                $SUDO sed 's/main/contrib/' < /etc/apt/sources.list.d/debian.sources | $SUDO tee /etc/apt/sources.list.d/contrib.sources > /dev/null
            fi
313
314
315
316
            ;;
    esac

    status 'Installing CUDA driver...'
317
318
    $SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
    $SUDO apt-get update
Michael Yang's avatar
Michael Yang committed
319
320
321

    [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
    DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
322
323
324
325
326
327
328
329
330
331
332
333
334
}

if [ ! -f "/etc/os-release" ]; then
    error "Unknown distribution. Skipping CUDA installation."
fi

. /etc/os-release

OS_NAME=$ID
OS_VERSION=$VERSION_ID

PACKAGE_MANAGER=
for PACKAGE_MANAGER in dnf yum apt-get; do
Michael Yang's avatar
Michael Yang committed
335
    if available $PACKAGE_MANAGER; then
336
337
338
339
340
341
342
343
344
345
        break
    fi
done

if [ -z "$PACKAGE_MANAGER" ]; then
    error "Unknown package manager. Skipping CUDA installation."
fi

if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
    case $OS_NAME in
346
        centos|rhel) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -d '.' -f 1) ;;
347
        rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
348
        fedora) [ $OS_VERSION -lt '39' ] && install_cuda_driver_yum $OS_NAME $OS_VERSION || install_cuda_driver_yum $OS_NAME '39';;
Michael Yang's avatar
Michael Yang committed
349
        amzn) install_cuda_driver_yum 'fedora' '37' ;;
Michael Yang's avatar
Michael Yang committed
350
351
        debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
        ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
Michael Yang's avatar
Michael Yang committed
352
        *) exit ;;
353
354
355
    esac
fi

356
if ! lsmod | grep -q nvidia || ! lsmod | grep -q nvidia_uvm; then
357
358
    KERNEL_RELEASE="$(uname -r)"
    case $OS_NAME in
359
360
        rocky) $SUDO $PACKAGE_MANAGER -y install kernel-devel kernel-headers ;;
        centos|rhel|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
Bruce MacDonald's avatar
Bruce MacDonald committed
361
        fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;
362
        debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
Michael Yang's avatar
Michael Yang committed
363
        *) exit ;;
364
365
    esac

366
    NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
Michael Yang's avatar
Michael Yang committed
367
368
369
370
    if [ -n "$NVIDIA_CUDA_VERSION" ]; then
        $SUDO dkms install $NVIDIA_CUDA_VERSION
    fi

371
372
373
374
375
376
377
378
    if lsmod | grep -q nouveau; then
        status 'Reboot to complete NVIDIA CUDA driver install.'
        exit 0
    fi

    $SUDO modprobe nvidia
    $SUDO modprobe nvidia_uvm
fi
Michael Yang's avatar
Michael Yang committed
379

380
# make sure the NVIDIA modules are loaded on boot with nvidia-persistenced
381
if available nvidia-persistenced; then
382
383
384
385
    $SUDO touch /etc/modules-load.d/nvidia.conf
    MODULES="nvidia nvidia-uvm"
    for MODULE in $MODULES; do
        if ! grep -qxF "$MODULE" /etc/modules-load.d/nvidia.conf; then
386
            echo "$MODULE" | $SUDO tee -a /etc/modules-load.d/nvidia.conf > /dev/null
387
388
        fi
    done
389
fi
390

391
status "NVIDIA GPU ready."
392
install_success