0


ubuntu22.04下使用conda安装pytorch(cpu及gpu版本)

本文介绍了conda下安装cpu、gpu版本的pytorch;并介绍了如何设置镜像源

ubuntu环境安装pytorch的CPU版本与GPU版本

系统:ubuntu22.04
显卡:RTX 3050
依赖工具:miniconda

确认环境

  1. lsb_release -a
  2. No LSB modules are available.
  3. Distributor ID: Ubuntu
  4. Description: Ubuntu 22.04.3 LTS
  5. Release: 22.04
  6. Codename: jammy
  1. $ nvidia-smi
  2. Tue Feb 1321:51:33 2024
  3. +---------------------------------------------------------------------------------------+
  4. | NVIDIA-SMI 535.154.05 Driver Version: 535.154.05 CUDA Version: 12.2||-----------------------------------------+----------------------+----------------------+
  5. | 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 |
  6. +-----------------------------------------+----------------------+----------------------+
  7. +---------------------------------------------------------------------------------------+
  8. | 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 |
  9. +---------------------------------------------------------------------------------------+
  1. $ conda --version
  2. conda 23.11.0

使用conda安装pytorch(CPU版本)

注意:默认conda安装pytorch的是cpu版本,如需要安装GPU版本的,注意直接看后面

创建一个新conda环境
  1. $ conda create -n myPyt

新创建的环境不包含任何依赖可以使用

  1. conda list

查看一下

开始安装pytorch(当然,也可以前面在创建环境的同时,把依赖包一同时安装了)

  1. $ conda install pytorch

为了方便验证同时安装

  1. ipython

IPython 是 Python 的原生交互式 shell 的增强版,可以完成许多不同寻常的任务,比如帮助实现并行化计算;主要使用它提供的交互性帮助,比如代码着色、改进了的命令行回调、制表符完成、宏功能以及改进了的交互式帮助

  1. # 激活环境
  2. $ conda activate myPyt
  3. # 安装ipython
  4. $ conda install ipython
验证一下pytorch环境

输入

  1. ipython

,进入交互式环境,依次输入如下两条命令

  1. import torch

  1. torch.cuda.is_available()
  1. $ ipython
  2. Python 3.11.7 (main, Dec 152023, 18:12:31)[GCC 11.2.0]
  3. Type 'copyright', 'credits' or 'license'formore information
  4. IPython 8.20.0 -- An enhanced Interactive Python. Type '?'for help.
  5. In [1]: import torch
  6. In [2]: torch.cuda.is_available()
  7. Out[2]: False

