Installing Pycharm, Python Tensorflow, Cuda and cudnn in Ubuntu 16.04

The version compatibility across the OS and these packages is a nightmare for every new person who tries to use Tensorflow.? In here, I record the successful procedure to install everything listed in the title of this note.

First of all, make sure that you have the?exact same version?of software in every steps, or it may not work.? To install Tensorflow at Ubuntu, you will need to install Ubuntu 16.04. ?Run?

lsb_release -a?

to check the version of your Ubuntu.? If you want to dual-boost OS from window10, like what I did, there is another note teach you how to do the dual-boost.? The following passages assume you have already had your Ubuntu 16.04 installed correctly.


Install pycharm

ref:?https://itsfoss.com/install-pycharm-ubuntu/

Open a terminal and use the following commands:

sudo add-apt-repository ppa:mystic-mirage/pycharmsudo apt-get update

To install the community edition of PyCharm, use the command below. It will download data of around 120 MB.

sudo apt-get install?pycharm-community

To install the professional edition of PyCharm, use the command below. It will download data of around 160 MB.

sudo apt-get install pycharm

Remove PyCharm

To uninstall PyCharm installed by the above described method, use the command below to uninstall the community edition of PyCharm:

sudo apt-get remove?pycharm-community

To remove the professional version, use the command below:

sudo apt-get remove pycharm

Afterward, use the command below to remove the PPA from the sources list:

sudo add-apt-repository --remove ppa:mystic-mirage/pycharm

That’s all you need to do. I hope this tutorial helped you toinstall PyCharm in Ubuntu Linux. Any questions or suggestions are always welcomed.


Install Python2.7

ref: https://askubuntu.com/questions/101591/how-do-i-install-the-latest-python-2-7-x-or-3-x-on-ubuntu

sudo add-apt-repository ppa:fkrull/deadsnakes

sudo apt-get update

sudo apt-get install python2.7


Install pip

Open Pycharm that you just installed.? Create a project.? Use Alt + F12or View -> Tool Windowto open the terminal.

Then follow the instructions in here:?https://www.rosehosting.com/blog/how-to-install-pip-on-ubuntu-16-04/

1. Connect to SSH and Update your System Software

First of all,?connect to your server via SSH and make sure that all your system software is up to date. Run the following command to update the package list and upgrade all your system software to the latest version available:

sudo apt-get update && sudo apt-get -y upgrade

2. Install Pip on Ubuntu 16.04

Once the upgrade is completed, you can move on and install Pip on your Ubuntu VPS. The installation of Pip is very simple. The only thing you need to do is to run the following command:

sudo apt-get install python-pip

3. Verify the Pip Installation on Ubuntu 16.04

The apt package manager will install Pip and all the dependencies required for the software to work optimally. Once the installation is completed you can verify that it was successful by using the following command:

pip -V

You should see something similar to the following:

# pip -V

pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

That means Pip has been successfully installed on your Ubuntu server and it is ready to use.


Install numpy, tdpm, pillow, scrip etc...

Go to?File -> Settings

In Project: -> Project interpreter, make sure you have a python2.7 selected.? Click the green plus sign at the right.? Then search whatever the package you need to install and click?Install Package?button at the bottom of the new page you just opened to mount it.

Caution: To run tensorflow using GPU, better to do not install it from pycharm.? See the UPDATE below how to install it.


Installing CUDA Toolkit 8.0 on Ubuntu 16.04

ref:?http://www.pradeepadiga.me/blog/2017/03/22/installing-cuda-toolkit-8-0-on-ubuntu-16-04/

GCC

One of them is to ensure where GCC is installed or not. We can confirm it by executing the following command.

gcc --version

Since I am using Ubuntu, GCC comes pre-installed and here is the output that I got.

build essentials

It is important have the build-essential?package installed. This is usually pre-installed on Ubuntu, however if it is not you can install it by executing the following command.

sudo apt-get install build-essential

On my laptop it was already installed hence I got the following output.

Download CUDA package from NVIDIA website

Navigate to?https://developer.nvidia.com/cuda-downloads?and download the appropriate package.

Once the page is successfully downloaded, we need to install the package. First navigate to the folder where the package is located. In my case it is under?~/Downloads/CUDA$?folder. Then issue the following command which installs the package.

sudo dpkg -i cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64.deb

Then update the package list from the repositories using the below command.

sudo apt-get update

Then install CUDA by executing the following command.

sudo apt-get install cuda

After a couple of minutes the installation would succeed and you should a screen similar to the following.

One of the important post installation steps is to update the PATH variable to include the CUDA binaries folder. To update it, we need to edit the file?/etc/environment. I use the?nano?text editor in this post, so the command would be

sudo nano /etc/environment

Once?nano?is open edit the PATH variable to include?/usr/local/cuda-8.0/bin?folder. After editing the file screen would look like this.

After editing this line press?Ctrl + X?to exit the editor and press?Y?when prompted whether you want to save it.

