Skip to content

Instantly share code, notes, and snippets.

@nikitametha
Last active August 29, 2023 02:25
  • Star 65 You must be signed in to star a gist
  • Fork 31 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save nikitametha/c54e1abecff7ab53896270509da80215 to your computer and use it in GitHub Desktop.
Installing Caffe on Ubuntu 16.04 and above (CPU ONLY, WITHOUT CUDA OR GPU SUPPORT)

This is a guide on how to install Caffe for Ubuntu 16.04 and above, without GPU support (No CUDA required).

Prerequisites:

OpenCV

sudo apt-get install libopencv-dev python-opencv

OpenBLAS OR Atlas

OpenBLAS

sudo apt-get install libopenblas-dev

Atlas

sudo apt-get install libatlas-base-dev

Boost

sudo apt-get install libboost-all-dev

Protobuf (USING PIP)

sudo pip install protobuf

If you don't have pip yet, install it using the following commands:
sudo apt-get install python-pip python-dev build-essential

sudo pip install --upgrade pip

General Dependencies

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler 

sudo apt-get install the python-dev 

sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev 

Getting Caffe

git clone https://github.com/BVLC/caffe

You will now find the caffe folder in your Home directory.

We have to make a copy of Makefile.config.example, which we generally name as, Makefile.config to which we can make changes based on our system settings.

cd caffe
cp Makefile.config.example Makefile.config

Making Changes In Makefile.config

Make sure you are still in the caffe directory, then use this command to open Makefile.config

gedit Makefile.config 
Note- The following line numbers may vary.
  • On line 8, uncomment CPU_ONLY := 1

  • On Line 21, uncomment OPENCV_VERSION := 3 if you're using OpenCV 3 or above. If you aren't sure, try this in another terminal

$ python
>>> import cv2
>>> cv2.__version__
'3.0.0'
  • On line 25, uncomment CUSTOM_CXX := g++

  • On line 50, set BLAS := open if you've installed OpenBLAS, or let it be the default BLAS := atlas if you've installed Atlas.

  • On line 94, change

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include

to

INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include  /usr/include/hdf5/serial 
  • On line 95, change
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib

to

LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib  /usr/lib/x86_64-linux-gnu/hdf5/serial

This should resolve hdf5 errors when running make

Now within the caffe directory, run the following one after the other

make all
make test
make runtest

These should run smoothly without any errors. If you do however, encounter errors, refer the ones at the end of this post or elsewhere to resolve them.

Make sure to REMOVE BUILD every time you resolve an error, and run those three commands again to rebuild.

This is VERY IMPORTANT, otherwise the errors will persist.

To remove build,

rm -rf ./build*/

Once all three run without errors, while in the caffe directory, type

make pycaffe

This will build a python wrapper. You will also find a python folder within the caffe folder now.

To use caffe within python, export its path as

export PYTHONPATH=~/Home/_username_/caffe/python:$PYTHONPATH

Replace _username_ with the your username in the system.

Once you've done this, run the python terminal and import caffe

>>> import caffe
>>>

This, should work. If it throws a 'module not found' error, check if it has been appended in pythonpath properly by typing

