install.sh 6.2 KB

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