This method of editing the PATH variable usually requires a reboot to take effect. However executing the below command would update the PATH variable immediately.

source /etc/environment

Now we are ready to validate the CUDA installation. Just execute the following command in the terminal.

nvcc --version

If the installation was successful, we should see the CUDA compiler version as seen in this screenshot.

Alternatively you can also execute the following command in the terminal. This gives more detailed information about the drivers.

nvidia-smi

We are now ready to enjoy the goodness of CUDA and can continue with the installation of TensorFlow. Stay tuned for the installation instructions of TensorFlow.

注意:如果你看到CUDA下載頁(yè)下載的東西大小和版本都不一樣了的話,說(shuō)明CUDA官方更新了剂跟。 那么繼續(xù)按照下載頁(yè)下面的幾行命令執(zhí)行就可以了坏瞄。

如果你全都安裝完了诈皿,在運(yùn)行nvidia-smi的時(shí)候收到這么個(gè)錯(cuò)誤消息:

”NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver“

的話檀夹,重啟一下電腦在運(yùn)行nvidia-smi就可以了张肾。


Install cudnn v5.1 (you may require for cudnn v6.0 too, see the update below)

ref: https://medium.com/@ikekramer/installing-cuda-8-0-and-cudnn-5-1-on-ubuntu-16-04-6b9f284f6e77

Download cudnn @https://developer.nvidia.com/rdp/cudnn-download

如果你安裝的CUDA已經(jīng)不是8.0了的話速勇,沒(méi)關(guān)系。注冊(cè)登錄上面的網(wǎng)址凛捏,它會(huì)給你寫出什么版本的cudnn搭配什么版本的CUDA担忧。對(duì)號(hào)下載就行了。

My os is Ubuntu 16.04.? amd64.? The debian archives for Ubuntu 16.04 Power8 doesn't apply to my os.? So I have to download the tar file for cuDNN v5.1 Library for Linux.? (This is under the testing.? If it doesn't work.? I have to change os to Ubuntu 14.04 cuz there are debian archives for amd64 version)

Next you need to uncompress and copy cuDNN to the toolkit directory. The toolkit default install location is /usr/local/cuda

tar xvzf cudnn-8.0-linux-x64-v5.1-ga.tgz

sudo cp -P cuda/include/cudnn.h /usr/local/cuda/include

sudo cp -P cuda/lib64/libcudnn* /usr/local/cuda/lib64

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

Now you need to update your bash file

nano ~/.bashrc

With the text editor open, scroll to the bottom and put in these lines:

export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"

export CUDA_HOME=/usr/local/cuda

Save and close it.


UPDATE:

What to check?

To run gpu using tensorflow under Pycharm IDE, here is a checklist:

1, test whether CUDA and cudnn run correctly.

2. test whether CUDA could pick up your GPU device.

3, make sure tensorflow from Pycharm can see Cuda and cudnn

Solutions:

TO "1",?install cuda and cudnn in the correct version as above says. Use?nvcc -V?and?nvidia-smi?command to examine the driver and installation.

Run sample code to test whether cuda works correctly

ref:http://xcat-docs.readthedocs.io/en/stable/advanced/gpu/nvidia/verify_cuda_install.html

-> Go to the dir that installed cuda (mine is/usr/local/cuda/samples)

-> runmake, it might take few minutes to make the files.

-> rundeviceQueryandbandwidthTest. They are at:

./bin/ppc64le/linux/release/deviceQuery

./bin/ppc64le/linux/release/bandwidthTest

if both passes, then cuda should work.


TO "2", run the testing code provided from the official site of Tensorflow:

ref:https://www.tensorflow.org/tutorials/using_gpu

write down the code below and run through terminal:

import tensorflow as tf

# Creates a graph.

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')

b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')

c = tf.matmul(a, b)

# Creates a session with log_device_placement set to True.

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

# Runs the op.

print(sess.run(c))

Save as a .py file and run python ./${file_full_path}

You should see the following output:

Device mapping:

/job:localhost/replica:0/task:0/device:GPU:0 -> device: 0, name: Tesla K40c, pci bus

id: 0000:05:00.0

b: /job:localhost/replica:0/task:0/device:GPU:0

a: /job:localhost/replica:0/task:0/device:GPU:0

MatMul: /job:localhost/replica:0/task:0/device:GPU:0

[[ 22. ?28.]

[ 49. ?64.]]

If it shows?device mapping: no known devices, then unintall your?tensorflow?and install?tensorflow-gpu.? See this:

ref:https://stackoverflow.com/questions/42326748/tensorflow-on-gpu-no-known-devices-despite-cudas-devicequery-returning-a-pas

Package?tensorflow?is likely a CPU version.? To use gpu to run CUDA, you need to install?tensorflow-gpu.? However, since installing?tensorflow-gpu will reinstall?tensorflow automatically, it'd be wise to manually uninstall all the tensorflow-related package, then install?tensorflow-gpu.

In my case:

pip uninstall tensorflow

is not enough. Because when reinstall with:

pip install tensorflow-gpu

