install.sh 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. check_os() {
  6. if [ "$(uname -s)" != "Linux" ]; then
  7. echo "This script is intended to run on Linux only."
  8. exit 1
  9. fi
  10. }
  11. determine_architecture() {
  12. ARCH=$(uname -m)
  13. case $ARCH in
  14. x86_64)
  15. ARCH_SUFFIX="amd64"
  16. ;;
  17. aarch64|arm64)
  18. ARCH_SUFFIX="arm64"
  19. ;;
  20. *)
  21. echo "Unsupported architecture: $ARCH"
  22. exit 1
  23. ;;
  24. esac
  25. }
  26. check_sudo() {
  27. if [ "$(id -u)" -ne 0 ]; then
  28. if command -v sudo >/dev/null 2>&1; then
  29. SUDO_CMD="sudo"
  30. echo "Downloading the ollama executable to the PATH, this will require sudo permissions."
  31. else
  32. echo "Error: sudo is not available. Please run as root or install sudo."
  33. exit 1
  34. fi
  35. else
  36. SUDO_CMD=""
  37. fi
  38. }
  39. install_cuda_drivers() {
  40. local os_name os_version
  41. if [ -f "/etc/os-release" ]; then
  42. . /etc/os-release
  43. os_name=$ID
  44. os_version=$VERSION_ID
  45. else
  46. echo "Unable to detect operating system. Skipping CUDA installation."
  47. return 1
  48. fi
  49. # based on https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#package-manager-installation
  50. case $os_name in
  51. CentOS)
  52. $SUDO_CMD yum install yum-utils
  53. $SUDO_CMD yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
  54. $SUDO_CMD yum clean all
  55. $SUDO_CMD yum -y install nvidia-driver-latest-dkms
  56. $SUDO_CMD yum -y install cuda-driver
  57. $SUDO_CMD yum install kernel-devel-$(uname -r) kernel-headers-$(uname -r)
  58. $SUDO_CMD dkms status | awk -F: '/added/ { print $1 }' | xargs -n1 $SUDO_CMD dkms install
  59. $SUDO_CMD modprobe nvidia
  60. ;;
  61. ubuntu)
  62. case $os_version in
  63. 20.04)
  64. wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2004/x86_64/cuda-keyring_1.1-1_all.deb
  65. ;;
  66. 22.04)
  67. wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
  68. ;;
  69. *)
  70. echo "Skipping automatic CUDA installation, not supported for Ubuntu ($os_version)."
  71. return
  72. ;;
  73. esac
  74. $SUDO_CMD dpkg -i cuda-keyring_1.1-1_all.deb
  75. $SUDO_CMD apt-get update
  76. $SUDO_CMD apt-get -y install cuda-drivers
  77. ;;
  78. RedHatEnterprise*|Kylin|Fedora|SLES|openSUSE*|Microsoft|Debian)
  79. echo "NVIDIA CUDA drivers may not be installed, you can install them from: https://developer.nvidia.com/cuda-downloads"
  80. ;;
  81. *)
  82. echo "Unsupported or unknown distribution, skipping GPU CUDA driver install: $os_name"
  83. ;;
  84. esac
  85. }
  86. check_install_cuda_drivers() {
  87. if lspci -d '10de:' | grep 'NVIDIA' >/dev/null; then
  88. # NVIDIA Corporation [10de] device is available
  89. if command -v nvidia-smi >/dev/null 2>&1; then
  90. CUDA_VERSION=$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")
  91. if [ -z "$CUDA_VERSION" ]; then
  92. echo "Warning: NVIDIA-SMI is available, but the CUDA version cannot be detected. Installing CUDA drivers..."
  93. install_cuda_drivers
  94. else
  95. echo "Detected CUDA version $CUDA_VERSION"
  96. fi
  97. else
  98. echo "Warning: NVIDIA GPU detected but NVIDIA-SMI is not available. Installing CUDA drivers..."
  99. install_cuda_drivers
  100. fi
  101. else
  102. echo "No NVIDIA GPU detected. Skipping driver installation."
  103. fi
  104. }
  105. download_ollama() {
  106. $SUDO_CMD mkdir -p /usr/bin
  107. $SUDO_CMD curl -fsSL -o /usr/bin/ollama "https://ollama.ai/download/latest/ollama-linux-$ARCH_SUFFIX"
  108. }
  109. configure_systemd() {
  110. if command -v systemctl >/dev/null 2>&1; then
  111. $SUDO_CMD useradd -r -s /bin/false -m -d /home/ollama ollama 2>/dev/null
  112. echo "Creating systemd service file for ollama..."
  113. cat <<EOF | $SUDO_CMD tee /etc/systemd/system/ollama.service >/dev/null
  114. [Unit]
  115. Description=Ollama Service
  116. After=network-online.target
  117. [Service]
  118. ExecStart=/usr/bin/ollama serve
  119. User=ollama
  120. Group=ollama
  121. Restart=always
  122. RestartSec=3
  123. Environment="HOME=/home/ollama"
  124. [Install]
  125. WantedBy=default.target
  126. EOF
  127. echo "Reloading systemd and enabling ollama service..."
  128. if [ "$(systemctl is-system-running || echo 'not running')" = 'running' ]; then
  129. $SUDO_CMD systemctl daemon-reload
  130. $SUDO_CMD systemctl enable ollama
  131. $SUDO_CMD systemctl restart ollama
  132. fi
  133. else
  134. echo "Run 'ollama serve' from the command line to start the service."
  135. fi
  136. }
  137. main() {
  138. check_os
  139. determine_architecture
  140. check_sudo
  141. download_ollama
  142. configure_systemd
  143. check_install_cuda_drivers
  144. echo "Installation complete. You can now run 'ollama' from the command line."
  145. }
  146. main