install.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. set -eu
  5. status() { echo ">>> $*" >&2; }
  6. error() { echo "ERROR $*"; exit 1; }
  7. warning() { echo "WARNING: $*"; }
  8. TEMP_DIR=$(mktemp -d)
  9. cleanup() { rm -rf $TEMP_DIR; }
  10. trap cleanup EXIT
  11. available() { command -v $1 >/dev/null; }
  12. require() {
  13. local MISSING=''
  14. for TOOL in $*; do
  15. if ! available $TOOL; then
  16. MISSING="$MISSING $TOOL"
  17. fi
  18. done
  19. echo $MISSING
  20. }
  21. [ "$(uname -s)" = "Linux" ] || error 'This script is intended to run on Linux only.'
  22. ARCH=$(uname -m)
  23. case "$ARCH" in
  24. x86_64) ARCH="amd64" ;;
  25. aarch64|arm64) ARCH="arm64" ;;
  26. *) error "Unsupported architecture: $ARCH" ;;
  27. esac
  28. IS_WSL2=false
  29. KERN=$(uname -r)
  30. case "$KERN" in
  31. *icrosoft*WSL2 | *icrosoft*wsl2) IS_WSL2=true;;
  32. *icrosoft) error "Microsoft WSL1 is not currently supported. Please use WSL2 with 'wsl --set-version <distro> 2'" ;;
  33. *) ;;
  34. esac
  35. VER_PARAM="${OLLAMA_VERSION:+?version=$OLLAMA_VERSION}"
  36. SUDO=
  37. if [ "$(id -u)" -ne 0 ]; then
  38. # Running as root, no need for sudo
  39. if ! available sudo; then
  40. error "This script requires superuser permissions. Please re-run as root."
  41. fi
  42. SUDO="sudo"
  43. fi
  44. NEEDS=$(require curl awk grep sed tee xargs)
  45. if [ -n "$NEEDS" ]; then
  46. status "ERROR: The following tools are required but missing:"
  47. for NEED in $NEEDS; do
  48. echo " - $NEED"
  49. done
  50. exit 1
  51. fi
  52. for BINDIR in /usr/local/bin /usr/bin /bin; do
  53. echo $PATH | grep -q $BINDIR && break || continue
  54. done
  55. OLLAMA_INSTALL_DIR=$(dirname ${BINDIR})
  56. status "Installing ollama to $OLLAMA_INSTALL_DIR"
  57. $SUDO install -o0 -g0 -m755 -d $BINDIR
  58. $SUDO install -o0 -g0 -m755 -d "$OLLAMA_INSTALL_DIR"
  59. if curl -I --silent --fail --location "https://ollama.com/download/ollama-linux-${ARCH}.tgz${VER_PARAM}" >/dev/null ; then
  60. status "Downloading Linux ${ARCH} bundle"
  61. curl --fail --show-error --location --progress-bar \
  62. "https://ollama.com/download/ollama-linux-${ARCH}.tgz${VER_PARAM}" | \
  63. $SUDO tar -xzf - -C "$OLLAMA_INSTALL_DIR"
  64. BUNDLE=1
  65. if [ "$OLLAMA_INSTALL_DIR/bin/ollama" != "$BINDIR/ollama" ] ; then
  66. status "Making ollama accessible in the PATH in $BINDIR"
  67. $SUDO ln -sf "$OLLAMA_INSTALL_DIR/ollama" "$BINDIR/ollama"
  68. fi
  69. else
  70. status "Downloading Linux ${ARCH} CLI"
  71. curl --fail --show-error --location --progress-bar -o "$TEMP_DIR/ollama"\
  72. "https://ollama.com/download/ollama-linux-${ARCH}${VER_PARAM}"
  73. $SUDO install -o0 -g0 -m755 $TEMP_DIR/ollama $OLLAMA_INSTALL_DIR/ollama
  74. BUNDLE=0
  75. if [ "$OLLAMA_INSTALL_DIR/ollama" != "$BINDIR/ollama" ] ; then
  76. status "Making ollama accessible in the PATH in $BINDIR"
  77. $SUDO ln -sf "$OLLAMA_INSTALL_DIR/ollama" "$BINDIR/ollama"
  78. fi
  79. fi
  80. # Check for NVIDIA JetPack systems with additional downloads
  81. if [ -f /etc/nv_tegra_release ] ; then
  82. if grep R36 /etc/nv_tegra_release > /dev/null ; then
  83. status "Downloading JetPack 6 components"
  84. curl --fail --show-error --location --progress-bar \
  85. "https://ollama.com/download/ollama-linux-${ARCH}-jetpack6.tgz${VER_PARAM}" | \
  86. $SUDO tar -xzf - -C "$OLLAMA_INSTALL_DIR"
  87. elif grep R35 /etc/nv_tegra_release > /dev/null ; then
  88. status "Downloading JetPack 5 components"
  89. curl --fail --show-error --location --progress-bar \
  90. "https://ollama.com/download/ollama-linux-${ARCH}-jetpack5.tgz${VER_PARAM}" | \
  91. $SUDO tar -xzf - -C "$OLLAMA_INSTALL_DIR"
  92. else
  93. warning "Unsupported JetPack version detected. GPU may not be supported"
  94. fi
  95. fi
  96. install_success() {
  97. status 'The Ollama API is now available at 127.0.0.1:11434.'
  98. status 'Install complete. Run "ollama" from the command line.'
  99. }
  100. trap install_success EXIT
  101. # Everything from this point onwards is optional.
  102. configure_systemd() {
  103. if ! id ollama >/dev/null 2>&1; then
  104. status "Creating ollama user..."
  105. $SUDO useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama
  106. fi
  107. if getent group render >/dev/null 2>&1; then
  108. status "Adding ollama user to render group..."
  109. $SUDO usermod -a -G render ollama
  110. fi
  111. if getent group video >/dev/null 2>&1; then
  112. status "Adding ollama user to video group..."
  113. $SUDO usermod -a -G video ollama
  114. fi
  115. status "Adding current user to ollama group..."
  116. $SUDO usermod -a -G ollama $(whoami)
  117. status "Creating ollama systemd service..."
  118. cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
  119. [Unit]
  120. Description=Ollama Service
  121. After=network-online.target
  122. [Service]
  123. ExecStart=$BINDIR/ollama serve
  124. User=ollama
  125. Group=ollama
  126. Restart=always
  127. RestartSec=3
  128. Environment="PATH=$PATH"
  129. [Install]
  130. WantedBy=default.target
  131. EOF
  132. SYSTEMCTL_RUNNING="$(systemctl is-system-running || true)"
  133. case $SYSTEMCTL_RUNNING in
  134. running|degraded)
  135. status "Enabling and starting ollama service..."
  136. $SUDO systemctl daemon-reload
  137. $SUDO systemctl enable ollama
  138. start_service() { $SUDO systemctl restart ollama; }
  139. trap start_service EXIT
  140. ;;
  141. esac
  142. }
  143. if available systemctl; then
  144. configure_systemd
  145. fi
  146. # WSL2 only supports GPUs via nvidia passthrough
  147. # so check for nvidia-smi to determine if GPU is available
  148. if [ "$IS_WSL2" = true ]; then
  149. if available nvidia-smi && [ -n "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
  150. status "Nvidia GPU detected."
  151. fi
  152. install_success
  153. exit 0
  154. fi
  155. # Don't attempt to install drivers on Jetson systems
  156. if [ -f /etc/nv_tegra_release ] ; then
  157. status "NVIDIA JetPack ready."
  158. install_success
  159. exit 0
  160. fi
  161. # Install GPU dependencies on Linux
  162. if ! available lspci && ! available lshw; then
  163. warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
  164. exit 0
  165. fi
  166. check_gpu() {
  167. # Look for devices based on vendor ID for NVIDIA and AMD
  168. case $1 in
  169. lspci)
  170. case $2 in
  171. nvidia) available lspci && lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;;
  172. amdgpu) available lspci && lspci -d '1002:' | grep -q 'AMD' || return 1 ;;
  173. esac ;;
  174. lshw)
  175. case $2 in
  176. nvidia) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[10DE\]' || return 1 ;;
  177. amdgpu) available lshw && $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \[1002\]' || return 1 ;;
  178. esac ;;
  179. nvidia-smi) available nvidia-smi || return 1 ;;
  180. esac
  181. }
  182. if check_gpu nvidia-smi; then
  183. status "NVIDIA GPU installed."
  184. exit 0
  185. fi
  186. if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
  187. install_success
  188. warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode."
  189. exit 0
  190. fi
  191. if check_gpu lspci amdgpu || check_gpu lshw amdgpu; then
  192. if [ $BUNDLE -ne 0 ]; then
  193. status "Downloading Linux ROCm ${ARCH} bundle"
  194. curl --fail --show-error --location --progress-bar \
  195. "https://ollama.com/download/ollama-linux-${ARCH}-rocm.tgz${VER_PARAM}" | \
  196. $SUDO tar -xzf - -C "$OLLAMA_INSTALL_DIR"
  197. install_success
  198. status "AMD GPU ready."
  199. exit 0
  200. fi
  201. # Look for pre-existing ROCm v6 before downloading the dependencies
  202. for search in "${HIP_PATH:-''}" "${ROCM_PATH:-''}" "/opt/rocm" "/usr/lib64"; do
  203. if [ -n "${search}" ] && [ -e "${search}/libhipblas.so.2" -o -e "${search}/lib/libhipblas.so.2" ]; then
  204. status "Compatible AMD GPU ROCm library detected at ${search}"
  205. install_success
  206. exit 0
  207. fi
  208. done
  209. status "Downloading AMD GPU dependencies..."
  210. $SUDO rm -rf /usr/share/ollama/lib
  211. $SUDO chmod o+x /usr/share/ollama
  212. $SUDO install -o ollama -g ollama -m 755 -d /usr/share/ollama/lib/rocm
  213. curl --fail --show-error --location --progress-bar "https://ollama.com/download/ollama-linux-amd64-rocm.tgz${VER_PARAM}" \
  214. | $SUDO tar zx --owner ollama --group ollama -C /usr/share/ollama/lib/rocm .
  215. install_success
  216. status "AMD GPU ready."
  217. exit 0
  218. fi
  219. 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/"
  220. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
  221. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
  222. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
  223. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
  224. install_cuda_driver_yum() {
  225. status 'Installing NVIDIA repository...'
  226. case $PACKAGE_MANAGER in
  227. yum)
  228. $SUDO $PACKAGE_MANAGER -y install yum-utils
  229. 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
  230. $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
  231. else
  232. error $CUDA_REPO_ERR_MSG
  233. fi
  234. ;;
  235. dnf)
  236. 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
  237. $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
  238. else
  239. error $CUDA_REPO_ERR_MSG
  240. fi
  241. ;;
  242. esac
  243. case $1 in
  244. rhel)
  245. status 'Installing EPEL repository...'
  246. # EPEL is required for third-party dependencies such as dkms and libvdpau
  247. $SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
  248. ;;
  249. esac
  250. status 'Installing CUDA driver...'
  251. if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
  252. $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
  253. fi
  254. $SUDO $PACKAGE_MANAGER -y install cuda-drivers
  255. }
  256. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
  257. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
  258. install_cuda_driver_apt() {
  259. status 'Installing NVIDIA repository...'
  260. 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
  261. 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
  262. else
  263. error $CUDA_REPO_ERR_MSG
  264. fi
  265. case $1 in
  266. debian)
  267. status 'Enabling contrib sources...'
  268. $SUDO sed 's/main/contrib/' < /etc/apt/sources.list | $SUDO tee /etc/apt/sources.list.d/contrib.list > /dev/null
  269. if [ -f "/etc/apt/sources.list.d/debian.sources" ]; then
  270. $SUDO sed 's/main/contrib/' < /etc/apt/sources.list.d/debian.sources | $SUDO tee /etc/apt/sources.list.d/contrib.sources > /dev/null
  271. fi
  272. ;;
  273. esac
  274. status 'Installing CUDA driver...'
  275. $SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
  276. $SUDO apt-get update
  277. [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
  278. DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
  279. }
  280. if [ ! -f "/etc/os-release" ]; then
  281. error "Unknown distribution. Skipping CUDA installation."
  282. fi
  283. . /etc/os-release
  284. OS_NAME=$ID
  285. OS_VERSION=$VERSION_ID
  286. PACKAGE_MANAGER=
  287. for PACKAGE_MANAGER in dnf yum apt-get; do
  288. if available $PACKAGE_MANAGER; then
  289. break
  290. fi
  291. done
  292. if [ -z "$PACKAGE_MANAGER" ]; then
  293. error "Unknown package manager. Skipping CUDA installation."
  294. fi
  295. if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
  296. case $OS_NAME in
  297. centos|rhel) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -d '.' -f 1) ;;
  298. rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
  299. fedora) [ $OS_VERSION -lt '39' ] && install_cuda_driver_yum $OS_NAME $OS_VERSION || install_cuda_driver_yum $OS_NAME '39';;
  300. amzn) install_cuda_driver_yum 'fedora' '37' ;;
  301. debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
  302. ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
  303. *) exit ;;
  304. esac
  305. fi
  306. if ! lsmod | grep -q nvidia || ! lsmod | grep -q nvidia_uvm; then
  307. KERNEL_RELEASE="$(uname -r)"
  308. case $OS_NAME in
  309. rocky) $SUDO $PACKAGE_MANAGER -y install kernel-devel kernel-headers ;;
  310. centos|rhel|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
  311. fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;
  312. debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
  313. *) exit ;;
  314. esac
  315. NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
  316. if [ -n "$NVIDIA_CUDA_VERSION" ]; then
  317. $SUDO dkms install $NVIDIA_CUDA_VERSION
  318. fi
  319. if lsmod | grep -q nouveau; then
  320. status 'Reboot to complete NVIDIA CUDA driver install.'
  321. exit 0
  322. fi
  323. $SUDO modprobe nvidia
  324. $SUDO modprobe nvidia_uvm
  325. fi
  326. # make sure the NVIDIA modules are loaded on boot with nvidia-persistenced
  327. if available nvidia-persistenced; then
  328. $SUDO touch /etc/modules-load.d/nvidia.conf
  329. MODULES="nvidia nvidia-uvm"
  330. for MODULE in $MODULES; do
  331. if ! grep -qxF "$MODULE" /etc/modules-load.d/nvidia.conf; then
  332. echo "$MODULE" | $SUDO tee -a /etc/modules-load.d/nvidia.conf > /dev/null
  333. fi
  334. done
  335. fi
  336. status "NVIDIA GPU ready."
  337. install_success