目录
1、安装Anaconda3
在官网https://www.anaconda.com/products/distribution#macos上找到合适Linux版的Anaconda安装包:

2、将Anaconda3的安装包上传至服务器
可以使用Xftp 7这款软件直接对本地和服务器上的文件进行操作

只需把本地的文件直接拖拽到右侧服务器上的区域即可。
3、在服务器上安装Anaconda
bash Anaconda3-2022.10-Linux-x86_64.sh
在Xshell上直接执行这个指令,然后一路Enter/yes完成安装操作
在安装的过程中,它可能会要你指定安装的目录,如果当前文件夹空间比较有限,你可以自定义安装到一个空间比较大的目录中:
Anaconda3 will now be installed into this location:
/home/xxx/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/home/xxx/anaconda3] >>> /raid/xxx/yyy/anaconda3
PREFIX=/raid/xxx/yyy/anaconda3
之后,你的anaconda就会被安装到/raid/xxx/yyy/anaconda3这个路径
注意:如果使用自定义的安装路径,可能需要后续配置一下服务器上面的一些配置文件,好让anaconda找到你安装的位置。具体的配置方法可以问问GPT。
4、配置清华源镜像(加速后续下载)
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
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/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/fastai/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
conda config --append channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda/
conda config --set show_channel_urls yes
默认下载镜像源设置好了以后,你在任何一个虚拟环境下载软件或者包都是从这个源下载的。当然你也可以不更改默认的下载源,而是每次下载包的时候都在命令后面添加镜像源地址(也没有很费事)。
5、在Anaconda中配置虚拟环境
5.1 创建虚拟环境
conda create -n research python=3.11
5.2 激活虚拟环境
conda activate research
5.3 Pytorch下载
Pytorch官网:PyTorch

可以用Pytorch官网上面提供的代码来进行安装,但是会非常慢,比较推荐的方法是使用下面的阿里云镜像来进行安装:
pip install torch torchvision -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
在pytorch虚拟环境中执行上述语句,就会在pytorch虚拟环境中安装pytorch了。
5.4使用whl(轮子文件)安装pytorch(强烈推荐!!)
进入下面这个网址,pytorch的下载官网官网. (可以把cu后面的数字更改为你现在要安装的cuda的版本)
假设我现在想要安装torch2.4.1+cu121 这个版本(表示torch版本为2.4.1,GPU对应的版本是1.2.1)
https://download.pytorch.org/whl/cu121
在页面中找到torch, torchvision, torchaudio; 点击进入。
torch, torchvision, torchaudio的版本对应关系在 Previous PyTorch Versions | PyTorch 查阅。

找到你要安装的版本,要和python版本对应好
e.g. torch-2.4.1+cu121-cp311-cp311-linux_x86_64.whl 表示 torch版本2.4.1, cu版本12.1, python版本3.11

5.4.1 本地手动下载
将轮子文件下载下来

