install.sh 8.2 KB

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