当前位置: 代码网 > 服务器>服务器>Linux > Linux内网离线安装Ollama

Linux内网离线安装Ollama

2024年08月02日 Linux 我要评论
在线安装地址https://ollama.com/download选择服务器系统,按照步骤完成安装。下载地址 https://github.com/ollama/ollama/releases/总共需要修改两个点,第一:Ollama下载地址;第二:Ollama安装包存放目录。我下载的是Ollama的v0.1.31版本,后面均以此版本为例说明。修改后的install.sh文件,v0.1.31版本。第二处修改,修改ollama安装目录。第一处修改,注释下载链接。

1、在线安装

在线安装地址https://ollama.com/download选择服务器系统,按照步骤完成安装

2、离线安装

步骤1:查看服务器cpu的型号
## 查看linux系统cpu型号命令,我的服务器cpu型号是x86_64
lscpu

在这里插入图片描述

步骤2:根据cpu型号下载ollama安装包,并保存到/home/ollama目录

下载地址 https://github.com/ollama/ollama/releases/

# x86_64 cpu选择下载ollama-linux-amd64
# aarch64|arm64 cpu选择下载ollama-linux-arm64

在这里插入图片描述

步骤3:离线下载linux环境的ollama安装脚本,并保存到/home/ollama目录
## 下载地址1,浏览器中打开下面地址
https://ollama.com/install.sh

## 下载地址2
https://github.com/ollama/ollama/blob/main/scripts/install.sh
## 国内加速可访问https://gitee.com/ai-big-model/ollama/blob/main/scripts/install.sh
步骤4:修改install.sh脚本

总共需要修改两个点,第一:ollama下载地址;第二:ollama安装包存放目录

第一处修改,注释下载链接

status "downloading ollama..."
## 在install.sh的第65行
#curl --fail --show-error --location --progress-bar -o $temp_dir/ollama "https://ollama.com/download/ollama-linux-${arch}${ver_param}"

第二处修改,修改ollama安装目录

status "installing ollama to $bindir..."
$sudo install -o0 -g0 -m755 -d $bindir
## 在install.sh的第73行
#$sudo install -o0 -g0 -m755 $temp_dir/ollama $bindir/ollama
$sudo install -o0 -g0 -m755 ./ollama-linux-amd64  $bindir/ollama

修改后的install.sh文件,v0.1.31版本

#!/bin/sh
# this script installs ollama on linux.
# it detects the current operating system architecture and installs the appropriate version of ollama.

set -eu

status() { echo ">>> $*" >&2; }
error() { echo "error $*"; exit 1; }
warning() { echo "warning: $*"; }

temp_dir=$(mktemp -d)
cleanup() { rm -rf $temp_dir; }
trap cleanup exit

available() { command -v $1 >/dev/null; }
require() {
    local missing=''
    for tool in $*; do
        if ! available $tool; then
            missing="$missing $tool"
        fi
    done

    echo $missing
}

[ "$(uname -s)" = "linux" ] || error 'this script is intended to run on linux only.'

arch=$(uname -m)
case "$arch" in
    x86_64) arch="amd64" ;;
    aarch64|arm64) arch="arm64" ;;
    *) error "unsupported architecture: $arch" ;;
esac

kern=$(uname -r)
case "$kern" in
    *icrosoft*wsl2 | *icrosoft*wsl2) ;;
    *icrosoft) error "microsoft wsl1 is not currently supported. please upgrade to wsl2 with 'wsl --set-version <distro> 2'" ;;
    *) ;;
esac

ver_param="${ollama_version:+?version=$ollama_version}"

sudo=
if [ "$(id -u)" -ne 0 ]; then
    # running as root, no need for sudo
    if ! available sudo; then
        error "this script requires superuser permissions. please re-run as root."
    fi

    sudo="sudo"
fi

needs=$(require curl awk grep sed tee xargs)
if [ -n "$needs" ]; then
    status "error: the following tools are required but missing:"
    for need in $needs; do
        echo "  - $need"
    done
    exit 1
fi

status "downloading ollama..."
# curl --fail --show-error --location --progress-bar -o $temp_dir/ollama "https://ollama.com/download/ollama-linux-${arch}${ver_param}"

for bindir in /usr/local/bin /usr/bin /bin; do
    echo $path | grep -q $bindir && break || continue
done

status "installing ollama to $bindir..."
$sudo install -o0 -g0 -m755 -d $bindir
# $sudo install -o0 -g0 -m755 $temp_dir/ollama $bindir/ollama
$sudo install -o0 -g0 -m755 ./ollama-linux-amd64  $bindir/ollama

