install.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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 upgrade to 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. status "Downloading ollama..."
  53. curl --fail --show-error --location --progress-bar -o $TEMP_DIR/ollama "https://ollama.com/download/ollama-linux-${ARCH}${VER_PARAM}"
  54. for BINDIR in /usr/local/bin /usr/bin /bin; do
  55. echo $PATH | grep -q $BINDIR && break || continue
  56. done
  57. status "Installing ollama to $BINDIR..."
  58. $SUDO install -o0 -g0 -m755 -d $BINDIR
  59. $SUDO install -o0 -g0 -m755 $TEMP_DIR/ollama $BINDIR/ollama
  60. install_success() {
  61. status 'The Ollama API is now available at 127.0.0.1:11434.'
  62. status 'Install complete. Run "ollama" from the command line.'
  63. }
  64. trap install_success EXIT
  65. # Everything from this point onwards is optional.
  66. configure_systemd() {
  67. if ! id ollama >/dev/null 2>&1; then
  68. status "Creating ollama user..."
  69. $SUDO useradd -r -s /bin/false -U -m -d /usr/share/ollama ollama
  70. fi
  71. if getent group render >/dev/null 2>&1; then
  72. status "Adding ollama user to render group..."
  73. $SUDO usermod -a -G render ollama
  74. fi
  75. if getent group video >/dev/null 2>&1; then
  76. status "Adding ollama user to video group..."
  77. $SUDO usermod -a -G video ollama
  78. fi
  79. status "Adding current user to ollama group..."
  80. $SUDO usermod -a -G ollama $(whoami)
  81. status "Creating ollama systemd service..."
  82. cat <<EOF | $SUDO tee /etc/systemd/system/ollama.service >/dev/null
  83. [Unit]
  84. Description=Ollama Service
  85. After=network-online.target
  86. [Service]
  87. ExecStart=$BINDIR/ollama serve
  88. User=ollama
  89. Group=ollama
  90. Restart=always
  91. RestartSec=3
  92. Environment="PATH=$PATH"
  93. [Install]
  94. WantedBy=default.target
  95. EOF
  96. SYSTEMCTL_RUNNING="$(systemctl is-system-running || true)"
  97. case $SYSTEMCTL_RUNNING in
  98. running|degraded)
  99. status "Enabling and starting ollama service..."
  100. $SUDO systemctl daemon-reload
  101. $SUDO systemctl enable ollama
  102. start_service() { $SUDO systemctl restart ollama; }
  103. trap start_service EXIT
  104. ;;
  105. esac
  106. }
  107. if available systemctl; then
  108. configure_systemd
  109. fi
  110. # WSL2 only supports GPUs via nvidia passthrough
  111. # so check for nvidia-smi to determine if GPU is available
  112. if [ "$IS_WSL2" = true ]; then
  113. if available nvidia-smi && [ -n "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
  114. status "Nvidia GPU detected."
  115. fi
  116. install_success
  117. exit 0
  118. fi
  119. # Install GPU dependencies on Linux
  120. if ! available lspci && ! available lshw; then
  121. warning "Unable to detect NVIDIA/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies."
  122. exit 0
  123. fi
  124. check_gpu() {
  125. # Look for devices based on vendor ID for NVIDIA and AMD
  126. case $1 in
  127. lspci)
  128. case $2 in
  129. nvidia) available lspci && lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;;
  130. amdgpu) available lspci && lspci -d '1002:' | grep -q 'AMD' || return 1 ;;
  131. esac ;;
  132. lshw)
  133. case $2 in
  134. nvidia) available lshw && $SUDO lshw -c display -numeric | grep -q 'vendor: .* \[10DE\]' || return 1 ;;
  135. amdgpu) available lshw && $SUDO lshw -c display -numeric | grep -q 'vendor: .* \[1002\]' || return 1 ;;
  136. esac ;;
  137. nvidia-smi) available nvidia-smi || return 1 ;;
  138. esac
  139. }
  140. if check_gpu nvidia-smi; then
  141. status "NVIDIA GPU installed."
  142. exit 0
  143. fi
  144. if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
  145. install_success
  146. warning "No NVIDIA/AMD GPU detected. Ollama will run in CPU-only mode."
  147. exit 0
  148. fi
  149. if check_gpu lspci amdgpu || check_gpu lshw amdgpu; then
  150. # Look for pre-existing ROCm v6 before downloading the dependencies
  151. for search in "${HIP_PATH:-''}" "${ROCM_PATH:-''}" "/opt/rocm" "/usr/lib64"; do
  152. if [ -n "${search}" ] && [ -e "${search}/libhipblas.so.2" -o -e "${search}/lib/libhipblas.so.2" ]; then
  153. status "Compatible AMD GPU ROCm library detected at ${search}"
  154. install_success
  155. exit 0
  156. fi
  157. done
  158. status "Downloading AMD GPU dependencies..."
  159. $SUDO rm -rf /usr/share/ollama/lib
  160. $SUDO chmod o+x /usr/share/ollama
  161. $SUDO install -o ollama -g ollama -m 755 -d /usr/share/ollama/lib/rocm
  162. curl --fail --show-error --location --progress-bar "https://ollama.com/download/ollama-linux-amd64-rocm.tgz${VER_PARAM}" \
  163. | $SUDO tar zx --owner ollama --group ollama -C /usr/share/ollama/lib/rocm .
  164. install_success
  165. status "AMD GPU ready."
  166. exit 0
  167. fi
  168. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
  169. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
  170. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
  171. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
  172. install_cuda_driver_yum() {
  173. status 'Installing NVIDIA repository...'
  174. case $PACKAGE_MANAGER in
  175. yum)
  176. $SUDO $PACKAGE_MANAGER -y install yum-utils
  177. $SUDO $PACKAGE_MANAGER-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
  178. ;;
  179. dnf)
  180. $SUDO $PACKAGE_MANAGER config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
  181. ;;
  182. esac
  183. case $1 in
  184. rhel)
  185. status 'Installing EPEL repository...'
  186. # EPEL is required for third-party dependencies such as dkms and libvdpau
  187. $SUDO $PACKAGE_MANAGER -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
  188. ;;
  189. esac
  190. status 'Installing CUDA driver...'
  191. if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
  192. $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
  193. fi
  194. $SUDO $PACKAGE_MANAGER -y install cuda-drivers
  195. }
  196. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
  197. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
  198. install_cuda_driver_apt() {
  199. status 'Installing NVIDIA repository...'
  200. 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
  201. case $1 in
  202. debian)
  203. status 'Enabling contrib sources...'
  204. $SUDO sed 's/main/contrib/' < /etc/apt/sources.list | $SUDO tee /etc/apt/sources.list.d/contrib.list > /dev/null
  205. if [ -f "/etc/apt/sources.list.d/debian.sources" ]; then
  206. $SUDO sed 's/main/contrib/' < /etc/apt/sources.list.d/debian.sources | $SUDO tee /etc/apt/sources.list.d/contrib.sources > /dev/null
  207. fi
  208. ;;
  209. esac
  210. status 'Installing CUDA driver...'
  211. $SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
  212. $SUDO apt-get update
  213. [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
  214. DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
  215. }
  216. if [ ! -f "/etc/os-release" ]; then
  217. error "Unknown distribution. Skipping CUDA installation."
  218. fi
  219. . /etc/os-release
  220. OS_NAME=$ID
  221. OS_VERSION=$VERSION_ID
  222. PACKAGE_MANAGER=
  223. for PACKAGE_MANAGER in dnf yum apt-get; do
  224. if available $PACKAGE_MANAGER; then
  225. break
  226. fi
  227. done
  228. if [ -z "$PACKAGE_MANAGER" ]; then
  229. error "Unknown package manager. Skipping CUDA installation."
  230. fi
  231. if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
  232. case $OS_NAME in
  233. centos|rhel) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -d '.' -f 1) ;;
  234. rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
  235. fedora) [ $OS_VERSION -lt '37' ] && install_cuda_driver_yum $OS_NAME $OS_VERSION || install_cuda_driver_yum $OS_NAME '37';;
  236. amzn) install_cuda_driver_yum 'fedora' '37' ;;
  237. debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
  238. ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
  239. *) exit ;;
  240. esac
  241. fi
  242. if ! lsmod | grep -q nvidia || ! lsmod | grep -q nvidia_uvm; then
  243. KERNEL_RELEASE="$(uname -r)"
  244. case $OS_NAME in
  245. rocky) $SUDO $PACKAGE_MANAGER -y install kernel-devel kernel-headers ;;
  246. centos|rhel|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
  247. fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;
  248. debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
  249. *) exit ;;
  250. esac
  251. NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '/added/ { print $1 }')
  252. if [ -n "$NVIDIA_CUDA_VERSION" ]; then
  253. $SUDO dkms install $NVIDIA_CUDA_VERSION
  254. fi
  255. if lsmod | grep -q nouveau; then
  256. status 'Reboot to complete NVIDIA CUDA driver install.'
  257. exit 0
  258. fi
  259. $SUDO modprobe nvidia
  260. $SUDO modprobe nvidia_uvm
  261. fi
  262. # make sure the NVIDIA modules are loaded on boot with nvidia-persistenced
  263. if command -v nvidia-persistenced > /dev/null 2>&1; then
  264. $SUDO touch /etc/modules-load.d/nvidia.conf
  265. MODULES="nvidia nvidia-uvm"
  266. for MODULE in $MODULES; do
  267. if ! grep -qxF "$MODULE" /etc/modules-load.d/nvidia.conf; then
  268. echo "$MODULE" | sudo tee -a /etc/modules-load.d/nvidia.conf > /dev/null
  269. fi
  270. done
  271. fi
  272. status "NVIDIA GPU ready."
  273. install_success