install.sh 7.9 KB

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