linux从零开始安装nvidia驱动和tensorflow

来源:互联网 发布:软件技术服务包括什么 编辑:程序博客网 时间:2024/06/07 09:34

安装nvidia驱动和CUDA

  1. 下载驱动和CUDA安装包,在官网下载对应版本就行
  2. sudo apt-get install linux-headers-$(uname -r) 或者 linux-headers-generic.否则直接安装会报错 kernel not found
  3. 安装 nvidia 驱动,一路accept和yes
  4. 安装 CUDA,一路yes。安装路径:/usr/local/cuda-8.0/。是否安装推荐的驱动, no
    最后显示类似下面Summary内容,表示安装成功。
  5. 在/etc/profile中添加:
export PATH=/usr/local/cuda-8.0/lib64:$PATHexport LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH

保存后

# source /etc/profile# nvcc -V  检查CUDA# apt-get install cmake 安装cmake# cd  /usr/local/cuda-8.0/samples# make  测试CUDA

测试时间较长,一段没有error即可中止

============ Summary ============Driver:   Not SelectedToolkit:  Installed in /usr/local/cuda-8.0Samples:  Installed in /storage/installers/cuda_samples, but missing recommended librariesPlease make sure that -   PATH includes /usr/local/cuda-8.0/bin -   LD_LIBRARY_PATH includes /usr/local/cuda-8.0/lib64, or, add /usr/local/cuda-8.0/lib64 to /etc/ld.so.conf and run ldconfig as rootTo uninstall the CUDA Toolkit, run the uninstall script in /usr/local/cuda-8.0/binPlease see CUDA_Installation_Guide_Linux.pdf in /usr/local/cuda-8.0/doc/pdf for detailed information on setting up CUDA.***WARNING: Incomplete installation! This installation did not install the CUDA Driver. A driver of version at least 361.00 is required for CUDA 8.0 functionality to work.To install the driver using this installer, run the following command, replacing <CudaInstaller> with the name of this run file:    sudo <CudaInstaller>.run -silent -driverLogfile is /tmp/cuda_install_16018.log

安装CUDNN

  1. 下载cudnn-8.0-linux-x64-v5.1.tgz
  2. tar -xvzf cudnn-8.0-linux-x64-v5.1.tgz 解压完会有一个cuda文件夹
# cd cuda# cp  include/cudnn.h /usr/local/cuda/include# cp  lib64/libcudnn.*   /usr/local/cuda/lib64

cuDNN安装完成!
有的博客说要建立软链接,但是我没有做,步骤如下:

# cd  /usr/local/cuda/lib64 # rm  -rf  libcudnn.so  libcudnn.so.5# ln  -s  libcudnn.5.1.3  libcudnn.so.5# ln  -s  libcudnn.so.5  libcudnn.so

首先安装anaconda, 因为很多python库都包含在里面了,一次性安装很方便

安装anaconda

  1. 从官网下载最新的anaconda安装包,我下的是Anaconda2-4.2.0
  2. bash Anaconda2-4.2.0-Linux-x86_64.sh
  3. PREFIX=/usr/share/anaconda2
  4. # vim /etc/profile (添加环境变量)
    export PATH=$PATH:/usr/share/anaconda2/bin
    再source /etc/profile生效
    修改镜像文件,使得系统默认python为anaconda中的python
# mv /usr/bin/python  /usr/bin/python_bk# ln -s /usr/share/anaconda2/bin/python  /usr/bin/python

安装h5py

conda install h5py #注意必须先安装anaconda2
这时会提示升级anaconda,yes即可

安装tensorflow

ln -s /usr/anaconda2/bin/pip /usr/bin/pip 建立软连接
github下载 tensorflow_gpu-0.12.0rc0-cp27-none-linux-x86_64.whl
pip install tensorflow_gpu-0.12.0rc0-cp27-none-linux-x86_64.whl
安装完成。测试:
python && import tensorflow 测试tensorflow
或者:

# cd  /usr/share/anaconda2/lib/python2.7/site-packages/tensorflow/models/image/mnist# CUDA_VISIBLE_DEVICES = 0(选择显卡)  python convolutional.py

开头出现以下字样表示安装成功:

Python 2.7.12 |Anaconda 4.2.0 (64-bit)| (default, Jul  2 2016, 17:42:40) [GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux2Type "help", "copyright", "credits" or "license" for more information.Anaconda is brought to you by Continuum Analytics.Please check out: http://continuum.io/thanks and https://anaconda.org>>> import tensorflow as tfI tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.so locallyI tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.so locallyI tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.so locallyI tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.so.1 locallyI tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.so locally