发现当前的版本是不cpu版本,不支持cuda加速的,我们查看一下依赖

  1. $ conda list
  2. # packages in environment at /home/bing/miniconda3/envs/myEnv: # # Name Version Build Channel
  3. _libgcc_mutex 0.1 main
  4. _openmp_mutex 5.1 1_gnu
  5. blas 1.0 mkl
  6. bzip21.0.8 h7b6447c_0
  7. ca-certificates 2023.12.12 h06a4308_0
  8. cffi 1.16.0 py311h5eee18b_0
  9. filelock 3.13.1 py311h06a4308_0
  10. fsspec 2023.10.0 py311h06a4308_0
  11. gmp 6.2.1 h295c915_3
  12. gmpy2 2.1.2 py311hc9b5ff0_0
  13. intel-openmp 2023.1.0 hdb19cb5_46306
  14. jinja2 3.1.3 py311h06a4308_0
  15. ld_impl_linux-64 2.38 h1181459_1
  16. libffi 3.4.4 h6a678d5_0
  17. libgcc-ng 11.2.0 h1234567_1
  18. libgomp 11.2.0 h1234567_1
  19. libprotobuf 3.20.3 he621ea3_0
  20. libstdcxx-ng 11.2.0 h1234567_1
  21. libuuid 1.41.5 h5eee18b_0
  22. markupsafe 2.1.3 py311h5eee18b_0
  23. mkl 2023.1.0 h213fc3f_46344
  24. mkl-service 2.4.0 py311h5eee18b_1
  25. mkl_fft 1.3.8 py311h5eee18b_0
  26. mkl_random 1.2.4 py311hdb19cb5_0
  27. mpc 1.1.0 h10f8cd9_1
  28. mpfr 4.0.2 hb69a4c5_1
  29. mpmath 1.3.0 py311h06a4308_0
  30. ncurses 6.4 h6a678d5_0
  31. networkx 3.1 py311h06a4308_0
  32. ninja 1.10.2 h06a4308_5
  33. ninja-base 1.10.2 hd09550d_5
  34. numpy 1.26.3 py311h08b1b3b_0
  35. numpy-base 1.26.3 py311hf175353_0
  36. openssl 3.0.13 h7f8727e_0
  37. pip 23.3.1 py311h06a4308_0
  38. pycparser 2.21 pyhd3eb1b0_0
  39. python 3.11.7 h955ad1f_0
  40. pytorch 2.1.0 cpu_py311h6d93b4c_0
  41. readline 8.2 h5eee18b_0
  42. setuptools 68.2.2 py311h06a4308_0
  43. sqlite 3.41.2 h5eee18b_0
  44. sympy 1.12 py311h06a4308_0
  45. tbb 2021.8.0 hdb19cb5_0
  46. tk 8.6.12 h1ccaba5_0
  47. typing-extensions 4.9.0 py311h06a4308_1
  48. typing_extensions 4.9.0 py311h06a4308_1
  49. tzdata 2023d h04d1e81_0
  50. wheel 0.41.2 py311h06a4308_0
  51. xz 5.4.5 h5eee18b_0
  52. zlib 1.2.13 h5eee18b_0

发现确实没安装任何cuda库,而pytorch的版本我们也可以看到确实cpu版本

  1. pytorch 2.1.0 cpu_py311h6d93b4c_0

conda安装GPU版本的pytorch

如何安装gpu版本的pytorch呢?我们继续
我们查看一下pytorch可安装的版本

  1. $ conda search pytorch
  2. Loading channels: done# Name Version Build Channel...
  3. pytorch 1.13.1 gpu_cuda113py39h0809116_0 pkgs/main
  4. pytorch 1.13.1 gpu_cuda113py39h09dffc6_0 pkgs/main
  5. pytorch 1.13.1 gpu_cuda113py39h926b89d_1 pkgs/main
  6. pytorch 1.13.1 gpu_cuda113py39hde3f150_1 pkgs/main
  7. pytorch 2.0.1 cpu_py310hab5cca8_0 pkgs/main
  8. pytorch 2.0.1 cpu_py310hdc00b08_0 pkgs/main
  9. pytorch 2.0.1 cpu_py311h53e38e9_0 pkgs/main
  10. pytorch 2.0.1 cpu_py311h6d93b4c_0 pkgs/main
  11. pytorch 2.0.1 cpu_py38hab5cca8_0 pkgs/main
  12. pytorch 2.0.1 cpu_py38hdc00b08_0 pkgs/main
  13. pytorch 2.0.1 cpu_py39hab5cca8_0 pkgs/main
  14. pytorch 2.0.1 cpu_py39hdc00b08_0 pkgs/main
  15. pytorch 2.0.1 gpu_cuda118py310h7799f5a_0 pkgs/main
  16. pytorch 2.0.1 gpu_cuda118py310he342708_0 pkgs/main
  17. pytorch 2.0.1 gpu_cuda118py311h7668aad_0 pkgs/main
  18. pytorch 2.0.1 gpu_cuda118py311hce0f3bd_0 pkgs/main
  19. pytorch 2.0.1 gpu_cuda118py38h7799f5a_0 pkgs/main
  20. pytorch 2.0.1 gpu_cuda118py38he342708_0 pkgs/main
  21. pytorch 2.0.1 gpu_cuda118py39h7799f5a_0 pkgs/main
  22. pytorch 2.0.1 gpu_cuda118py39he342708_0 pkgs/main
  23. pytorch 2.1.0 cpu_py310hab5cca8_0 pkgs/main
  24. pytorch 2.1.0 cpu_py310hdc00b08_0 pkgs/main
  25. pytorch 2.1.0 cpu_py311h53e38e9_0 pkgs/main
  26. pytorch 2.1.0 cpu_py311h6d93b4c_0 pkgs/main
  27. pytorch 2.1.0 cpu_py38hab5cca8_0 pkgs/main
  28. pytorch 2.1.0 cpu_py38hdc00b08_0 pkgs/main
  29. pytorch 2.1.0 cpu_py39hab5cca8_0 pkgs/main
  30. pytorch 2.1.0 cpu_py39hdc00b08_0 pkgs/main

