install.sh 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  41. if [ -f "/etc/os-release" ]; then
  42. . /etc/os-release
  43. os_name=$ID
  44. else
  45. echo "Unable to detect operating system. Skipping CUDA installation."
  46. return 1
  47. fi
  48. # based on https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#package-manager-installation
  49. case $os_name in
  50. CentOS)
  51. $SUDO_CMD yum install yum-utils
  52. $SUDO_CMD yum-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64/cuda-rhel7.repo
  53. $SUDO_CMD yum clean all
  54. $SUDO_CMD yum -y install nvidia-driver-latest-dkms
  55. $SUDO_CMD yum -y install cuda-driver
  56. $SUDO_CMD yum install kernel-devel-$(uname -r) kernel-headers-$(uname -r)
  57. $SUDO_CMD dkms status | awk -F: '/added/ { print $1 }' | xargs -n1 $SUDO_CMD dkms install
  58. $SUDO_CMD modprobe nvidia
  59. ;;
  60. RedHatEnterprise*|Kylin|Fedora|SLES|openSUSE*|Microsoft|Ubuntu|Debian)
  61. echo "NVIDIA CUDA drivers may not be installed, you can install them from: https://developer.nvidia.com/cuda-downloads"
  62. ;;
  63. *)
  64. echo "Unsupported or unknown distribution, skipping GPU CUDA driver install: $os_name"
  65. ;;
  66. esac
  67. }
  68. check_install_cuda_drivers() {
  69. if lspci -d '10de:' | grep 'NVIDIA' >/dev/null; then
  70. # NVIDIA Corporation [10de] device is available
  71. if command -v nvidia-smi >/dev/null 2>&1; then
  72. CUDA_VERSION=$(nvidia-smi | grep -o "CUDA Version: [0-9]*\.[0-9]*")
  73. if [ -z "$CUDA_VERSION" ]; then
  74. echo "Warning: NVIDIA-SMI is available, but the CUDA version cannot be detected. Installing CUDA drivers..."
  75. install_cuda_drivers
  76. else
  77. echo "Detected CUDA version $CUDA_VERSION"
  78. fi
  79. else
  80. echo "Warning: NVIDIA GPU detected but NVIDIA-SMI is not available. Installing CUDA drivers..."
  81. install_cuda_drivers
  82. fi
  83. else
  84. echo "No NVIDIA GPU detected. Skipping driver installation."
  85. fi
  86. }
  87. download_ollama() {
  88. $SUDO_CMD mkdir -p /usr/bin
  89. $SUDO_CMD curl -fsSL -o /usr/bin/ollama "https://ollama.ai/download/latest/ollama-linux-$ARCH_SUFFIX"
  90. }
  91. configure_systemd() {
  92. if command -v systemctl >/dev/null 2>&1; then
  93. $SUDO_CMD useradd -r -s /bin/false -m -d /home/ollama ollama 2>/dev/null
  94. echo "Creating systemd service file for ollama..."
  95. cat <<EOF | $SUDO_CMD tee /etc/systemd/system/ollama.service >/dev/null
  96. [Unit]
  97. Description=Ollama Service
  98. After=network-online.target
  99. [Service]
  100. ExecStart=/usr/bin/ollama serve
  101. User=ollama
  102. Group=ollama
  103. Restart=always
  104. RestartSec=3
  105. Environment="HOME=/home/ollama"
  106. [Install]
  107. WantedBy=default.target
  108. EOF
  109. echo "Reloading systemd and enabling ollama service..."
  110. if [ "$(systemctl is-system-running || echo 'not running')" = 'running' ]; then
  111. $SUDO_CMD systemctl daemon-reload
  112. $SUDO_CMD systemctl enable ollama
  113. $SUDO_CMD systemctl restart ollama
  114. fi
  115. else
  116. echo "Run 'ollama serve' from the command line to start the service."
  117. fi
  118. }
  119. main() {
  120. check_os
  121. determine_architecture
  122. check_sudo
  123. download_ollama
  124. configure_systemd
  125. check_install_cuda_drivers
  126. echo "Installation complete. You can now run 'ollama' from the command line."
  127. }
  128. main