Skip to content

Instantly share code, notes, and snippets.

Last active April 2, 2024 18:37
Show Gist options
  • Save nikitametha/c54e1abecff7ab53896270509da80215 to your computer and use it in GitHub Desktop.
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).



sudo apt-get install libopencv-dev python-opencv

OpenBLAS OR Atlas


sudo apt-get install libopenblas-dev


sudo apt-get install libatlas-base-dev


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

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__
  • 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


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


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-

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


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/] 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

Copy link

nrobertio commented Dec 6, 2019 via email

Copy link

nrobertio commented Dec 6, 2019 via email

Copy link

Thank you very much. Really appreciate your work here.

Copy link

samiulhaq952 commented Apr 2, 2024

This worked like a charm. Thank you a lot!

If you may, let me point two little improvements:

  • if you downloaded Caffe to another directory, you can create a symbolic link using ln -s command. For example, go to your home directory in a terminal, then type ln -s ~/Downloads/caffe assuming caffe is in your Download folder. Once this is done, I got caffe installed at /home/gilva/caffe/python, then caffe got loaded correctly when I used export PYTHONPATH=/home/gilvan/caffe/python:$PYTHONPATH
  • if your system also missed the scikit-image Python binding, then an error ImportError: No module named was shown when importing Caffe to python. Try installing the python-skimage package by typing the command sudo apt-get install python-skimage and it will be gone.

Hello Sir.
I am facing issues while running make runtest command. I am getting the below errors:

F0402 17:21:22.449431 14248 db_leveldb.cpp:16] Check failed: status.ok() Failed to open leveldb /tmp/caffe_test.6186-8dae/db
IO error: /tmp/caffe_test.6186-8dae/db: Invalid argument
*** Check failure stack trace: ***
@ 0x7f459ff9fb03 (unknown)
@ 0x7f459ffa79d1 (unknown)
@ 0x7f459ff9f7c2 (unknown)
@ 0x7f459ffa178f (unknown)
@ 0x7f459ecf6149 (unknown)
@ 0x7f45a01c5fa9 (unknown)
@ 0x7f45a01cab14 (unknown)
@ 0x7f45a03fa3cf (unknown)
@ 0x7f45a03f22ed (unknown)
@ 0x7f45a03f2428 (unknown)
@ 0x7f45a03f2505 (unknown)
@ 0x7f45a03f29f4 (unknown)
@ 0x7f45a03fa9b7 (unknown)
@ 0x7f45a03f2ad1 (unknown)
@ 0x7f45a0120f96 (unknown)
@ 0x7f459e759d90 (unknown)
@ 0x7f459e759e40 (unknown)
@ 0x7f45a0127725 (unknown)
@ (nil) (unknown)
Aborted (core dumped)
make: *** [Makefile:543: runtest] Error 134

Can you please help me? I have been struggling for 1 week. Your assistance will be highly appreciated.
Best Regards,
Sami Ul Haq

Copy link

samiulhaq952 commented Apr 2, 2024

Hello @nikitametha Ma'am, I hope you're doing well.
Thanks for this amazing and easy-to-follow tutorial. I have successfully run the first two commands.
However, I am facing the below errors while running the last command, "make run test".

[----------] 1 test from LayerFactoryTest/1, where TypeParam = caffe::CPUDevice
[ RUN ] LayerFactoryTest/1.TestCreateLayer
F0402 21:26:53.470124 19252 db_leveldb.cpp:16] Check failed: status.ok() Failed to open leveldb /tmp/caffe_test.1f86-7dbe
IO error: /tmp/caffe_test.1f86-7dbe: Invalid argument
*** Check failure stack trace: ***
@ 0x7f6e1c30fb03 (unknown)
@ 0x7f6e1c3179d1 (unknown)
@ 0x7f6e1c30f7c2 (unknown)
@ 0x7f6e1c31178f (unknown)
@ 0x7f6e1b066149 (unknown)
@ 0x7f6e1c60124a (unknown)
@ 0x7f6e1c75e3cf (unknown)
@ 0x7f6e1c7562ed (unknown)
@ 0x7f6e1c756428 (unknown)
@ 0x7f6e1c756505 (unknown)
@ 0x7f6e1c7569f4 (unknown)
@ 0x7f6e1c75e9b7 (unknown)
@ 0x7f6e1c756ad1 (unknown)
@ 0x7f6e1c484f96 (unknown)
@ 0x7f6e1aac9d90 (unknown)
@ 0x7f6e1aac9e40 (unknown)
@ 0x7f6e1c48b725 (unknown)
@ (nil) (unknown)
Aborted (core dumped)
make: *** [Makefile:543: runtest] Error 134

I have attached my "Makefile.config" to this message. Kindly check it and help me solve this issue. I am struggling with this issue since last 5 days. Your assistance will be highly appreciated.

PC: I am using Ubuntu 22.04.4 LTS on window 10 without GPU

Thanks & Best regards,
Sami Ul Haq

Link to config file:

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