install_success() { 
    status 'the ollama api is now available at 127.0.0.1:11434.'
    status 'install complete. run "ollama" from the command line.'
}
trap install_success exit

# everything from this point onwards is optional.

configure_systemd() {
    if ! id ollama >/dev/null 2>&1; then
        status "creating ollama user..."
        $sudo useradd -r -s /bin/false -u -m -d /usr/share/ollama ollama
    fi
    if getent group render >/dev/null 2>&1; then
        status "adding ollama user to render group..."
        $sudo usermod -a -g render ollama
    fi
    if getent group video >/dev/null 2>&1; then
        status "adding ollama user to video group..."
        $sudo usermod -a -g video ollama
    fi

    status "adding current user to ollama group..."
    $sudo usermod -a -g ollama $(whoami)

    status "creating ollama systemd service..."
    cat <<eof | $sudo tee /etc/systemd/system/ollama.service >/dev/null
[unit]
description=ollama service
after=network-online.target

[service]
execstart=$bindir/ollama serve
user=ollama
group=ollama
restart=always
restartsec=3
environment="path=$path"

[install]
wantedby=default.target
eof
    systemctl_running="$(systemctl is-system-running || true)"
    case $systemctl_running in
        running|degraded)
            status "enabling and starting ollama service..."
            $sudo systemctl daemon-reload
            $sudo systemctl enable ollama

            start_service() { $sudo systemctl restart ollama; }
            trap start_service exit
            ;;
    esac
}

if available systemctl; then
    configure_systemd
fi

if ! available lspci && ! available lshw; then
    warning "unable to detect nvidia/amd gpu. install lspci or lshw to automatically detect and install gpu dependencies."
    exit 0
fi

check_gpu() {
    # look for devices based on vendor id for nvidia and amd
    case $1 in
        lspci) 
            case $2 in
                nvidia) available lspci && lspci -d '10de:' | grep -q 'nvidia' || return 1 ;;
                amdgpu) available lspci && lspci -d '1002:' | grep -q 'amd' || return 1 ;;
            esac ;;
        lshw) 
            case $2 in
                nvidia) available lshw && $sudo lshw -c display -numeric | grep -q 'vendor: .* \[10de\]' || return 1 ;;
                amdgpu) available lshw && $sudo lshw -c display -numeric | grep -q 'vendor: .* \[1002\]' || return 1 ;;
            esac ;;
        nvidia-smi) available nvidia-smi || return 1 ;;
    esac
}

if check_gpu nvidia-smi; then
    status "nvidia gpu installed."
    exit 0
fi

if ! check_gpu lspci nvidia && ! check_gpu lshw nvidia && ! check_gpu lspci amdgpu && ! check_gpu lshw amdgpu; then
    install_success
    warning "no nvidia/amd gpu detected. ollama will run in cpu-only mode."
    exit 0
fi

if check_gpu lspci amdgpu || check_gpu lshw amdgpu; then
    # look for pre-existing rocm v6 before downloading the dependencies
    for search in "${hip_path:-''}" "${rocm_path:-''}" "/opt/rocm"; do
        if [ -n "${search}" ] && [ -e "${search}/lib/libhipblas.so.2" ]; then
            status "compatible amd gpu rocm library detected at ${search}"
            install_success
            exit 0
        fi
    done

    status "downloading amd gpu dependencies..."
    $sudo rm -rf /usr/share/ollama/lib
    $sudo chmod o+x /usr/share/ollama
    $sudo install -o ollama -g ollama -m 755 -d /usr/share/ollama/lib/rocm
    curl --fail --show-error --location --progress-bar "https://ollama.com/download/ollama-linux-amd64-rocm.tgz${ver_param}" \
        | $sudo tar zx --owner ollama --group ollama -c /usr/share/ollama/lib/rocm .
    install_success
    status "amd gpu dependencies installed."
    exit 0
fi

# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-7-centos-7
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-8-rocky-8
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#rhel-9-rocky-9
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#fedora
install_cuda_driver_yum() {
    status 'installing nvidia repository...'
    case $package_manager in
        yum)
            $sudo $package_manager -y install yum-utils
            $sudo $package_manager-config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
            ;;
        dnf)
            $sudo $package_manager config-manager --add-repo https://developer.download.nvidia.com/compute/cuda/repos/$1$2/$(uname -m)/cuda-$1$2.repo
            ;;
    esac

    case $1 in
        rhel)
            status 'installing epel repository...'
            # epel is required for third-party dependencies such as dkms and libvdpau
            $sudo $package_manager -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-$2.noarch.rpm || true
            ;;
    esac

    status 'installing cuda driver...'

    if [ "$1" = 'centos' ] || [ "$1$2" = 'rhel7' ]; then
        $sudo $package_manager -y install nvidia-driver-latest-dkms
    fi

    $sudo $package_manager -y install cuda-drivers
}

# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#ubuntu
# ref: https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html#debian
install_cuda_driver_apt() {
    status 'installing nvidia repository...'
    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

    case $1 in
        debian)
            status 'enabling contrib sources...'
            $sudo sed 's/main/contrib/' < /etc/apt/sources.list | $sudo tee /etc/apt/sources.list.d/contrib.list > /dev/null
            if [ -f "/etc/apt/sources.list.d/debian.sources" ]; then
                $sudo sed 's/main/contrib/' < /etc/apt/sources.list.d/debian.sources | $sudo tee /etc/apt/sources.list.d/contrib.sources > /dev/null
            fi
            ;;
    esac

    status 'installing cuda driver...'
    $sudo dpkg -i $temp_dir/cuda-keyring.deb
    $sudo apt-get update

    [ -n "$sudo" ] && sudo_e="$sudo -e" || sudo_e=
    debian_frontend=noninteractive $sudo_e apt-get -y install cuda-drivers -q
}

if [ ! -f "/etc/os-release" ]; then
    error "unknown distribution. skipping cuda installation."
fi

. /etc/os-release

os_name=$id
os_version=$version_id

package_manager=
for package_manager in dnf yum apt-get; do
    if available $package_manager; then
        break
    fi
done

if [ -z "$package_manager" ]; then
    error "unknown package manager. skipping cuda installation."
fi

if ! check_gpu nvidia-smi || [ -z "$(nvidia-smi | grep -o "cuda version: [0-9]*\.[0-9]*")" ]; then
    case $os_name in
        centos|rhel) install_cuda_driver_yum 'rhel' $(echo $os_version | cut -d '.' -f 1) ;;
        rocky) install_cuda_driver_yum 'rhel' $(echo $os_version | cut -c1) ;;
        fedora) [ $os_version -lt '37' ] && install_cuda_driver_yum $os_name $os_version || install_cuda_driver_yum $os_name '37';;
        amzn) install_cuda_driver_yum 'fedora' '37' ;;
        debian) install_cuda_driver_apt $os_name $os_version ;;
        ubuntu) install_cuda_driver_apt $os_name $(echo $os_version | sed 's/\.//') ;;
        *) exit ;;
    esac
fi

if ! lsmod | grep -q nvidia; then
    kernel_release="$(uname -r)"
    case $os_name in
        rocky) $sudo $package_manager -y install kernel-devel kernel-headers ;;
        centos|rhel|amzn) $sudo $package_manager -y install kernel-devel-$kernel_release kernel-headers-$kernel_release ;;
        fedora) $sudo $package_manager -y install kernel-devel-$kernel_release ;;
        debian|ubuntu) $sudo apt-get -y install linux-headers-$kernel_release ;;
        *) exit ;;
    esac

    nvidia_cuda_version=$($sudo dkms status | awk -f: '/added/ { print $1 }')
    if [ -n "$nvidia_cuda_version" ]; then
        $sudo dkms install $nvidia_cuda_version
    fi

    if lsmod | grep -q nouveau; then
        status 'reboot to complete nvidia cuda driver install.'
        exit 0
    fi

    $sudo modprobe nvidia
fi


status "nvidia cuda drivers installed."
步骤4:运行 install.sh脚本 ,安装
# 执行installl.sh脚本,需要sudo 权限  chmod +x install.sh
./install.sh
# 如果报错误权限不足,执行
chmod +x install.sh
# 如果报错误: bash: ./build_android.sh:/bin/sh^m:解释器错误: 没有那个文件或目录,执行
sed -i 's/\r$//' install.sh

在这里插入图片描述

步骤5:配置大模型下载目录
# 执行命令
vim ~/.bashrc
# 配置 ollama_models 环境变量自定义路径
### ollama model dir 改为自定义的路径,默认路径/usr/share/ollama/.ollama/models 
export ollama_models=/home/ollama/ollama_cache
# 复制/usr/share/ollama/.ollama/models目录中(blobs  manifests)的文件夹到ollama_models环境变量目录
cp -r /usr/share/ollama/.ollama/models /home/ollama/ollama_cache
步骤6:运行大模型,如通义千问
# 需要先将大模型下载到ollama_models文件中
# ollama run <模型名称>
ollama run qwen
步骤7:关闭 ollama 服务
# 关闭ollama服务
service ollama stop
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com