install.sh 10 KB

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