It's still reinstall tensorflow with cpu not gpu. So, before install tensorfow-pgu, I tried to remove all related tensor folders in site-packages uninstall protobuf, and it works!

For conclusion:

pip uninstall tensorflow

Remove all tensor folders in ~/Python35/Lib/site-packages

pip uninstall protobuf

pip install tensorflow-gpu

If the commandpip uninstall tensorflow?results in some kind of error and failed in uninstall, then we can remove the package from pycharm.? Go File-> settings-> project interpreter->minus sign to uninstall.? Also check the fold from terminal whether they are still there.?/usr/local/lib/python2.7/dist-packages/?and /usr/local/lib/python2.7/site-packages/.? List of folders need to remove:

tensorflow,tensorboard,anything starts from?tensor, and?protobuf.

Open?NVIDIA X Server Settings.? At GPU-0 (your gpu name) tab, monitor your GPU Utilization.? If you see it rapidly goes up in a very short period of time, it means your tensorflow runs on gpu correctly.

If you see error message occurs while you import tensorflow like:

ImportError: libcudnn.so.5: cannot open shared object file: No such file or directory

or

ImportError: libcudnn.so.6: cannot open shared object file: No such file or directory

it means it couldn't find the corresponding version of cudnn library.? It might trying to load cudnn v6.? If so,Download cuDNN v6.0 (April 27, 2017), for CUDA 8.0, untar and place the files to the corresponding right places as above (since cudnn v5.1 and v6.0 can be exist at the same time, you don't have to delete the old copied files from v5.1) and run the tensorflow code again.? It should be fixed.

TO "3",? it is possible that everything works when you run through terminal, but it suddenly breaks when you run through pycharm although you have already added LD_LIBRARY_PATH?and CUDA_HOME to ~/bashrc file.? It is because pycharm has its own environment variable.? Running in pycharm, we need to set?CUDA_HOME and LD_LIBRARY_PATH again probably for each individual project.

To do that, go to the Run menu->edit configurations then choose your project. Then you click on the Environment Variables and add an entry for CUDA_HOME?and LD_LIBRARY_PATH.

ref:https://stackoverflow.com/questions/43691706/pycharm-tensorflow-importerror-but-works-fine-with-terminal

Up to now, your configuration of cuda and tensorflow should be done.? Make sure you have enabled to use gpu in your code, then everything should work.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末坯癣,一起剝皮案震驚了整個(gè)濱河市瓶盛,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌示罗,老刑警劉巖惩猫,帶你破解...
    沈念sama閱讀 211,042評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異蚜点,居然都是意外死亡轧房,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門绍绘,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)奶镶,“玉大人,你說(shuō)我怎么就攤上這事陪拘〕д颍” “怎么了?”我有些...
    開封第一講書人閱讀 156,674評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵左刽,是天一觀的道長(zhǎng)捺信。 經(jīng)常有香客問(wèn)我,道長(zhǎng)欠痴,這世上最難降的妖魔是什么迄靠? 我笑而不...
    開封第一講書人閱讀 56,340評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮斋否,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘拭荤。我一直安慰自己茵臭,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評(píng)論 5 384
  • 文/花漫 我一把揭開白布舅世。 她就那樣靜靜地躺著旦委,像睡著了一般。 火紅的嫁衣襯著肌膚如雪雏亚。 梳的紋絲不亂的頭發(fā)上缨硝,一...
    開封第一講書人閱讀 49,749評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音罢低,去河邊找鬼查辩。 笑死胖笛,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的宜岛。 我是一名探鬼主播长踊,決...
    沈念sama閱讀 38,902評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼萍倡!你這毒婦竟也來(lái)了身弊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 37,662評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤列敲,失蹤者是張志新(化名)和其女友劉穎阱佛,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體戴而,經(jīng)...
    沈念sama閱讀 44,110評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡凑术,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了填硕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片麦萤。...
    茶點(diǎn)故事閱讀 38,577評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖扁眯,靈堂內(nèi)的尸體忽然破棺而出壮莹,到底是詐尸還是另有隱情,我是刑警寧澤姻檀,帶...
    沈念sama閱讀 34,258評(píng)論 4 328
  • 正文 年R本政府宣布命满,位于F島的核電站,受9級(jí)特大地震影響绣版,放射性物質(zhì)發(fā)生泄漏胶台。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評(píng)論 3 312
  • 文/蒙蒙 一杂抽、第九天 我趴在偏房一處隱蔽的房頂上張望诈唬。 院中可真熱鬧,春花似錦缩麸、人聲如沸铸磅。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)阅仔。三九已至,卻和暖如春弧械,著一層夾襖步出監(jiān)牢的瞬間八酒,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評(píng)論 1 264
  • 我被黑心中介騙來(lái)泰國(guó)打工刃唐, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留羞迷,地道東北人界轩。 一個(gè)月前我還...
    沈念sama閱讀 46,271評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像闭树,于是被迫代替她去往敵國(guó)和親耸棒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容