install.sh 7.7 KB

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