Skip to content

Instantly share code, notes, and snippets.

@oneleo
Last active March 5, 2019 10:10
Show Gist options
  • Save oneleo/8a62bf483d46ee47c1a34b8c8fbea397 to your computer and use it in GitHub Desktop.
Save oneleo/8a62bf483d46ee47c1a34b8c8fbea397 to your computer and use it in GitHub Desktop.
Install Tensorflow/Keras in Ubuntu 18.04
# 在 Ubuntu 18 安裝 Tensorflow/Keras 開發環境
## 安裝 nVidia 顯示卡驅動程式
```
$ sudo apt-key adv --fetch-keys http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/7fa2af80.pub
$ sudo bash -c 'echo "deb http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 /" > /etc/apt/sources.list.d/cuda.list'
$ sudo add-apt-repository ppa:graphics-drivers/ppa
$ sudo apt-get update
$ sudo apt-get -y install nvidia-driver-415 htop git
```
## 測試 nVidia 驅動程式安裝完成
```
$ sudo reboot # 重新開機
$ nvidia-smi
$ htop
```
## 安裝 CUDA Toolkit
```
$ cd /tmp && wget ftp://10.131.28.168/Public/cuda_10.0.130_410.48_linux
$ sudo chmod +x cuda_10.0.130_410.48_linux
$ sudo ./cuda_10.0.130_410.48_linux
```
### cuda_10.0.130_410.48_linux 的安裝流程(注意:安裝的過程中不要再安裝 nVidia Driver):
```
Do you accept the previously read EULA?
accept/decline/quit: accept →【ENTER】
Install NVIDIA Accelerated Graphics Driver for Linux-x86_64 410.48?
(y)es/(n)o/(q)uit: n →【ENTER】
Install the CUDA 10.0 Toolkit?
(y)es/(n)o/(q)uit: y →【ENTER】
Enter Toolkit Location
[ default is /usr/local/cuda-10.0 ]:【ENTER】
Do you want to install a symbolic link at /usr/local/cuda?
(y)es/(n)o/(q)uit: y →【ENTER】
Install the CUDA 10.0 Samples?
(y)es/(n)o/(q)uit: y →【ENTER】
Enter CUDA Samples Location
[ default is /home/chtti ]:【ENTER】
```
## 刪除用不到的 cuda_10.0.130_410.48_linux 安裝檔
```
$ sudo rm -rf cuda_10.0.130_410.48_linux
```
## 安裝 CUDNN
```
$ cd /tmp && wget ftp://10.131.28.168/Public/cudnn-10.0-linux-x64-v7.5.0.56.tgz
$ tar -zxvf cudnn-10.0-linux-x64-v7.5.0.56.tgz
$ 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 /usr/local/cuda/lib64/libcudnn*
$ sudo rm -rf cuda cudnn-10.0-linux-x64-v7.5.0.56.tgz
```
## 設定環境變數
```
$ vim ~/.bashrc
```
### 在 ~/.bashrc 檔最下方加入以下內容
```
(上略)
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64:/usr/local/cuda/extras/CUPTI/lib64"
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:/usr/local/cuda/bin
```
## 測試環境變數是否有設定完成
```
$ source ~/.bashrc
$ nvcc -V
```
## 安裝 Anaconda
```
$ cd /tmp && wget ftp://10.131.28.168/Public/Anaconda3-2018.12-Linux-x86_64.sh
$ chmod +x Anaconda3-2018.12-Linux-x86_64.sh
$ ./Anaconda3-2018.12-Linux-x86_64.sh
```
### Anaconda3-2018.12-Linux-x86_64.sh 的安裝流程:
```
Please, press ENTER to continue
>>>【ENTER】
Do you accept the license terms? [yes|no]
[no] >>> yes →【ENTER】
Anaconda3 will now be installed into this location: /home/chtti/anaconda3
- Press ENTER to confirm the location
- Press CTRL-C to abort the installation
- Or specify a different location below
[/home/chtti/anaconda3] >>>
Do you wish the installer to initialize Anaconda3 in your /home/chtti/.bashrc ? [yes|no]
[no] >>> yes →【ENTER】
Do you wish to proceed with the installation of Microsoft VSCode? [yes|no]
>>> yes →【ENTER】
```
## 刪除用不到的 Anaconda3-2018.12-Linux-x86_64.sh 安裝檔
```
$ sudo rm -rf cuda Anaconda3-2018.12-Linux-x86_64.sh
```
## 測試 Anaconda 是否有安裝完成
```
$ source ~/.bashrc
$ conda
```
## 使用 conda 指令建立 Tensorflow 開發環境(名為 tensorflow217)
```
$ conda create -n tensorflow217 anaconda nb_conda # Proceed ([y]/n)? y →【ENTER】
$ source activate tensorflow217
```
## 在 tensorflow217 開發環境安裝 tensorflow、opencv、keras
```
$ pip install tensorflow-gpu
$ pip install opencv-python
$ pip install keras
```
## 測試 tensorflow217 開發環境可正確執行 tensorflow
```
$ cd /tmp && git clone https://github.com/tensorflow/benchmarks.git
$ cd benchmarks/
$ git checkout cnn_tf_v1.12_compatible
$ python scripts/tf_cnn_benchmarks/tf_cnn_benchmarks.py --num_gpus=1 --model resnet50 --batch_size=16
```
## 在 tensorflow217 開發環境中開啟 jupyter 網頁開發界面(工作目錄在 ~/workspace)
```
$ mkdir -p ~/workspace && cd ~/workspace
$ jupyter notebook
```
## 刪除名為 tensorflow217 開發環境
```
source deactivate
conda-env remove -n tensorflow217 # Proceed ([y]/n)? y →【ENTER】
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment