Ubuntu下安装TensorFlow

本文介绍了在Ubuntu 16.04上安装TensorFlow的详细步骤,包括修改pip源、安装virtualenv、设置Python虚拟环境、安装CUDA和CuDNN,以及在PyCharm中配置TensorFlow。文章强调了在安装过程中需要注意选择Python版本,并提供了相应的命令行操作指南。
摘要由CSDN通过智能技术生成

本文是2017.1月份的时候安装的步骤, 现在可能有些已经不适用, 请大家注意鉴别.

修改pip源

在/HOME里创建一个文件(存在的话就直接修改,反正不管有没有都执行下面的代码, 不存在的话会自动创建).

cd $HOME  
mkdir .pip  
cd .pip  
sudo gedit pip.conf

在里面添加

[global]  
index-url = https://pypi.doubanio.com/simple/  
[install]  
trusted-host=pypi.doubanio.com  
disable-pip-version-check = true  
timeout = 6000    

安装virtualenv

Ubuntu 16.04: How to install OpenCV

目的是把Python安装进这个虚拟环境里, 从而不影响本机的Python环境

$ sudo pip install virtualenv virtualenvwrapper //装了两个东西
$ sudo rm -rf ~/get-pip.py ~/.cache/pip         //这句我没执行 可能是删除一些安装后的残留文件吧

Once we have virtualenv and virtualenvwrapper installed, we need to update our ~/.bashrc file to include the following lines at the bottom of the file:

# virtualenv and virtualenvwrapper
export WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

The ~/.bashrc file is simply a shell script that Bash runs whenever you launch a new terminal. You normally use this file to set various configurations. In this case, we are setting an environment variable called WORKON_HOME to point to the directory where our Python virtual environments live. We then load any necessary configurations from virtualenvwrapper .
To update your ~/.bashrc file simply use a standard text editor. I would recommend using nano , vim , or emacs . You can also use graphical editors as well, but if you’re just getting started, nano is likely the easiest to operate.

After editing our ~/.bashrc file, we need to reload the changes:
$ source ~/.bashrc

Note: Calling source on .bashrc only has to be done once for our current shell session. Anytime we open up a new terminal, the contents of .bashrc will be automatically executed (including our updates).

Now that we have installed virtualenv and virtualenvwrapper , the next step is to actually create the Python virtual environment — we do this using the mkvirtualenv command.

But before executing this command, you need to make a choice: Do you want to use Python 2.7 or Python 3?

The outcome of your choice will determine which command you run in the following section.

//Python2:
$ mkvirtualenv yourEnvName -p python2
//Python3:
$ mkvirtualenv yourEnvName -p python3

进入虚拟环境:
$ workon yourEnvName
退出虚拟环境:
$ deactivate

安装CUDA

见<安装mxnet.md>

安装CuDNN

Ubuntu 16.04 Cuda7.5/Cuda8.0 tensorflow-gpu

同学给我了’cudnn-8.0-linux-x64-v5.1.tgz’. 解压到/HOME里, 然后Terminal执行:

sudo cp cuda/include/cudnn.h /usr/local/cuda/include/    
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64/    
sudo chmod a+r /usr/local/cuda/include/cudnn.h    
sudo chmod a+r /usr/local/cuda/lib64/libcudnn*   

安装Tensorflow

这里我同学直接给我了’tensorflow_gpu-0.12.1-cp35-cp35m-linux_x86_64.whl’, 所以没有下.
下载的话: Github: tensorflow/tensorflow. 在’Installation’里选择对应的版本(注意选GPU, 不要CPU-only).

注意上面安装了virtualenv并创建了Python3的环境, 因此我想让tensorflow使用Python3的环境, 所以首先要进入虚拟环境:
workon yourEnvName

再在虚拟环境里安装Tensorflow:
pip install tensorflow_gpu-0.12.1-cp35-cp35m-linux_x86_64.whl

安装后测试:

import Tensorflow as tf
tf.InteractiveSession()

正常执行就成功了.

修改pycharm.sh

如果使用pycharm的话,进入pycharm根目录的bin/,以便使其能用的了TF.
打开pycharm.sh,然后在第180行左右插入:

LD_LIBRARY_PATH="/usr/local/cuda/lib64" #先找到下面  
LD_LIBRARY_PATH="$IDE_BIN_HOME:$LD_LIBRARY_PATH" "$JAVA_BIN"  #pycharm.py里本来就有.先找到这行, 在上面加上面那句.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值