{"id":805,"date":"2025-11-03T22:27:01","date_gmt":"2025-11-03T14:27:01","guid":{"rendered":"https:\/\/cqiot.cc\/?p=805"},"modified":"2025-11-03T22:27:01","modified_gmt":"2025-11-03T14:27:01","slug":"ollama%e5%9b%bd%e5%86%85linux%e7%b3%bb%e7%bb%9f%e5%ae%89%e8%a3%85%e8%84%9a%e6%9c%ac","status":"publish","type":"post","link":"https:\/\/cqiot.cc\/index.php\/2025\/11\/03\/ollama%e5%9b%bd%e5%86%85linux%e7%b3%bb%e7%bb%9f%e5%ae%89%e8%a3%85%e8%84%9a%e6%9c%ac\/","title":{"rendered":"ollama\u56fd\u5185LINUX\u7cfb\u7edf\u5b89\u88c5\u811a\u672c"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>#!\/bin\/sh\n# This script installs Ollama on Linux.\n# It detects the current operating system architecture and installs the appropriate version of Ollama.\n\n# Enable auto-export of variables to ensure proper function behavior\n# This resolves issues related to variable scope in some environments\nset -a\n\nset -eu\n\nred=\"$( (\/usr\/bin\/tput bold || :; \/usr\/bin\/tput setaf 1 || :) 2>&amp;-)\"\nplain=\"$( (\/usr\/bin\/tput sgr0 || :) 2>&amp;-)\"\n\nstatus() { echo \">>> $*\" >&amp;2; }\nerror() { echo \"${red}ERROR:${plain} $*\"; exit 1; }\nwarning() { echo \"${red}WARNING:${plain} $*\"; }\n\nTEMP_DIR=$(mktemp -d)\ncleanup() { rm -rf $TEMP_DIR; }\ntrap cleanup EXIT\n\navailable() { command -v $1 >\/dev\/null; }\nrequire() {\n    local MISSING=''\n    for TOOL in $*; do\n        if ! available $TOOL; then\n            MISSING=\"$MISSING $TOOL\"\n        fi\n    done\n\n    echo $MISSING\n}\n\n# \u6539\u8fdb\u7684\u4ee3\u7406\u4e0b\u8f7d\u51fd\u6570\n# \u5982\u679c\u4e0b\u8f7d\u5931\u8d25\uff0c\u53ef\u5c1d\u8bd5\u66ff\u6362 https:\/\/ghfast.top \u4e3a https:\/\/gh-proxy.com\uff0c\u6216\u5176\u4ed6\u7c7b\u4f3c github \u52a0\u901f\u670d\u52a1\ndownload_with_proxy() {\n    local url=\"$1\"\n    local output_file=\"$2\"\n    local github_url=$(curl -s -I \"$url\" | grep -i Location | awk '{print $2}' | tr -d '\\r')\n    local proxy_url=\"https:\/\/gh-proxy.com\/$github_url\"\n    local max_retries=3\n    local retry_count=0\n\n    while &#91; $retry_count -lt $max_retries ]; do\n        if curl -L --http1.1 \"$proxy_url\" -o \"$output_file\"; then\n            return 0\n        else\n            retry_count=$((retry_count + 1))\n            echo \"Download failed. Retrying in 5 seconds... (Attempt $retry_count of $max_retries)\"\n            sleep 5\n        fi\n    done\n\n    echo \"Download failed after $max_retries attempts.\"\n    return 1\n}\n\n&#91; \"$(uname -s)\" = \"Linux\" ] || error 'This script is intended to run on Linux only.'\n\nARCH=$(uname -m)\ncase \"$ARCH\" in\n    x86_64) ARCH=\"amd64\" ;;\n    aarch64|arm64) ARCH=\"arm64\" ;;\n    *) error \"Unsupported architecture: $ARCH\" ;;\nesac\n\nIS_WSL2=false\n\nKERN=$(uname -r)\ncase \"$KERN\" in\n    *icrosoft*WSL2 | *icrosoft*wsl2) IS_WSL2=true;;\n    *icrosoft) error \"Microsoft WSL1 is not currently supported. Please use WSL2 with 'wsl --set-version &lt;distro> 2'\" ;;\n    *) ;;\nesac\n\nVER_PARAM=\"${OLLAMA_VERSION:+?version=$OLLAMA_VERSION}\"\n\nSUDO=\nif &#91; \"$(id -u)\" -ne 0 ]; then\n    # Running as root, no need for sudo\n    if ! available sudo; then\n        error \"This script requires superuser permissions. Please re-run as root.\"\n    fi\n\n    SUDO=\"sudo\"\nfi\n\nNEEDS=$(require curl awk grep sed tee xargs)\nif &#91; -n \"$NEEDS\" ]; then\n    status \"ERROR: The following tools are required but missing:\"\n    for NEED in $NEEDS; do\n        echo \"  - $NEED\"\n    done\n    exit 1\nfi\n\nfor BINDIR in \/usr\/local\/bin \/usr\/bin \/bin; do\n    echo $PATH | grep -q $BINDIR &amp;&amp; break || continue\ndone\nOLLAMA_INSTALL_DIR=$(dirname ${BINDIR})\n\nif &#91; -d \"$OLLAMA_INSTALL_DIR\/lib\/ollama\" ] ; then\n    status \"Cleaning up old version at $OLLAMA_INSTALL_DIR\/lib\/ollama\"\n    $SUDO rm -rf \"$OLLAMA_INSTALL_DIR\/lib\/ollama\"\nfi\n\nstatus \"Installing ollama to $OLLAMA_INSTALL_DIR\"\n$SUDO install -o0 -g0 -m755 -d $BINDIR\n$SUDO install -o0 -g0 -m755 -d \"$OLLAMA_INSTALL_DIR\/lib\/ollama\"\n\nstatus \"Downloading Linux ${ARCH} bundle\"\nif download_with_proxy \"https:\/\/ollama.com\/download\/ollama-linux-${ARCH}.tgz${VER_PARAM}\" \"$TEMP_DIR\/ollama.tgz\"; then\n    $SUDO tar -xzf \"$TEMP_DIR\/ollama.tgz\" -C \"$OLLAMA_INSTALL_DIR\"\n    \n    if &#91; \"$OLLAMA_INSTALL_DIR\/bin\/ollama\" != \"$BINDIR\/ollama\" ] ; then\n        status \"Making ollama accessible in the PATH in $BINDIR\"\n        $SUDO ln -sf \"$OLLAMA_INSTALL_DIR\/ollama\" \"$BINDIR\/ollama\"\n    fi\nelse\n    error \"Failed to download Ollama bundle\"\nfi\n\n# Check for NVIDIA JetPack systems with additional downloads\nif &#91; -f \/etc\/nv_tegra_release ] ; then\n    if grep R36 \/etc\/nv_tegra_release > \/dev\/null ; then\n        status \"Downloading JetPack 6 components\"\n        if download_with_proxy \"https:\/\/ollama.com\/download\/ollama-linux-${ARCH}-jetpack6.tgz${VER_PARAM}\" \"$TEMP_DIR\/ollama-jetpack.tgz\"; then\n            $SUDO tar -xzf \"$TEMP_DIR\/ollama-jetpack.tgz\" -C \"$OLLAMA_INSTALL_DIR\"\n        else\n            error \"Failed to download JetPack 6 components\"\n        fi\n    elif grep R35 \/etc\/nv_tegra_release > \/dev\/null ; then\n        status \"Downloading JetPack 5 components\"\n        if download_with_proxy \"https:\/\/ollama.com\/download\/ollama-linux-${ARCH}-jetpack5.tgz${VER_PARAM}\" \"$TEMP_DIR\/ollama-jetpack.tgz\"; then\n            $SUDO tar -xzf \"$TEMP_DIR\/ollama-jetpack.tgz\" -C \"$OLLAMA_INSTALL_DIR\"\n        else\n            error \"Failed to download JetPack 5 components\"\n        fi\n    else\n        warning \"Unsupported JetPack version detected. GPU may not be supported\"\n    fi\nfi\n\ninstall_success() {\n    status 'The Ollama API is now available at 127.0.0.1:11434.'\n    status 'Install complete. Run \"ollama\" from the command line.'\n}\ntrap install_success EXIT\n\nstart_ollama() {\n    if &#91; -f \"$BINDIR\/ollama\" ]; then\n        status \"Starting Ollama...\"\n        nohup $BINDIR\/ollama serve > \/var\/log\/ollama.log 2>&amp;1 &amp;\n        PID=$!\n        status \"Ollama started with PID $PID\"\n    else\n        error \"Ollama binary not found at $BINDIR\/ollama\"\n    fi\n}\n\n# Since we're in a container, skip systemd setup and directly start ollama\nstatus \"systemd not available in container. Starting Ollama directly...\"\nstart_ollama\n\n# WSL2 only supports GPUs via nvidia passthrough\n# so check for nvidia-smi to determine if GPU is available\nif &#91; \"$IS_WSL2\" = true ]; then\n    if available nvidia-smi &amp;&amp; &#91; -n \"$(nvidia-smi | grep -o \"CUDA Version: &#91;0-9]*\\.&#91;0-9]*\")\" ]; then\n        status \"Nvidia GPU detected.\"\n    fi\n    install_success\n    exit 0\nfi\n\n# Don't attempt to install drivers on Jetson systems\nif &#91; -f \/etc\/nv_tegra_release ] ; then\n    status \"NVIDIA JetPack ready.\"\n    install_success\n    exit 0\nfi\n\n# Install GPU dependencies on Linux\nif ! available lspci &amp;&amp; ! available lshw; then\n    warning \"Unable to detect NVIDIA\/AMD GPU. Install lspci or lshw to automatically detect and install GPU dependencies.\"\n    exit 0\nfi\n\ncheck_gpu() {\n    # Look for devices based on vendor ID for NVIDIA and AMD\n    case $1 in\n        lspci)\n            case $2 in\n                nvidia) available lspci &amp;&amp; lspci -d '10de:' | grep -q 'NVIDIA' || return 1 ;;\n                amdgpu) available lspci &amp;&amp; lspci -d '1002:' | grep -q 'AMD' || return 1 ;;\n            esac ;;\n        lshw)\n            case $2 in\n                nvidia) available lshw &amp;&amp; $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \\&#91;10DE\\]' || return 1 ;;\n                amdgpu) available lshw &amp;&amp; $SUDO lshw -c display -numeric -disable network | grep -q 'vendor: .* \\&#91;1002\\]' || return 1 ;;\n            esac ;;\n        nvidia-smi) available nvidia-smi || return 1 ;;\n    esac\n}\n\nif check_gpu nvidia-smi; then\n    status \"NVIDIA GPU installed.\"\n    exit 0\nfi\n\nif ! check_gpu lspci nvidia &amp;&amp; ! check_gpu lshw nvidia &amp;&amp; ! check_gpu lspci amdgpu &amp;&amp; ! check_gpu lshw amdgpu; then\n    install_success\n    warning \"No NVIDIA\/AMD GPU detected. Ollama will run in CPU-only mode.\"\n    exit 0\nfi\n\nif check_gpu lspci amdgpu || check_gpu lshw amdgpu; then\n    status \"Downloading Linux ROCm ${ARCH} bundle\"\n    if download_with_proxy \"https:\/\/ollama.com\/download\/ollama-linux-${ARCH}-rocm.tgz${VER_PARAM}\" \"$TEMP_DIR\/ollama-rocm.tgz\"; then\n        $SUDO tar -xzf \"$TEMP_DIR\/ollama-rocm.tgz\" -C \"$OLLAMA_INSTALL_DIR\"\n        \n        install_success\n        status \"AMD GPU ready.\"\n        exit 0\n    else\n        error \"Failed to download AMD GPU bundle\"\n    fi\nfi\n\nCUDA_REPO_ERR_MSG=\"NVIDIA GPU detected, but your OS and Architecture are not supported by NVIDIA. Please install the CUDA driver manually https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/\"\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#rhel-7-centos-7\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#rhel-8-rocky-8\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#rhel-9-rocky-9\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#fedora\ninstall_cuda_driver_yum() {\n    status 'Installing NVIDIA repository...'\n    \n    case $PACKAGE_MANAGER in\n        yum)\n            $SUDO $PACKAGE_MANAGER -y install yum-utils\n            if curl -I --silent --fail --location \"https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-$1$2.repo\" >\/dev\/null ; then\n                $SUDO $PACKAGE_MANAGER-config-manager --add-repo https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-$1$2.repo\n            else\n                error $CUDA_REPO_ERR_MSG\n            fi\n            ;;\n        dnf)\n            if curl -I --silent --fail --location \"https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-$1$2.repo\" >\/dev\/null ; then\n                $SUDO $PACKAGE_MANAGER config-manager --add-repo https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-$1$2.repo\n            else\n                error $CUDA_REPO_ERR_MSG\n            fi\n            ;;\n    esac\n\n    case $1 in\n        rhel)\n            status 'Installing EPEL repository...'\n            # EPEL is required for third-party dependencies such as dkms and libvdpau\n            $SUDO $PACKAGE_MANAGER -y install https:\/\/dl.fedoraproject.org\/pub\/epel\/epel-release-latest-$2.noarch.rpm || true\n            ;;\n    esac\n\n    status 'Installing CUDA driver...'\n\n    if &#91; \"$1\" = 'centos' ] || &#91; \"$1$2\" = 'rhel7' ]; then\n        $SUDO $PACKAGE_MANAGER -y install nvidia-driver-latest-dkms\n    fi\n\n    $SUDO $PACKAGE_MANAGER -y install cuda-drivers\n}\n\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#ubuntu\n# ref: https:\/\/docs.nvidia.com\/cuda\/cuda-installation-guide-linux\/index.html#debian\ninstall_cuda_driver_apt() {\n    status 'Installing NVIDIA repository...'\n    if curl -I --silent --fail --location \"https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-keyring_1.1-1_all.deb\" >\/dev\/null ; then\n        curl -fsSL -o $TEMP_DIR\/cuda-keyring.deb https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$1$2\/$(uname -m | sed -e 's\/aarch64\/sbsa\/')\/cuda-keyring_1.1-1_all.deb\n    else\n        error $CUDA_REPO_ERR_MSG\n    fi\n\n    case $1 in\n        debian)\n            status 'Enabling contrib sources...'\n            $SUDO sed 's\/main\/contrib\/' &lt; \/etc\/apt\/sources.list | $SUDO tee \/etc\/apt\/sources.list.d\/contrib.list > \/dev\/null\n            if &#91; -f \"\/etc\/apt\/sources.list.d\/debian.sources\" ]; then\n                $SUDO sed 's\/main\/contrib\/' &lt; \/etc\/apt\/sources.list.d\/debian.sources | $SUDO tee \/etc\/apt\/sources.list.d\/contrib.sources > \/dev\/null\n            fi\n            ;;\n    esac\n\n    status 'Installing CUDA driver...'\n    $SUDO dpkg -i $TEMP_DIR\/cuda-keyring.deb\n    $SUDO apt-get update\n\n    &#91; -n \"$SUDO\" ] &amp;&amp; SUDO_E=\"$SUDO -E\" || SUDO_E=\n    DEBIAN_FRONTEND=noninteractive $SUDO_E apt-get -y install cuda-drivers -q\n}\n\nif &#91; ! -f \"\/etc\/os-release\" ]; then\n    error \"Unknown distribution. Skipping CUDA installation.\"\nfi\n\n. \/etc\/os-release\n\nOS_NAME=$ID\nOS_VERSION=$VERSION_ID\n\nPACKAGE_MANAGER=\nfor PACKAGE_MANAGER in dnf yum apt-get; do\n    if available $PACKAGE_MANAGER; then\n        break\n    fi\ndone\n\nif &#91; -z \"$PACKAGE_MANAGER\" ]; then\n    error \"Unknown package manager. Skipping CUDA installation.\"\nfi\n\nif ! check_gpu nvidia-smi || &#91; -z \"$(nvidia-smi | grep -o \"CUDA Version: &#91;0-9]*\\.&#91;0-9]*\")\" ]; then\n    case $OS_NAME in\n        centos|rhel) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -d '.' -f 1) ;;\n        rocky) install_cuda_driver_yum 'rhel' $(echo $OS_VERSION | cut -c1) ;;\n        fedora) &#91; $OS_VERSION -lt '39' ] &amp;&amp; install_cuda_driver_yum $OS_NAME $OS_VERSION || install_cuda_driver_yum $OS_NAME '39';;\n        amzn) install_cuda_driver_yum 'fedora' '37' ;;\n        debian) install_cuda_driver_apt $OS_NAME $OS_VERSION ;;\n        ubuntu) install_cuda_driver_apt $OS_NAME $(echo $OS_VERSION | sed 's\/\\.\/\/') ;;\n        *) exit ;;\n    esac\nfi\n\nif ! lsmod | grep -q nvidia || ! lsmod | grep -q nvidia_uvm; then\n    KERNEL_RELEASE=\"$(uname -r)\"\n    case $OS_NAME in\n        rocky) $SUDO $PACKAGE_MANAGER -y install kernel-devel kernel-headers ;;\n        centos|rhel|amzn) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE kernel-headers-$KERNEL_RELEASE ;;\n        fedora) $SUDO $PACKAGE_MANAGER -y install kernel-devel-$KERNEL_RELEASE ;;\n        debian|ubuntu) $SUDO apt-get -y install linux-headers-$KERNEL_RELEASE ;;\n        *) exit ;;\n    esac\n\n    NVIDIA_CUDA_VERSION=$($SUDO dkms status | awk -F: '\/added\/ { print $1 }')\n    if &#91; -n \"$NVIDIA_CUDA_VERSION\" ]; then\n        $SUDO dkms install $NVIDIA_CUDA_VERSION\n    fi\n\n    if lsmod | grep -q nouveau; then\n        status 'Reboot to complete NVIDIA CUDA driver install.'\n        exit 0\n    fi\n\n    $SUDO modprobe nvidia\n    $SUDO modprobe nvidia_uvm\nfi\n\n# make sure the NVIDIA modules are loaded on boot with nvidia-persistenced\nif available nvidia-persistenced; then\n    $SUDO touch \/etc\/modules-load.d\/nvidia.conf\n    MODULES=\"nvidia nvidia-uvm\"\n    for MODULE in $MODULES; do\n        if ! grep -qxF \"$MODULE\" \/etc\/modules-load.d\/nvidia.conf; then\n            echo \"$MODULE\" | $SUDO tee -a \/etc\/modules-load.d\/nvidia.conf > \/dev\/null\n        fi\n    done\nfi\n\nstatus \"NVIDIA GPU ready.\"\ninstall_success<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-805","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/posts\/805","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/comments?post=805"}],"version-history":[{"count":1,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/posts\/805\/revisions"}],"predecessor-version":[{"id":806,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/posts\/805\/revisions\/806"}],"wp:attachment":[{"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/media?parent=805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/categories?post=805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cqiot.cc\/index.php\/wp-json\/wp\/v2\/tags?post=805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}