install.sh 7.2 KB

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