安装conda

下载:

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh

交互式安装:

bash ~/Miniconda3-latest-Linux-x86_64.sh

静默安装:

bash ~/Miniconda3-latest-Linux-x86_64.sh -b -u -p /root/miniconda3

 

激活环境:

手动执行:

eval "$(/root/miniconda3/bin/conda shell.bash hook)"

运行脚本初始化:在  ~/.bashrc 文件中已经包含了所有的初始化命令,只要加载这个文件即可。

source ~/.bashrc


cat .bashrc
...
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/root/miniconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
    eval "$__conda_setup"
else
    if [ -f "/root/miniconda3/etc/profile.d/conda.sh" ]; then
        . "/root/miniconda3/etc/profile.d/conda.sh"
    else
        export PATH="/root/miniconda3/bin:$PATH"
    fi
fi
unset __conda_setup
# <<< conda initialize <<<


配置下载源:

生成 ~/.condarc 文件:用来配置下载源,高优先级

conda config --set show_channel_urls yes

修改下载源:.condarc 默认在用户的家目录下,在安装目录 miniconda3/ 下也有一个但是优先级低于 ~/.condarc ,用命令修改下载源修改的就是  ~/.condarc 这个文件。

手动修改:

vim ~/.condarc

channels:
  - defaults
show_channel_urls: true
default_channels:
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free
  - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
custom_channels:
  conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
  simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

使用命令修改:

#全部清华园,备用

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch-lts
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/impleitks
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/deepmodeling
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/auto
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/biobakery
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/caffe2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/qiime2

其他命令:

conda config --set auto_activate_base false
作用:禁用 Conda 的 base 环境自动激活。

conda config --remove-key channels
作用:删除 Conda 配置中所有自定义的软件源(channels)。

查看信息:

# conda info

     active environment : base
    active env location : /root/miniconda3
            shell level : 1
       user config file : /root/.condarc
 populated config files : /root/miniconda3/.condarc
                          /root/.condarc
          conda version : 25.1.1
    conda-build version : not installed
         python version : 3.12.9.final.0
                 solver : libmamba (default)
       virtual packages : __archspec=1=skylake
                          __conda=25.1.1=0
                          __cuda=12.4=0
                          __glibc=2.39=0
                          __linux=6.11.0=0
                          __unix=0=0
       base environment : /root/miniconda3  (writable)
      conda av data dir : /root/miniconda3/etc/conda
  conda av metadata url : None
           channel URLs : https://repo.anaconda.com/pkgs/main/linux-64
                          https://repo.anaconda.com/pkgs/main/noarch
                          https://repo.anaconda.com/pkgs/r/linux-64
                          https://repo.anaconda.com/pkgs/r/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/noarch
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/linux-64
                          https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r/noarch
          package cache : /root/miniconda3/pkgs
                          /root/.conda/pkgs
       envs directories : /root/miniconda3/envs
                          /root/.conda/envs
               platform : linux-64
             user-agent : conda/25.1.1 requests/2.32.3 CPython/3.12.9 Linux/6.11.0-19-generic ubuntu/24.04.2 glibc/2.39 solver/libmamba conda-libmamba-solver/25.1.1 libmambapy/2.0.5 aau/0.5.0 c/. s/. e/.
                UID:GID : 0:0
             netrc file : None
           offline mode : False



安装显卡驱动 nvidia drivers

安装工具包:

apt update
apt upgrade
apt install build-essential dkms

避免开源驱动冲突:禁用 Linux 系统中的 Nouveau 开源显卡驱动(主要针对 NVIDIA 显卡),安装 NVIDIA 官方驱动必须禁用 Nouveau 才能正常安装。

vim /etc/modprobe.d/blacklist-nouv.conf
blacklist nouveau
blacklist lbm-nouveau
options nouveau modeset=0

update-initramfs -u # 更新配置

# 重启后查看
lsmod | grep nouveau  # 若无输出则表示成功

到官网下载驱动:

地址:

https://www.nvidia.cn/drivers/lookup/

图片.png

安装:

chmod +x NVIDIA-Linux-x86_64-570.133.07.run
sudo ./NVIDIA-Linux-x86_64-570.133.07.run

安装驱动:

方式一:

lspci | grep -i nvidia  # 查看显卡型号

# 检测推荐驱动版本
~# nvidia-detector
nvidia-driver-570 

apt install nvidia-driver- # 双击tab

apt install nvidia-driver-570-server -y

方式二:

自动安装ubutun驱动:

sudo ubuntu-drivers autoinstall  # Ubuntu 自动安装推荐驱动



安装CUDA

方法一:命令行直接安装

apt install nvidia-cuda-toolkit

方法二:去官网下载后安装

官方地址:

developer.nvidia.com/cuda-downloads

image.png

下载:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2404/x86_64/cuda-keyring_1.1-1_all.deb

安装:

sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update
sudo apt-get -y install cuda-toolkit-12-8

查看是否安装成功:

~# nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2023 NVIDIA Corporation
Built on Fri_Jan__6_16:45:21_PST_2023
Cuda compilation tools, release 12.0, V12.0.140
Build cuda_12.0.r12.0/compiler.32267302_0



安装vLLM

配置国内下载源:

mkdir -p ~/.pip
vim ~/.pip/pip.conf

[global]
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

pip config list

创建虚拟环境:

conda create --name llm python=3.10
conda activate llm # 禁止用deactivate

安装vllm:

pip install vLLM
vllm --version

安装pytorch:

方式一:到官网下载到本地安装

http://download.pytorch.org/whl/torch

方式二:命令行安装

conda install pytorch


下载模型:

http://hf-mirror.com/deepseek-ai/DeepSeek-R1-Distill-Qwen-1.5B/tree/main


测试代码:

# Qwen2-vLLM-Local.py
import os
from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

# 设置环境变量(cpu环境需要单独设置,GPU环境不需要)
#os.environ['VLLM_TARGET_DEVICE'] = 'gpu'

# 模型ID:我们下载的模型权重文件目录
#model_dir = '/data/ModelSpace/Qwen2-0.5B'
model_dir = '/root/model/' # 模型目录

# 初始化 Tokenizer
tokenizer = AutoTokenizer.from_pretrained(
    model_dir,
    local_files_only=True,
)

# 提示词
messages = [
    {'role': 'system', 'content': '你是一个智能回答助手。'},
    {'role': 'user', 'content': '天空为什么是蓝色的?'}
]
text = tokenizer.apply_chat_template(
    messages,
    tokenize=False,
    add_generation_prompt=True,
)

# 初始化大语言模型
llm = LLM(
    model=model_dir,
    tensor_parallel_size=1,
    device='cuda',  # CPU 无需张量并行,cpu填写cpu,gpu填写cuda
    max_model_len=32768,
    gpu_memory_utilization=0.95
)

# 超参数:最多512个Token
sampling_params = SamplingParams(
    temperature=0.7,
    top_p=0.8,
    repetition_penalty=1.05,
    max_tokens=512
)

# 模型推理输出
outputs = llm.generate([text], sampling_params)

for output in outputs:
    prompt = output.prompt
    generated_text = output.outputs[0].text
    print(f'提示词: {prompt!r}, 大模型推理输出: {generated_text!r}')

测试:

conda activate llm
python llm-vLLM-Local.py

图片.png