ubuntu安装Anaconda和pytorch

技术·学习 · 2023-06-09 · 66 人浏览

ubuntu安装Anaconda和pytorch

安装Anaconda

  1. 下载Anaconda:免费下载 |蟒蛇 (anaconda.com)
  2. 给Anaconda执行权限

    • sudo chmod 777 Anaconda3-2023.03-1-Linux-x86_64.sh
  3. 安装Anaconda

    • ./Anaconda3-2023.03-1-Linux-x86_64.sh
    • 这些设置基本上可以一直按Enter键
    • 中间会让选择一个yes or no的问题,我们肯定直接选择yes
  4. 查看一下当前的环境

    • conda -V
  5. 修改环境变量

    • nano ~/.bashrc
    • 在最后添加

      • export PATH=/home/****/anaconda3/bin:$PATH
      • ****为用户名
  6. 更新一下环境变量

    • source ~/.bashrc
  7. 查看安装是否成功

    • conda -V
    • 可以看到Anaconda已经安装成功了

问题

1. ubuntu的anaconda无法激活环境

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

答:

  • conda init bash
  • source activate

安装pytorch等环境

  1. 进入官网查看对应版本

    • image-20230607163743107
  2. 安装

    • conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=11.3 -c pytorch
  3. 测试是否安装成功

    • #测试pytorch-gpu是否能用
      import torch
      flag = torch.cuda.is_available()
      print(flag)
      ngpu= 1
      # Decide which device we want to run on
      device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
      print('cuda设备名:',device)
      print('gpu名称:',torch.cuda.get_device_name(0))
      print('pytorch版本:',torch.__version__)
      print('cuda版本:',torch.version.cuda)
      print('cudnn版本号:',torch.backends.cudnn.version())
      print('定义一个torch格式的3*3的矩阵:',torch.rand(3,3).cuda()) 
Linux Ubuntu pytorch Anaconda
Theme Jasmine by Kent Liao