install.sh 6.2 KB

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