install.sh 6.6 KB

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