Installation Caffe + NVIDIA CUDA 7 Tools on Ubuntu 14.04 (step by step, including external libraries)
Guide for Ubuntu 16.04 with a 64 bit processor.
Tested on a Dell XPS L421x laptop with a NVIDIA GeForce GT 630m.
This guide is intended to be a (sort of) comprehensive step by step tutorial, including the installation of external dependencies.
This tutorial assumes some decisions about the versions and programs to install, to make it as straightforward to follow as possible, while still having as much advanced features as possible.
Notably:
It uses Anaconda Python (which facilitates installation and has many great features).
It uses OpenBLAS (which presumably increases performance over ATLAS).
The "code formatted" sections should be run in a terminal.
This guide was based on the guides in:
Caffe: General, Caffe installation.
Caffe: Ubuntu prerequisites.
NVIDIA: CUDA Installation (more complete guide).
Other resources as StackOverflow, Caffe-users group, forums, etc.
1. Install Git:
```bash sudo apt-get install git ```2. NVIDIA drivers:
Find your graphics card model: go to System Settings -> Software and Updates -> Additional Drivers, and find out which Nvidia binary driver is displayed. Or:
```bash lspci | grep -i nvidia ```Go to the NVIDIA website and find the latest driver version compatible with your graphics card, if you have a NVIDIA GeForce, go to: http://www.geforce.com/drivers
Check the latest version compiled in "Proprietary GPU Drivers" PPA which also matches your graphics card in: https://launchpad.net/~graphics-drivers/+archive/ubuntu/ppa. For stability, you may want to use the recommended driver in that page.
3. Add the "Proprietary GPU Drivers" PPA repository:
```bash sudo add-apt-repository ppa:graphics-drivers/ppa ```4. Update the repositories database:
```bash sudo apt-get update ```5. Install your selected version of the graphics card:
This could break your graphic system, be sure to know how to use a shell to be able to repair it), for example:
```bash sudo apt-get install nvidia-340 ```6. Restart your system:
```bash sudo shutdown -r now ```7. Check the NVIDIA drivers installed:
```bash cat /proc/driver/nvidia/version ```8. Check that your computer is CUDA enabled: You should have a compatible NVIDIA graphics card):
lspci | grep -i nvidia
You need to have an account or register in the NVIDIA developer program (it's free): https://developer.nvidia.com/user
9. Download CUDA 7 from: https://developer.nvidia.com/cuda-downloads, you can download the "deb (network)" package (cuda repo).
Go to the downloads directory:
cd ~/Downloads
10. Install the package (the repository):
sudo dpkg -i cuda-repo-ubuntu1404*amd64.deb
Update the repositories DB:
sudo apt-get update
11. Install CUDA (it should list cuda packages):
sudo apt-get install cuda
12. Add CUDA to the PATH variable: (and make it permanent for the current user):
echo 'export PATH=/usr/local/cuda/bin:$PATH' >> ~/.bashrc
13. Add CUDA to the LD_LIBRARY_PATH variable: (and make it permanent for the current user):
echo 'export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
Log out and log in again to activate the new variables (close and open the terminal).
14. Check CUDA installation:
To check the correct installation running the samples, install the samples in a directory under your home:
cuda-install-samples-7.0.sh ~/cuda-samples
You can check the version of the NVIDIA drivers installed with:
cat /proc/driver/nvidia/version
You can check the version of CUDA installed with:
nvcc -V
Go to the samples directory:
cd ~/cuda-samples/NVIDIA*Samples
Build the samples:
make -j $(($(nproc) + 1))
Note: you could just run make, but that last part (-j $(($(nproc) + 1))) executes the make command in parallel, using the number of cores in your machine (plus one), so the compilation would finish faster.
Restart your computer:
sudo shutdown -r now
Check the CUDA installation, run:
bin/x86_64/linux/release/deviceQuery
Check that it detects a CUDA device.
Check that it detects your graphics card.
Check at the end that it says that the test passed.
Run the bandwidth test to check that the system and the CUDA device are capable to communicate:
bin/x86_64/linux/release/bandwidthTest
15. Install OpenBLAS:
Note: you could just use "ATLAS" with:
sudo apt-get install libatlas-base-dev
But you would probably get a better performance from OpenBLAS following the next instructions, and it's quite straightforward.
16. Install Fortran compiler:
sudo apt-get install gfortran
17. Create a "code" directory to store your code:
Note: you can put your code wherever you want, so, modify this part as you wish, but I will assume that your are doing it.
mkdir ~/code
Enter that directory:
cd ~/code
18. Clone OpenBLAS:
git clone https://github.com/xianyi/OpenBLAS.git
Go to the OpenBLAS directory:
cd OpenBLAS
19. Compile OpenBLAS:
make -j $(($(nproc) + 1))
Note: you could just run make, but that last part (-j $(($(nproc) + 1))) executes the make command in parallel, using the number of cores in your machine (plus one), so the compilation would finish faster.
20. Install OpenBLAS:
sudo make PREFIX=/usr/local install
21. Install Boost:
sudo apt-get install libboost-all-dev
22. Install OpenCV:
sudo apt-get install libopencv-dev
23.Install: protobuf, glog, gflags:
sudo apt-get install libprotobuf-dev libgoogle-glog-dev libgflags-dev protobuf-compiler
24. Install IO libraries: hdf5, leveldb, snappy, lmdb:
sudo apt-get install libhdf5-serial-dev libleveldb-dev libsnappy-dev liblmdb-dev
25. Install Anaconda Python:
Go to the Anaconda website and download Anaconda for Linux (you would probably want the 64x version): http://continuum.io/downloads
Go to the downloads directory:
cd ~/Downloads
Execute the installer:
bash Anaconda*.sh
When asked if add Anaconda to the PATH for the current user, type "yes".
Log out and log in again to activate the new variables (close and open the terminal).
26. Install HDF5:
Install HDF5 with conda, although it will only be used by Anaconda:
conda install hdf5
27.Configure the HDF5 version:
Note: this is a quick fix for a minor bug / issue with the version of libhdf5. If you know a better / proper way to solve it, let me know. If this section doesn't apply to you, omit it.
Go to the libraries directory:
cd /usr/lib/x86_64-linux-gnu
Link the system version of HDF5 from 7 to 9:
sudo ln -s libhdf5.so.7 libhdf5.so.9
sudo ln -s libhdf5_hl.so.7 libhdf5_hl.so.9
Update the "Dynamic Linker":
sudo ldconfig
28. Install CuDNN:
Note: for this to work, you need a NVIDIA GPU based on the "Kepler" architeture (or the newer, "Maxwell" architecture).
Note: although this guide includes all the build, installation and configuration of CuDNN, only the build and installation was tested, the usage hasn't been tested. (It was only tested on a NVIDIA GT 525m and a NVIDIA GTX 580 which have a "Fermi" (older) architecture, and is not supported by CuDNN.
You need to have an account or register in the NVIDIA developer program (it's free): https://developer.nvidia.com/user
Using your account, download CuDNN: https://developer.nvidia.com/cuDNN
Go to the downloads directory:
cd ~/Downloads/
Uncompress the CuDNN download:
tar xvf cudnn*.tgz
Enter the uncompressed directory:
cd cuda
Copy the *.h files to your CUDA installation:
sudo cp */*.h /usr/local/cuda/include/
Copy the .so files to your CUDA installation:
sudo cp */*.so* /usr/local/cuda/lib64/
29. Configure and compile Caffe:
- Create a "code" directory to store your code:
Note: you can put your code wherever you want, so, modify this part as you wish, but I will assume that your are following the tutorial.
mkdir ~/code
Enter that directory:
cd ~/code
- Clone Caffe:
git clone https://github.com/BVLC/caffe.git
- Enter the new Caffe directory:
cd caffe
- Copy the Makefile.config:
cp Makefile.config.example Makefile.config
- Enable CuDNN, set the USE_CUDNN := 1 flag in Makefile.config:
Note: this will only work with a GPU with Kepler or newer architecture (Maxwell). This tutorial was tested on an older GPU, so this part was omitted.
sed -i 's/# USE_CUDNN := 1/USE_CUDNN := 1/' Makefile.config
- Enable OpenBLAS:
Note: if you didn't install OpenBLAS and installed ATLAS, omit this part.
sed -i 's/BLAS := atlas/BLAS := open/' Makefile.config
- Enable Anaconda:
sed -i 's|# ANACONDA_HOME :=
sed -i 's|# PYTHON_INCLUDE :=
sed -i 's|#
sed -i 's|# PYTHON_LIB :=
sed -i 's|# WITH_PYTHON_LAYER := 1|WITH_PYTHON_LAYER := 1|' Makefile.config
- Install Python dependencies using conda:
for req in $(cat python/requirements.txt); do conda install $req; done
- Install Python dependencies using pip (to install what couldn't be installed by conda):
pip install -r python/requirements.txt
- Remove bad Anaconda libm:
Note: this is a fix for a common bug with Anaconda. It may not apply to you.
mkdir ~/anaconda/lib/libm.orig.d
mv ~/anaconda/lib/libm.* ~/anaconda/lib/libm.orig.d
- Build Caffe:
make all -j $(($(nproc) + 1))
- Build the tests:
make test -j $(($(nproc) + 1))
- Run the tests:
make runtest -j $(($(nproc) + 1))
After running make runtest, check that all the tests passed.
- Build PyCaffe (Python interface):
make pycaffe -j $(($(nproc) + 1))
Note: you could just run make all or make test or make runtest or make pycaffe respectively, but that last part (-j $(($(nproc) + 1))) executes the make command in parallel, using the number of cores in your machine (plus one), so the compilation would finish faster.
Export a variable CAFFE_ROOT with your Caffe root directory and make it permanent:
echo "export CAFFE_ROOT=$(pwd)" >> ~/.bashrc
- Add Caffe to your PYTHONPATH variable using your CAFFE_ROOT:
Technical Note: you could add Caffe to your PYTHONPATH directly, but by using a CAFFE_ROOT variable, you could move your Caffe directory and would only have to modify your CAFFE_ROOT variable. And you could use the same to export Caffe binaries to your PATH variable.
echo 'export PYTHONPATH=$CAFFE_ROOT/python:$PYTHONPATH' >> ~/.bashrc
Log out and log in again to activate the new variables (close and open the terminal).
- To test that PyCaffe is working open IPython:
ipython
- Import "caffe":
import caffe
Check that there are no errors.
Congratulations, you have an up and running Caffe installation.
Now you may want to follow one of the tutorials:
LeNet MNIST Tutorial
ImageNet tutorial