install.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. $SUDO $PACKAGE_MANAGER -y update
  118. if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
  119. $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms
  120. fi
  121. $SUDO $PACKAGE_MANAGER -y install cuda-drivers
  122. }
  123. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
  124. # ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
  125. install_cuda_driver_apt() {
  126. status 'Installing NVIDIA repository...'
  127. 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
  128. case $1 in
  129. debian)
  130. status 'Enabling contrib sources...'
  131. $SUDO sed 's/main/contrib/' < /etc/apt/sources.list | sudo tee /etc/apt/sources.list.d/contrib.list > /dev/null
  132. ;;
  133. esac
  134. status 'Installing CUDA driver...'
  135. $SUDO dpkg -i $TEMP_DIR/cuda-keyring.deb
  136. $SUDO apt-get update
  137. [ -n "$SUDO" ] && SUDO_E="$SUDO -E" || SUDO_E=
  138. DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q
  139. }
  140. if [ ! -f "/etc/os-release" ]; then
  141. error "Unknown distribution. Skipping CUDA installation."
  142. fi
  143. . /etc/os-release
  144. OS_NAME=$ID
  145. OS_VERSION=$VERSION_ID
  146. PACKAGE_MANAGER=
  147. for PACKAGE_MANAGER in dnf yum apt-get; do
  148. if command -v $PACKAGE_MANAGER >/dev/null; then
  149. break
  150. fi
  151. done
  152. if [ -z "$PACKAGE_MANAGER" ]; then
  153. error "Unknown package manager. Skipping CUDA installation."
  154. fi
  155. if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")" ]; then
  156. case $OS_NAME in
  157. centos|rhel) install_cuda_driver_yum 'rhel' $OS_VERSION ;;
  158. rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;
  159. fedora) install_cuda_driver_dnf $OS_NAME $OS_VERSION ;;
  160. debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;
  161. ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's/\.//') ;;
  162. esac
  163. fi
  164. if ! lsmod | grep -q nvidia; then
  165. KERNEL_RELEASE="$(uname -r)"
  166. case $OS_NAME in
  167. centos|rhel|rocky|fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;
  168. debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;
  169. esac
  170. $SUDO dkms status | awk -F: '/added/ { print $1 }' | xargs -n1 $SUDO dkms install
  171. $SUDO modprobe nvidia
  172. fi