本文介绍了conda下安装cpu、gpu版本的pytorch;并介绍了如何设置镜像源
系统:ubuntu22.04
显卡:RTX 3050
依赖工具:miniconda
lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
$ nvidia-smi
Tue Feb 13 21:51:33 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.154.05 Driver Version: 535.154.05 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 NVIDIA GeForce RTX 3050 ... Off | 00000000:02:00.0 Off | N/A |
| N/A 48C P3 7W / 35W | 435MiB / 4096MiB | 11% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| 0 N/A N/A 1854 G /usr/lib/xorg/Xorg 226MiB |
| 0 N/A N/A 2225 G /usr/bin/gnome-shell 84MiB |
| 0 N/A N/A 3199 G ...irefox/2987/usr/lib/firefox/firefox 95MiB |
| 0 N/A N/A 48808 G ...resh,SpareRendererForSitePerProcess 21MiB |
+---------------------------------------------------------------------------------------+
$ conda --version
conda 23.11.0
注意:默认conda安装pytorch的是cpu版本,如需要安装GPU版本的,注意直接看后面
$ conda create -n myPyt
新创建的环境不包含任何依赖可以使用conda list
查看一下
开始安装pytorch(当然,也可以前面在创建环境的同时,把依赖包一同时安装了)
$ conda install pytorch
为了方便验证同时安装ipython
IPython 是 Python 的原生交互式 shell 的增强版,可以完成许多不同寻常的任务,比如帮助实现并行化计算;主要使用它提供的交互性帮助,比如代码着色、改进了的命令行回调、制表符完成、宏功能以及改进了的交互式帮助
# 激活环境
$ conda activate myPyt
# 安装ipython
$ conda install ipython
输入ipython
,进入交互式环境,依次输入如下两条命令import torch
、torch.cuda.is_available()
$ ipython
Python 3.11.7 (main, Dec 15 2023, 18:12:31) [GCC 11.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.20.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import torch
In [2]: torch.cuda.is_available()
Out[2]: False
发现当前的版本是不cpu版本,不支持cuda加速的,我们查看一下依赖
$ conda list
# packages in environment at /home/bing/miniconda3/envs/myEnv:
#
# Name Version Build Channel
_libgcc_mutex 0.1 main
_openmp_mutex 5.1 1_gnu
blas 1.0 mkl
bzip2 1.0.8 h7b6447c_0
ca-certificates 2023.12.12 h06a4308_0
cffi 1.16.0 py311h5eee18b_0
filelock 3.13.1 py311h06a4308_0
fsspec 2023.10.0 py311h06a4308_0
gmp 6.2.1 h295c915_3
gmpy2 2.1.2 py311hc9b5ff0_0
intel-openmp 2023.1.0 hdb19cb5_46306
jinja2 3.1.3 py311h06a4308_0
ld_impl_linux-64 2.38 h1181459_1
libffi 3.4.4 h6a678d5_0
libgcc-ng 11.2.0 h1234567_1
libgomp 11.2.0 h1234567_1
libprotobuf 3.20.3 he621ea3_0
libstdcxx-ng 11.2.0 h1234567_1
libuuid 1.41.5 h5eee18b_0
markupsafe 2.1.3 py311h5eee18b_0
mkl 2023.1.0 h213fc3f_46344
mkl-service 2.4.0 py311h5eee18b_1
mkl_fft 1.3.8 py311h5eee18b_0
mkl_random 1.2.4 py311hdb19cb5_0
mpc 1.1.0 h10f8cd9_1
mpfr 4.0.2 hb69a4c5_1
mpmath 1.3.0 py311h06a4308_0
ncurses 6.4 h6a678d5_0
networkx 3.1 py311h06a4308_0
ninja 1.10.2 h06a4308_5
ninja-base 1.10.2 hd09550d_5
numpy 1.26.3 py311h08b1b3b_0
numpy-base 1.26.3 py311hf175353_0
openssl 3.0.13 h7f8727e_0
pip 23.3.1 py311h06a4308_0
pycparser 2.21 pyhd3eb1b0_0
python 3.11.7 h955ad1f_0
pytorch 2.1.0 cpu_py311h6d93b4c_0
readline 8.2 h5eee18b_0
setuptools 68.2.2 py311h06a4308_0
sqlite 3.41.2 h5eee18b_0
sympy 1.12 py311h06a4308_0
tbb 2021.8.0 hdb19cb5_0
tk 8.6.12 h1ccaba5_0
typing-extensions 4.9.0 py311h06a4308_1
typing_extensions 4.9.0 py311h06a4308_1
tzdata 2023d h04d1e81_0
wheel 0.41.2 py311h06a4308_0
xz 5.4.5 h5eee18b_0
zlib 1.2.13 h5eee18b_0
发现确实没安装任何cuda库,而pytorch的版本我们也可以看到确实cpu版本pytorch 2.1.0 cpu_py311h6d93b4c_0
如何安装gpu版本的pytorch呢?我们继续
我们查看一下pytorch可安装的版本
$ conda search pytorch
Loading channels: done
# Name Version Build Channel
...
pytorch 1.13.1 gpu_cuda113py39h0809116_0 pkgs/main
pytorch 1.13.1 gpu_cuda113py39h09dffc6_0 pkgs/main
pytorch 1.13.1 gpu_cuda113py39h926b89d_1 pkgs/main
pytorch 1.13.1 gpu_cuda113py39hde3f150_1 pkgs/main
pytorch 2.0.1 cpu_py310hab5cca8_0 pkgs/main
pytorch 2.0.1 cpu_py310hdc00b08_0 pkgs/main
pytorch 2.0.1 cpu_py311h53e38e9_0 pkgs/main
pytorch 2.0.1 cpu_py311h6d93b4c_0 pkgs/main
pytorch 2.0.1 cpu_py38hab5cca8_0 pkgs/main
pytorch 2.0.1 cpu_py38hdc00b08_0 pkgs/main
pytorch 2.0.1 cpu_py39hab5cca8_0 pkgs/main
pytorch 2.0.1 cpu_py39hdc00b08_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py310h7799f5a_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py310he342708_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py311h7668aad_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py311hce0f3bd_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py38h7799f5a_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py38he342708_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py39h7799f5a_0 pkgs/main
pytorch 2.0.1 gpu_cuda118py39he342708_0 pkgs/main
pytorch 2.1.0 cpu_py310hab5cca8_0 pkgs/main
pytorch 2.1.0 cpu_py310hdc00b08_0 pkgs/main
pytorch 2.1.0 cpu_py311h53e38e9_0 pkgs/main
pytorch 2.1.0 cpu_py311h6d93b4c_0 pkgs/main
pytorch 2.1.0 cpu_py38hab5cca8_0 pkgs/main
pytorch 2.1.0 cpu_py38hdc00b08_0 pkgs/main
pytorch 2.1.0 cpu_py39hab5cca8_0 pkgs/main
pytorch 2.1.0 cpu_py39hdc00b08_0 pkgs/main
我们可以看到,2.1.0
版本的,Build
列中,有点的cpu
有的是gpu
,那么如何安装时指定安装带gpu表示的版本的呢?
我们只需制定版本号的同时,指定build即可
$ conda create -n pyt-gpu
$ conda activate pyt-gpu
$ conda install pytorch=2.0.1=gpu_cuda118py39he342708_0
...
The following NEW packages will be INSTALLED:
_libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
_openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
blas pkgs/main/linux-64::blas-1.0-mkl
ca-certificates pkgs/main/linux-64::ca-certificates-2023.12.12-h06a4308_0
cffi pkgs/main/linux-64::cffi-1.16.0-py39h5eee18b_0
cudatoolkit pkgs/main/linux-64::cudatoolkit-11.8.0-h6a678d5_0
cudnn pkgs/main/linux-64::cudnn-8.9.2.26-cuda11_0
cupti pkgs/main/linux-64::cupti-11.8.0-he078b1a_0
filelock pkgs/main/linux-64::filelock-3.13.1-py39h06a4308_0
gmp pkgs/main/linux-64::gmp-6.2.1-h295c915_3
gmpy2 pkgs/main/linux-64::gmpy2-2.1.2-py39heeb90bb_0
intel-openmp pkgs/main/linux-64::intel-openmp-2023.1.0-hdb19cb5_46306
jinja2 pkgs/main/linux-64::jinja2-3.1.3-py39h06a4308_0
ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
libffi pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0
libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
libprotobuf pkgs/main/linux-64::libprotobuf-3.20.3-he621ea3_0
libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
magma pkgs/main/linux-64::magma-2.7.1-h2c23e93_0
markupsafe pkgs/main/linux-64::markupsafe-2.1.3-py39h5eee18b_0
mkl pkgs/main/linux-64::mkl-2023.1.0-h213fc3f_46344
mkl-service pkgs/main/linux-64::mkl-service-2.4.0-py39h5eee18b_1
mkl_fft pkgs/main/linux-64::mkl_fft-1.3.8-py39h5eee18b_0
mkl_random pkgs/main/linux-64::mkl_random-1.2.4-py39hdb19cb5_0
mpc pkgs/main/linux-64::mpc-1.1.0-h10f8cd9_1
mpfr pkgs/main/linux-64::mpfr-4.0.2-hb69a4c5_1
mpmath pkgs/main/linux-64::mpmath-1.3.0-py39h06a4308_0
ncurses pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
networkx pkgs/main/linux-64::networkx-3.1-py39h06a4308_0
ninja pkgs/main/linux-64::ninja-1.10.2-h06a4308_5
ninja-base pkgs/main/linux-64::ninja-base-1.10.2-hd09550d_5
numpy pkgs/main/linux-64::numpy-1.26.3-py39h5f9d8c6_0
numpy-base pkgs/main/linux-64::numpy-base-1.26.3-py39hb5e798b_0
openssl pkgs/main/linux-64::openssl-3.0.13-h7f8727e_0
pip pkgs/main/linux-64::pip-23.3.1-py39h06a4308_0
pycparser pkgs/main/noarch::pycparser-2.21-pyhd3eb1b0_0
python pkgs/main/linux-64::python-3.9.18-h955ad1f_0
pytorch pkgs/main/linux-64::pytorch-2.0.1-gpu_cuda118py39he342708_0
readline pkgs/main/linux-64::readline-8.2-h5eee18b_0
setuptools pkgs/main/linux-64::setuptools-68.2.2-py39h06a4308_0
sqlite pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
sympy pkgs/main/linux-64::sympy-1.12-py39h06a4308_0
tbb pkgs/main/linux-64::tbb-2021.8.0-hdb19cb5_0
tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
typing-extensions pkgs/main/linux-64::typing-extensions-4.9.0-py39h06a4308_1
typing_extensions pkgs/main/linux-64::typing_extensions-4.9.0-py39h06a4308_1
tzdata pkgs/main/noarch::tzdata-2023d-h04d1e81_0
wheel pkgs/main/linux-64::wheel-0.41.2-py39h06a4308_0
xz pkgs/main/linux-64::xz-5.4.5-h5eee18b_0
zlib pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
Proceed ([y]/n)?
安装时,从提示安装的依赖我们可以看出,这个版本确实带上了cuda相关包cudatoolkit
、cudnn
,这次安装显然时间长了很多,包的大小也近2G
安装完成后,我们再次确认一下cuda加速是否可用;
同样,我们先安装一个ipython
$ conda install ipython
进入ipython
后,依次执行如下代码import torch
、torch.cuda.is_available()
$ ipython
Python 3.9.18 (main, Sep 11 2023, 13:41:44)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.15.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import torch
In [2]: torch.cuda.is_available()
Out[2]: True
这次cuda可以正常工作,到此完成gpu版本的pytorch安装