我们可以看到,

  1. 2.1.0

版本的,

  1. Build

列中,有点的

  1. cpu

有的是

  1. gpu

,那么如何安装时指定安装带gpu表示的版本的呢?

我们只需制定版本号的同时,指定build即可

  1. $ conda create -n pyt-gpu
  2. $ conda activate pyt-gpu
  3. $ conda installpytorch=2.0.1=gpu_cuda118py39he342708_0
  4. ...
  5. The following NEW packages will be INSTALLED:
  6. _libgcc_mutex pkgs/main/linux-64::_libgcc_mutex-0.1-main
  7. _openmp_mutex pkgs/main/linux-64::_openmp_mutex-5.1-1_gnu
  8. blas pkgs/main/linux-64::blas-1.0-mkl
  9. ca-certificates pkgs/main/linux-64::ca-certificates-2023.12.12-h06a4308_0
  10. cffi pkgs/main/linux-64::cffi-1.16.0-py39h5eee18b_0
  11. cudatoolkit pkgs/main/linux-64::cudatoolkit-11.8.0-h6a678d5_0
  12. cudnn pkgs/main/linux-64::cudnn-8.9.2.26-cuda11_0
  13. cupti pkgs/main/linux-64::cupti-11.8.0-he078b1a_0
  14. filelock pkgs/main/linux-64::filelock-3.13.1-py39h06a4308_0
  15. gmp pkgs/main/linux-64::gmp-6.2.1-h295c915_3
  16. gmpy2 pkgs/main/linux-64::gmpy2-2.1.2-py39heeb90bb_0
  17. intel-openmp pkgs/main/linux-64::intel-openmp-2023.1.0-hdb19cb5_46306
  18. jinja2 pkgs/main/linux-64::jinja2-3.1.3-py39h06a4308_0
  19. ld_impl_linux-64 pkgs/main/linux-64::ld_impl_linux-64-2.38-h1181459_1
  20. libffi pkgs/main/linux-64::libffi-3.4.4-h6a678d5_0
  21. libgcc-ng pkgs/main/linux-64::libgcc-ng-11.2.0-h1234567_1
  22. libgomp pkgs/main/linux-64::libgomp-11.2.0-h1234567_1
  23. libprotobuf pkgs/main/linux-64::libprotobuf-3.20.3-he621ea3_0
  24. libstdcxx-ng pkgs/main/linux-64::libstdcxx-ng-11.2.0-h1234567_1
  25. magma pkgs/main/linux-64::magma-2.7.1-h2c23e93_0
  26. markupsafe pkgs/main/linux-64::markupsafe-2.1.3-py39h5eee18b_0
  27. mkl pkgs/main/linux-64::mkl-2023.1.0-h213fc3f_46344
  28. mkl-service pkgs/main/linux-64::mkl-service-2.4.0-py39h5eee18b_1
  29. mkl_fft pkgs/main/linux-64::mkl_fft-1.3.8-py39h5eee18b_0
  30. mkl_random pkgs/main/linux-64::mkl_random-1.2.4-py39hdb19cb5_0
  31. mpc pkgs/main/linux-64::mpc-1.1.0-h10f8cd9_1
  32. mpfr pkgs/main/linux-64::mpfr-4.0.2-hb69a4c5_1
  33. mpmath pkgs/main/linux-64::mpmath-1.3.0-py39h06a4308_0
  34. ncurses pkgs/main/linux-64::ncurses-6.4-h6a678d5_0
  35. networkx pkgs/main/linux-64::networkx-3.1-py39h06a4308_0
  36. ninja pkgs/main/linux-64::ninja-1.10.2-h06a4308_5
  37. ninja-base pkgs/main/linux-64::ninja-base-1.10.2-hd09550d_5
  38. numpy pkgs/main/linux-64::numpy-1.26.3-py39h5f9d8c6_0
  39. numpy-base pkgs/main/linux-64::numpy-base-1.26.3-py39hb5e798b_0
  40. openssl pkgs/main/linux-64::openssl-3.0.13-h7f8727e_0
  41. pip pkgs/main/linux-64::pip-23.3.1-py39h06a4308_0
  42. pycparser pkgs/main/noarch::pycparser-2.21-pyhd3eb1b0_0
  43. python pkgs/main/linux-64::python-3.9.18-h955ad1f_0
  44. pytorch pkgs/main/linux-64::pytorch-2.0.1-gpu_cuda118py39he342708_0
  45. readline pkgs/main/linux-64::readline-8.2-h5eee18b_0
  46. setuptools pkgs/main/linux-64::setuptools-68.2.2-py39h06a4308_0
  47. sqlite pkgs/main/linux-64::sqlite-3.41.2-h5eee18b_0
  48. sympy pkgs/main/linux-64::sympy-1.12-py39h06a4308_0
  49. tbb pkgs/main/linux-64::tbb-2021.8.0-hdb19cb5_0
  50. tk pkgs/main/linux-64::tk-8.6.12-h1ccaba5_0
  51. typing-extensions pkgs/main/linux-64::typing-extensions-4.9.0-py39h06a4308_1
  52. typing_extensions pkgs/main/linux-64::typing_extensions-4.9.0-py39h06a4308_1
  53. tzdata pkgs/main/noarch::tzdata-2023d-h04d1e81_0
  54. wheel pkgs/main/linux-64::wheel-0.41.2-py39h06a4308_0
  55. xz pkgs/main/linux-64::xz-5.4.5-h5eee18b_0
  56. zlib pkgs/main/linux-64::zlib-1.2.13-h5eee18b_0
  57. Proceed ([y]/n)?