然后上传到服务器上,切换到你的虚拟环境,使用pip install命令安装即可。
pip install torch-2.4.1+cu121-cp311-cp311-linux_x86_64.whl
5.4.2 服务器直接下载
如果你的服务器网速足够快(e.g. 公司),或者你发现上面这种做法反而很慢,那么你可以直接在服务器上执行下载命令:
他就会直接在网址 https://download.pytorch.org/whl/cu118 中寻找对应版本的torch进行安装;
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 --index-url https://download.pytorch.org/whl/cu118
pip install torch==2.1.2 torchvision==0.16.2 torchaudio==2.1.2 \
--index-url https://download.pytorch.org/whl/cu118 \
--no-cache-dir \
--isolated
--isolated 的作用是:
- 忽略用户的 pip 配置文件
- 不读
~/.pip/pip.conf - 不受你之前设置的阿里云/extra-index-url 干扰
这样才是真正只看你命令行指定的源。
5.4.3 服务器wget下载,手动安装(最推荐)
执行后面的wget命令会下载wheel文件到当前目录,建议切换一个目录保存。
cd /root/zhanghaotian/torch
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/torch-2.5.1%2Bcu121-cp310-cp310-linux_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/torchvision-0.20.1%2Bcu121-cp310-cp310-linux_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/torchaudio-2.5.1%2Bcu121-cp310-cp310-linux_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_nccl_cu12-2.21.5-py3-none-manylinux2014_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl
wget -c https://mirrors.aliyun.com/pytorch-wheels/cu121/nvidia_nvjitlink_cu12-12.1.105-py3-none-manylinux1_x86_64.whl
pip install \
./nvidia_cublas_cu12-12.1.3.1-py3-none-manylinux1_x86_64.whl \
./nvidia_cuda_cupti_cu12-12.1.105-py3-none-manylinux1_x86_64.whl \
./nvidia_cuda_nvrtc_cu12-12.1.105-py3-none-manylinux1_x86_64.whl \
./nvidia_cuda_runtime_cu12-12.1.105-py3-none-manylinux1_x86_64.whl \
./nvidia_cudnn_cu12-9.1.0.70-py3-none-manylinux2014_x86_64.whl \
./nvidia_cufft_cu12-11.0.2.54-py3-none-manylinux1_x86_64.whl \
./nvidia_curand_cu12-10.3.2.106-py3-none-manylinux1_x86_64.whl \
./nvidia_cusolver_cu12-11.4.5.107-py3-none-manylinux1_x86_64.whl \
./nvidia_cusparse_cu12-12.1.0.106-py3-none-manylinux1_x86_64.whl \
./nvidia_nccl_cu12-2.21.5-py3-none-manylinux2014_x86_64.whl \
./nvidia_nvtx_cu12-12.1.105-py3-none-manylinux1_x86_64.whl \
./nvidia_nvjitlink_cu12-12.1.105-py3-none-manylinux1_x86_64.whl \
--no-index \
--find-links .
pip download \
sympy==1.13.1 \
triton==3.1.0 \
numpy \
pillow \
typing_extensions \
filelock \
networkx \
jinja2 \
fsspec \
mpmath \
MarkupSafe \
-d . \
-i https://pypi.tuna.tsinghua.edu.cn/simple \
--extra-index-url https://mirrors.aliyun.com/pypi/simple/ \
--extra-index-url https://pypi.mirrors.ustc.edu.cn/simple \
--extra-index-url https://repo.huaweicloud.com/repository/pypi/simple \
--trusted-host pypi.tuna.tsinghua.edu.cn \
--trusted-host mirrors.aliyun.com \
--trusted-host pypi.mirrors.ustc.edu.cn \
--trusted-host repo.huaweicloud.com
pip install \
./torch-2.5.1+cu121-cp310-cp310-linux_x86_64.whl \
./torchvision-0.20.1+cu121-cp310-cp310-linux_x86_64.whl \
./torchaudio-2.5.1+cu121-cp310-cp310-linux_x86_64.whl \
--find-links . \
--no-index
安装其他依赖项
6、在VScode中使用pytorch
VScode中需要先安装Remote SSH插件,并建立链接,详见这篇文章使用VScode连接远程服务器的配置方法_vscode hostname user-CSDN博客
因为服务器上面装了python,因此如果我们不做调整,那默认的会使用服务器上装好的python,而不是我们自己装的虚拟环境pytorch。在VScode中进行以下操作:
第一步、 Ctrl+shift+P
第二步、选择Python: Select Interpreter,选择你的pytorch虚拟环境中python解释器

在上图中,第一行(base)表示的当前的环境是base,使用conda activate pytorch就可以切换到pytorch这个环境中;
第二行的(pytorch)就表示当前环境是pytorch。
zht@daitao:~$ :表示当前所在的目录
该文详细介绍了如何在Linux服务器上安装Anaconda3,通过清华源加速下载,创建和激活虚拟环境,特别是如何在Anaconda环境中安装Pytorch。此外,还讲解了在VScode中利用RemoteSSH插件连接服务器并选择Pytorch虚拟环境的步骤。

955

被折叠的 条评论
为什么被折叠?



