在 Linux 上搭建 PyTorch 环境,通常推荐用 conda(或 miniconda) 或 venv + pip。下面给你一套最常用、最稳妥的流程。
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
按提示安装,最后重启终端或:
source ~/.bashrc
conda create -n pytorch python=3.10 -y
conda activate pytorch
conda install pytorch torchvision torchaudio cpuonly -c pytorch
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
不确定 CUDA 版本可用:
nvidia-smi
python -c "import torch; print(torch.__version__)"
python3 -m venv pytorch-env
source pytorch-env/bin/activate
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install jupyter
jupyter notebook
pip install numpy pandas matplotlib scikit-learn
Illegal instruction → Python 版本太高,换 3.9/3.10CUDA not available → 驱动或 CUDA 版本不匹配如果你告诉我:
我可以直接给你一条可复制的命令。