安装时,从提示安装的依赖我们可以看出,这个版本确实带上了cuda相关包

  1. cudatoolkit

  1. cudnn

,这次安装显然时间长了很多,包的大小也近

  1. 2G

安装完成后,我们再次确认一下cuda加速是否可用;

同样,我们先安装一个

  1. ipython
  1. $ conda install ipython

进入

  1. ipython

后,依次执行如下代码

  1. import torch

  1. torch.cuda.is_available()
  1. $ ipython
  2. Python 3.9.18 (main, Sep 112023, 13:41:44)
  3. Type 'copyright', 'credits' or 'license'formore information
  4. IPython 8.15.0 -- An enhanced Interactive Python. Type '?'for help.
  5. In [1]: import torch
  6. In [2]: torch.cuda.is_available()
  7. Out[2]: True

这次cuda可以正常工作,到此完成gpu版本的pytorch安装

当然不是采用conda安装的话,希望自己手鲁,从nvidia驱动、cuda、cudnn这些库开始手动一个个安装好后,最后再安装pytorch也是可以的;关于pytorch手动安装方式,这里给出官方地址,点这里

最后再安装个jupyter工具

  1. conda install jupyter

启动命令

  1. jupyter-notebook

本文转载自: https://blog.csdn.net/youlinhuanyan/article/details/136109857
版权归原作者 丰色木夕 所有, 如有侵权,请联系我们删除。

“ubuntu22.04下使用conda安装pytorch(cpu及gpu版本)”的评论:

还没有评论