Ubuntu16.04环境下安装cuda10+pytorch1.2

1、背景

环境:Ubuntu16.04 & titanx
作为新一代的人工智能训练平台的PYTORCH,有其独特的优势,为此,完整的安装这一平台,是开展所有工作的首要条件,一开始,笔者认为应该轻松的完成,但是发现实际上要从零完成这一工作,尤其是在NVIDIA TITANX 下,是需要3~4天的摸索的,为了便于后续的工作,也为了后面使用的童鞋,写下这篇文章,闲话少说,先上最终完成shell输出。

xxx@xxxUbuntu:~$ sudo pip3 install torch-1.2.0-cp35-cp35m-manylinux1_x86_64.whl
[sudo] xxx 的密码: 
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
WARNING: The directory '/home/xxx/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Processing ./torch-1.2.0-cp35-cp35m-manylinux1_x86_64.whl
Collecting numpy
  Downloading numpy-1.18.4-cp35-cp35m-manylinux1_x86_64.whl (20.0 MB)
     |████████████████████████████████| 20.0 MB 19 kB/s 
Installing collected packages: numpy, torch
Successfully installed numpy-1.18.4 torch-1.2.0
xxx@xxxUbuntu:~$ sudo pip3 install torchvision-0.4.0-cp35-cp35m-manylinux1_x86_64.whl
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
WARNING: The directory '/home/xxx/.cache/pip' or its parent directory is not owned or is not writable by the current user. The cache has been disabled. Check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Processing ./torchvision-0.4.0-cp35-cp35m-manylinux1_x86_64.whl
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from torchvision==0.4.0) (1.10.0)
Collecting pillow>=4.1.1
  Downloading Pillow-7.1.2-cp35-cp35m-manylinux1_x86_64.whl (2.1 MB)
     |████████████████████████████████| 2.1 MB 6.6 kB/s 
Requirement already satisfied: numpy in /usr/local/lib/python3.5/dist-packages (from torchvision==0.4.0) (1.18.4)
Requirement already satisfied: torch==1.2.0 in /usr/local/lib/python3.5/dist-packages (from torchvision==0.4.0) (1.2.0)
Installing collected packages: pillow, torchvision
  Attempting uninstall: pillow
    Found existing installation: Pillow 3.1.2
    Uninstalling Pillow-3.1.2:
      Successfully uninstalled Pillow-3.1.2
Successfully installed pillow-7.1.2 torchvision-0.4.0
xxx@xxxUbuntu:~$ python3
Python 3.5.2 (default, Apr 16 2020, 17:47:17) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import torchvision
>>> print(torch.cuda.is_available())
True
>>> print(torch.__version__)
1.2.0
>>> 

2、安装UBUNTU16.04

为了最终安装成功PYTORCH,我起码进行了4次的UBUNTU的安装,其中碰到了多个问题,在此记录过程如下:
(1) 进入windows,删除原来安装的四个分区(/boot,swap, /,/home);
(2) 插入安装用的USB盘;
(3)安装类型选最后一项“其他选项”,这样可以自己手动分区,采用了以下参数
逻辑分区,4G,起始,Ext4日志文件系统,挂载点/boot;
逻辑分区,16G,起始,交换空间,无挂载点;
逻辑分区,50G,起始,Ext4日志文件系统,挂载点为 /;
逻辑分区,全部剩余空间,起始,Ext4日志文件系统,挂载点为/home。
(4)更新源,参考 WIN7系统安装UBUNTU16.04和QT5一文;
(5)安装完成WIN10引导被删,需要执行下面的命令:

sudo update-grub

至此,UBUNTU16.04安装基本完成。

3、安装NVIDIA 驱动

NVIDIA的驱动的安装重复了很多次,主要是和后续的CUDA,PYTORCH有相互匹配的问题。最终选定了NVIDIA-Linux-x86_64-430.64.run作为载体,这样符合CUDA10.0。安装的具体步骤如下:
(1)删除原有的驱动

sudo apt-get remove --purge nvidia*

(2)屏蔽nouveau,具体如下

sudo chmod 666/etc/modprobe.d/blacklist.conf
sudo gedit /etc/modprobe.d/blacklist.conf
blacklist vga16fb
blacklist nouveau
blacklist rivafb
blacklist rivatv
blacklist nvidiafb
sudo update-initramfs -u

(3) 最好获取版本的源码,更新下环境

uname -r
sudo apt-get install linux-source
sudo apt-get install linux-headers-4.10.0-28-generic

更新支持或编译库

 sudo apt-get update 
 sudo apt-get install dkms build-essential linux-headers-generic
 sudo apt-get install gcc-multilib xorg-dev
 sudo apt-get install freeglut3-dev libx11-dev libxmu-dev install libxi-dev  libgl1-mesa-glx libglu1-mesa libglu1-mesa-dev

(4) 安装NVIDIA-Linux-x86_64-430.64.run
按下CTRL+ALT+F1 退出图形模式,然后执行下面命令:

sudo service lightdm stop
cd /home/xxx/
sudo chmod a+x NVIDIA-Linux-x86_64-430.64.run
sudo ./NVIDIA-Linux-x86_64-430.64.run -no-x-check -no-nouveau-check -no-opengl-files 

安装过程中,驱动安装选否,其他选是即可。
(5)验证安装。

nvidia-smi

4、安装cuda10

cuda10先要下载下来,我使用的是cuda_10.0.130_410.48_linux.run,还有一个补丁包cuda_10.0.130.1_linux.run
执行下列命令:

sudo sh cuda_10.0.130_410.48_linux.run
sudo sh cuda_10.0.130.1_linux.run
sudo gedit /etc/profile
export PATH=/usr/local/cuda-10.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64:$LD_LIBRARY_PATH
source /etc/profile

最后通过 执行 nvcc -V 进行安装验证。

5、安装cudnn7.6

下载CUDA10.0相对应的CUDNN,我们下载了cudnn-10.0-linux-x64-v7.6.2.24.solitairetheme8,需要使用下面的命令处理:

cp cudnn-10.0-linux-x64-v7.6.2.24.solitairetheme8 cudnn-10.0-linux-x64-v7.6.2.24.tgz
tar -zxvf cudnn-10.0-linux-x64-v7.3.1.20.tgz
sudo cp cuda/include/cudnn.h /usr/local/cuda/include/
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/
cd /usr/local/cuda/lib64
sudo chmod +r libcudnn.so.7.6.2 
sudo ln -sf libcudnn.so.7.6.2 libcudnn.so.7 
sudo ln -sf libcudnn.so.7 libcudnn.so 
sudo ldconfig
ll #验证是否安装成功

6、安装pytorch1.2

pytorch安装最主要是对应相应的版本,我采用的是torch-1.2.0-cp35-cp35m-manylinux1_x86_64.whl和torchvision-0.4.0-cp35-cp35m-manylinux1_x86_64.whl,
首要条件,确认PIP3安装了
可以采用下列命令完成

sudo pip3 install torch-1.2.0-cp35-cp35m-manylinux1_x86_64.whl
sudo pip3 install torchvision-0.4.0-cp35-cp35m-manylinux1_x86_64.whl
python3
import torch
import torchvision
print(torch.cuda.is_available())
print(torch.__version__)

验证通过的话,pytorch v1.2 就安装成功了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值