>>> import sys
>>> sys.path
['', '/home/nikita/caffe/python', '/home/nikita', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib
/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nikita/.local/lib/python2.7/site-
packages']

If you see that the /home/_username_/caffe/python path, isn't there, then do

>>> sys.path.append('/Home/_username_/caffe/python')

Alternatively, just use

caffe/python

depending on whether your export is already under home/username

That's all there is to installing caffe.

Common Errors

OpenCV Errors

.
.
/usr/bin/ld: cannot find -lopencv_imgcodecs
/usr/bin/ld: cannot find -lopencv_videoio
collect2: error: ld returned 1 exit status

This usually happens when you've either forgotten to uncomment line 21 when you have OpenCV 3, or if you haven't checked the version properly. If you're using OpenCV < 3, this shouldn't be an error at all. If it still shows on OpenCV >= 3.0, then in the Makefile ( NOT Makefile.config ) on line 181 (in my case), add libraries such that it looks like

LIBRARIES += glog gflags protobuf leveldb snappy \
  lmdb boost_system hdf5_hl hdf5 \
  opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

Also, make sure to rebuild!

OpenBLAS Errors

/usr/bin/ld: cannot find -lcblas
collect2: ld returned 1 exit status
make: *** [.build_release/lib/libcaffe.so] Error 1

This happens if you've not installed OpenBLAS properly, or if you've set
BLAS := open when you have only Atlas installed. It may sound silly, but 'to err is human'.

If it persists after rebuild, install Atlas instead and change to BLAS := atlas

@akshayjaryal603
Copy link

akshayjaryal603 commented Jul 26, 2018

import caffe
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named caffe

This error show when I import caffe

@NgoThiTien
Copy link

This error show When i install caffe:
make all
CXX .build_release/src/caffe/proto/caffe.pb.cc
In file included from .build_release/src/caffe/proto/caffe.pb.cc:4:0:
.build_release/src/caffe/proto/caffe.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
#error This file was generated by a newer version of protoc which is
^~~~~
.build_release/src/caffe/proto/caffe.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
#error incompatible with your Protocol Buffer headers. Please update
^~~~~
.build_release/src/caffe/proto/caffe.pb.h:14:2: error: #error your headers.
#error your headers.
^~~~~
In file included from .build_release/src/caffe/proto/caffe.pb.cc:4:0:
.build_release/src/caffe/proto/caffe.pb.h:25:10: fatal error: google/protobuf/generated_message_table_driven.h: No such file or directory
#include <google/protobuf/generated_message_table_driven.h>
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:598: recipe for target '.build_release/src/caffe/proto/caffe.pb.o' failed
make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1

@mbmamin1996
Copy link

hi i run a code then i have this
Traceback (most recent call last):
File "test_spatial_narrow_as_op.py", line 16, in
from caffe2.proto import caffe2_pb2
ImportError: No module named caffe2.proto

i used python 2 and ubuntu 16

thanks for helping

@AshuMaths1729
Copy link

Thanks a lot for this. I had been loitering around on the net for 2 days and then I found this.

@keerthanss
Copy link

Thank you so much for this! It's very useful and saves a lot of time!

@rali20
Copy link

rali20 commented Nov 22, 2018

PROTOC src/caffe/proto/caffe.proto
CXX src/caffe/blob.cpp
CXX src/caffe/data_transformer.cpp
CXX src/caffe/common.cpp
CXX src/caffe/internal_thread.cpp
In file included from ./include/caffe/blob.hpp:8:0,
from src/caffe/blob.cpp:4:
./include/caffe/common.hpp:5:10: fatal error: gflags/gflags.h: No such file or directory
#include <gflags/gflags.h>
^~~~~~~~~~~~~~~~~
compilation terminated.
src/caffe/common.cpp:1:10: fatal error: glog/logging.h: No such file or directory
#include <glog/logging.h>
^~~~~~~~~~~~~~~~
compilation terminated.
Makefile:499: recipe for target '.build_release/src/caffe/blob.o' failed
make: *** [.build_release/src/caffe/blob.o] Error 1
make: *** Waiting for unfinished jobs....
Makefile:499: recipe for target '.build_release/src/caffe/common.o' failed
make: *** [.build_release/src/caffe/common.o] Error 1
In file included from ./include/caffe/blob.hpp:8:0,
from ./include/caffe/data_transformer.hpp:6,
from src/caffe/data_transformer.cpp:6:
./include/caffe/common.hpp:5:10: fatal error: gflags/gflags.h: No such file or directory
#include <gflags/gflags.h>
^~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:499: recipe for target '.build_release/src/caffe/data_transformer.o' failed
make: *** [.build_release/src/caffe/data_transformer.o] Error 1
In file included from ./include/caffe/internal_thread.hpp:4:0,
from src/caffe/internal_thread.cpp:2:
./include/caffe/common.hpp:5:10: fatal error: gflags/gflags.h: No such file or directory
#include <gflags/gflags.h>
^~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:499: recipe for target '.build_release/src/caffe/internal_thread.o' failed
make: *** [.build_release/src/caffe/internal_thread.o] Error 1

@rali20
Copy link

rali20 commented Nov 22, 2018

I am getting this error while building

@symisc
Copy link

symisc commented Nov 24, 2018

Before running make pycaffe, you shroud run on the directory where the caffe source reside:

sudo pip install -r ./python/requirements.txt

@nayanleo
Copy link

nayanleo commented Jan 8, 2019

hello all,

I am getting erros while runnning commnad : ~/caffe$ make all
getting error like:
CXX/LD -o .build_release/tools/caffe.bin
//home/vikram/anaconda3/lib/libpng16.so.16: undefined reference to `inflateValidate@ZLIB_1.2.9'
collect2: error: ld returned 1 exit status
Makefile:637: recipe for target '.build_release/tools/caffe.bin' failed
make: *** [.build_release/tools/caffe.bin] Error 1

can you please help me to resolve this problem,
thank you in advance.

@vijeshkpaei
Copy link

surely open in python folder for checking caffe installed or not

ie,

open your terminal in caffe>>python
$python

import caffe

@vijeshkpaei
Copy link

vijeshkpaei commented Jan 16, 2019

and all are pls check your opencv version and copy paste to Makefile.config file for make

Uncomment if you're using OpenCV 3

OPENCV_VERSION := 3.4.3

@bhushan-valecha
Copy link

hello all
i am getting errors while running ~/caffe$ make all
CXX src/caffe/layer_factory.cpp
In file included from ./include/caffe/common.hpp:19:0,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/layer.hpp:8,
from src/caffe/layer_factory.cpp:8:
./include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory
compilation terminated.
Makefile:591: recipe for target '.build_release/src/caffe/layer_factory.o' failed
make: *** [.build_release/src/caffe/layer_factory.o] Error 1

can you please help me to resolve this problem,
thank you in advance.

@bhushan-valecha
Copy link

bhushan-valecha commented Jan 23, 2019

hello all
i am getting errors while running ~/caffe$ make all
CXX src/caffe/layer_factory.cpp
In file included from ./include/caffe/common.hpp:19:0,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/layer.hpp:8,
from src/caffe/layer_factory.cpp:8:
./include/caffe/util/device_alternate.hpp:34:23: fatal error: cublas_v2.h: No such file or directory
compilation terminated.
Makefile:591: recipe for target '.build_release/src/caffe/layer_factory.o' failed
make: *** [.build_release/src/caffe/layer_factory.o] Error 1

i have made the changes in the Makefile.config

can you please help me to resolve this problem,
thank you in advance.

@rivaldo4t
Copy link

rivaldo4t commented Mar 12, 2019

I got an ImportError: No module named google.protobuf.internal which was resolved by this
Also, for some implementations, you might stumble across Check failed: registry.count(type) == 1 (0 vs. 1) Unknown layer type: Python. To get rid of it, look here.

@ideallyworld
Copy link

Hi,
I install in my ubutun 16.4 step by stepy and all is OK untill the make pycaffe and find a python folder within the caffe folder.

Then error come out when import caffe in python just as follow:

import caffe

root@ubuntu:/caffe/python# cd ..
root@ubuntu:
/caffe# ls
build cmake CONTRIBUTING.md data docker examples INSTALL.md Makefile Makefile.config.example models README.md src
caffe.cloc CMakeLists.txt CONTRIBUTORS.md distribute docs include LICENSE Makefile.config matlab python scripts tools
root@ubuntu:/caffe# export PYTHONPATH=/caffe/python:$PYTHONPATH
root@ubuntu:/caffe# import caffe
import: not authorized `caffe' @ error/constitute.c/WriteImage/1028.
root@ubuntu:
/caffe# python
Python 2.7.12 (default, Nov 12 2018, 14:36:49)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.

import caffe
Traceback (most recent call last):
File "", line 1, in
File "/root/caffe/python/caffe/init.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/root/caffe/python/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver,
ImportError: No module named _caffe
sys.path
Traceback (most recent call last):
File "", line 1, in
NameError: name 'sys' is not defined
import sys
sys.path
['', '/root/caffe/python', '/root/caffe', '/opt/movidius/caffe/python', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages']
sys.path.append('/caffe/python')
File "", line 1
sys.path.append('/caffe/python')
^
IndentationError: unexpected indent
sys.path.append('/caffe/python')
import caffe
Traceback (most recent call last):
File "", line 1, in
File "/root/caffe/python/caffe/init.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/root/caffe/python/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver,
ImportError: No module named _caffe
import sys.path
Traceback (most recent call last):
File "", line 1, in
ImportError: No module named path

Could someone please help me to fix this problem?

BRs,
Thanks.

@NguyenDangBinh
Copy link

dear all.
I had issue like that:
(OpenSSL) binh@binh:~/3Dpose_ssl/caffe-3dssl$ make all
CXX src/caffe/solver.cpp
In file included from ./include/caffe/util/device_alternate.hpp:40:0,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/net.hpp:10,
from ./include/caffe/solver.hpp:7,
from src/caffe/solver.cpp:6:
./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’:
./include/caffe/util/cudnn.hpp:21:10: warning: enumeration value ‘CUDNN_STATUS_RUNTIME_PREREQUISITE_MISSING’ not handled in switch [-Wswitch]
switch (status) {
^
./include/caffe/util/cudnn.hpp: In function ‘void caffe::cudnn::setConvolutionDesc(cudnnConvolutionStruct**, cudnnTensorDescriptor_t, cudnnFilterDescriptor_t, int, int, int, int)’:
./include/caffe/util/cudnn.hpp:113:70: error: too few arguments to function ‘cudnnStatus_t cudnnSetConvolution2dDescriptor(cudnnConvolutionDescriptor_t, int, int, int, int, int, int, cudnnConvolutionMode_t, cudnnDataType_t)’
pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION));
^
./include/caffe/util/cudnn.hpp:15:28: note: in definition of macro ‘CUDNN_CHECK’
cudnnStatus_t status = condition;
^
In file included from ./include/caffe/util/cudnn.hpp:5:0,
from ./include/caffe/util/device_alternate.hpp:40,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/net.hpp:10,
from ./include/caffe/solver.hpp:7,
from src/caffe/solver.cpp:6:
/home/binh/anaconda2/envs/OpenSSL/include/cudnn.h:500:27: note: declared here
cudnnStatus_t CUDNNWINAPI cudnnSetConvolution2dDescriptor( cudnnConvolutionDescriptor_t convDesc,
^
Makefile:575: recipe for target '.build_release/src/caffe/solver.o' failed
make: *** [.build_release/src/caffe/solver.o] Error 1

How to solve? Thank you so much.

@NguyenDangBinh
Copy link

dear all.
I had issue like that:
(OpenSSL) binh@binh:~/3Dpose_ssl/caffe-3dssl$ make all
CXX src/caffe/solver.cpp
In file included from ./include/caffe/util/device_alternate.hpp:40:0,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/net.hpp:10,
from ./include/caffe/solver.hpp:7,
from src/caffe/solver.cpp:6:
./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’:
./include/caffe/util/cudnn.hpp:21:10: warning: enumeration value ‘CUDNN_STATUS_RUNTIME_PREREQUISITE_MISSING’ not handled in switch [-Wswitch]
switch (status) {
^
./include/caffe/util/cudnn.hpp: In function ‘void caffe::cudnn::setConvolutionDesc(cudnnConvolutionStruct**, cudnnTensorDescriptor_t, cudnnFilterDescriptor_t, int, int, int, int)’:
./include/caffe/util/cudnn.hpp:113:70: error: too few arguments to function ‘cudnnStatus_t cudnnSetConvolution2dDescriptor(cudnnConvolutionDescriptor_t, int, int, int, int, int, int, cudnnConvolutionMode_t, cudnnDataType_t)’
pad_h, pad_w, stride_h, stride_w, 1, 1, CUDNN_CROSS_CORRELATION));
^
./include/caffe/util/cudnn.hpp:15:28: note: in definition of macro ‘CUDNN_CHECK’
cudnnStatus_t status = condition;
^
In file included from ./include/caffe/util/cudnn.hpp:5:0,
from ./include/caffe/util/device_alternate.hpp:40,
from ./include/caffe/common.hpp:19,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/net.hpp:10,
from ./include/caffe/solver.hpp:7,
from src/caffe/solver.cpp:6:
/home/binh/anaconda2/envs/OpenSSL/include/cudnn.h:500:27: note: declared here
cudnnStatus_t CUDNNWINAPI cudnnSetConvolution2dDescriptor( cudnnConvolutionDescriptor_t convDesc,
^
Makefile:575: recipe for target '.build_release/src/caffe/solver.o' failed
make: *** [.build_release/src/caffe/solver.o] Error 1

How to solve? Thank you so much.

@Kannank97
Copy link

Hey, Thank you so much. This saved me a lot of time for my work

@ilyes-ouled-omar
Copy link

Thank you!!!
It was really helpful :)

@ashikrai
Copy link

ashikrai commented Aug 9, 2019

I have followed all the given steps but im getting error on
make all

Error is given below, can anyone help me:

CXX .build_release/src/caffe/proto/caffe.pb.cc
In file included from .build_release/src/caffe/proto/caffe.pb.cc:4:0:
.build_release/src/caffe/proto/caffe.pb.h:12:2: error: #error This file was generated by a newer version of protoc which is
#error This file was generated by a newer version of protoc which is
^
.build_release/src/caffe/proto/caffe.pb.h:13:2: error: #error incompatible with your Protocol Buffer headers. Please update
#error incompatible with your Protocol Buffer headers. Please update
^
.build_release/src/caffe/proto/caffe.pb.h:14:2: error: #error your headers.
#error your headers.
^
In file included from .build_release/src/caffe/proto/caffe.pb.cc:4:0:
.build_release/src/caffe/proto/caffe.pb.h:23:35: fatal error: google/protobuf/arena.h: No such file or directory
compilation terminated.
Makefile:598: recipe for target '.build_release/src/caffe/proto/caffe.pb.o' failed
make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1

@nrobertio
Copy link

hi, I am successfully done with all the commands listed, but in the last while checking using "import caffe" it says Import error: no module named caffe.

can you help me in encountering this error.
import sys
caffe_root = '/home/nvidia/caffe/' # this file should be run from {caffe_root}/examples (otherwise change this line)
sys.path.insert(0, caffe_root + 'python')

import caffe

@Zrufy
Copy link

Zrufy commented Oct 24, 2019

unsupported GNU version! gcc versions later than 8 are not supported!

@ziggyjosh16
Copy link

I'm wondering if it is possible to install caffe inside a conda environment

@nrobertio
Copy link

nrobertio commented Dec 6, 2019 via email

@nrobertio
Copy link

nrobertio commented Dec 6, 2019 via email

@Hassan313
Copy link

Thank you very much. Really appreciate your work here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment