Linux服务器无权限安装caffe教程
作者:贾金让
前言:服务器上没有root权限,不能使用sudo和apt-get无脑安装caffe需要的各种依赖,因此需要手动安装这些依赖库。核心就是将原来apt-get安装在/usr/include,/usr/lib等位置的库,手动安装在我们自己的路径下,即自定义路径安装依赖。
这些依赖由于版本,交叉依赖等问题,不小心的话会出现各种错误,比较经典的就是opencv和cuda9.0这种错误(下面有处理方法)。因此把我自己的安装流程记录下来,防止重复踩坑。
参考:/yosinski/caffe/blob/jason_public/doc/linux-no-root-install-log.md
0.根目录下,终端依次执行:
mkdir tempmkdir local
用途:以后所有步骤下载的源码都放在temp文件夹中,编译得到的库、头文件和可执行程序都放在local文件夹中。
PS:下面路径中的所有your_root_path都要替换为你自己的根目录路径。
安装必备工具m4, autoconf, automake, libtool
安装m4
cd tempwget /gnu/m4/m4-1.4.18.tar.gztar zxvf m4-1.4.18.tar.gzcd m4-1.4.18./configure --prefix=/your_root_path/localmakemake install
安装autoconf
cd tempwget /gnu/autoconf/autoconf-2.69.tar.gztar zxvf autoconf-2.69.tar.gzcd autoconf-2.69./configure --prefix=/your_root_path/localmakemake install
安装automake
cd tempwget /gnu/automake/automake-1.16.1.tar.gztar zxvf automake-1.16.1.tar.gzcd automake-1.16.1./configure --prefix=/your_root_path/localmakemake install
安装libtool
cd tempwget http://mirror.csclub.uwaterloo.ca/gnu/libtool/libtool-2.4.6.tar.gzcd libtool-2.4.6tar zxvf libtool-2.4.6.tar.gz./configure --prefix=/your_root_path/localmakemake install
将它们的可执行程序路径写入环境变量中:
vim ~/.bashrc
点击i进入输入模式
将下列代码复制粘贴到文件最后:
export PATH="/your_root_path/local/bin:$PATH"export LIBRARY_PATH="/your_root_path/local/lib:$LIBRARY_PATH"export LD_LIBRARY_PATH="/your_root_path/local/lib:$LD_LIBRARY_PATH"
点ESC退出输入模式,输入:wq
进行保存并退出。
命令行输入source ~/.bashrc
激活环境变量。
可分别使用下面的命令进行版本查询并验证是否安装成功:
m4 --versionautoconf --versionautomake --versionlibtool --version
PS:本部分参考/qq_30549833/article/details/72955881
2.安装boost库
源码下载地址:/users/history/version_1_67_0.html
或使用wget下载。
cd tempwget /boostorg/release/1.67.0/source/boost_1_67_0.tar.gztar zxvf boost_1_67_0.tar.gzcd boost_1_67_0./bootstrap.sh --prefix=/your_root_path/local./b2 -j32./b2 install
将boost库头文件路径写入环境变量:
vim ~/.bashrc
在最后复制粘贴下面代码:
export BOOST_INCLUDEDIR="/your_root_path/local/include:$BOOST_INCLUDEDIR"
点ESC退出输入模式,输入:wq
进行保存并退出。
命令行输入source ~/.bashrc
激活环境变量。
3.安装Glog
从github上下载源码:
git clone /google/glog.gitcd glog./autogen.sh./configure --prefix=/your_root_path/localmakemake install
4.安装Gflags
从github上下载源码:
git clone /gflags/gflags.gitcd gflagsmkdir buildcd buildCXXFLAGS="-fPIC" cmake -D CMAKE_INSTALL_PREFIX=/your_root_pat/local ..makemake install
PS:这时如使用cmake编译caffe仍然是找不到gflags和glog的,需要将caffe_root_path/caffe/cmake/Modules/FindGFlags.cmake和FindGlog.cmake中的内容做一下修改,下面直接给出修改后的两个文件内容:
FindGFlags.cmake:
# - Try to find GFLAGS## The following variables are optionally searched for defaults# GFLAGS_ROOT_DIR: Base directory where all GFLAGS components are found## The following are set after configuration is done:# GFLAGS_FOUND# GFLAGS_INCLUDE_DIRS# GFLAGS_LIBRARIES# GFLAGS_LIBRARYRARY_DIRSinclude(FindPackageHandleStandardArgs)set(GFLAGS_ROOT_DIR "" CACHE PATH "Folder contains Gflags")# We are testing only a couple of files in the include directoriesif(WIN32)# 增加代码find_path(GFLAGS_INCLUDE_DIR gflags/gflags.hPATHS ${GFLAGS_ROOT_DIR}/src/windows NO_DEFAULT_PATH)find_path(GFLAGS_INCLUDE_DIR gflags/gflags.hPATHS ${GFLAGS_ROOT_DIR}/src/windows)else()# 增加代码find_path(GFLAGS_INCLUDE_DIR gflags/gflags.hPATHS ${GFLAGS_ROOT_DIR}/includeNO_DEFAULT_PATH)find_path(GFLAGS_INCLUDE_DIR gflags/gflags.hPATHS ${GFLAGS_ROOT_DIR}/include)endif()if(MSVC)# 增加代码find_library(GFLAGS_LIBRARY_RELEASENAMES libgflagsPATHS ${GFLAGS_ROOT_DIR}PATH_SUFFIXES Release NO_DEFAULT_PATH)find_library(GFLAGS_LIBRARY_RELEASENAMES libgflagsPATHS ${GFLAGS_ROOT_DIR}PATH_SUFFIXES Release)# 增加代码find_library(GFLAGS_LIBRARY_DEBUGNAMES libgflags-debugPATHS ${GFLAGS_ROOT_DIR}PATH_SUFFIXES Debug NO_DEFAULT_PATH)find_library(GFLAGS_LIBRARY_DEBUGNAMES libgflags-debugPATHS ${GFLAGS_ROOT_DIR}PATH_SUFFIXES Debug)set(GFLAGS_LIBRARY optimized ${GFLAGS_LIBRARY_RELEASE} debug ${GFLAGS_LIBRARY_DEBUG})else()find_library(GFLAGS_LIBRARY gflags PATHS ${GFLAGS_ROOT_DIR}/lib NO_DEFAULT_PATH)find_library(GFLAGS_LIBRARY gflags PATHS ${GFLAGS_ROOT_DIR}/lib)endif()find_package_handle_standard_args(GFlags DEFAULT_MSG GFLAGS_INCLUDE_DIR GFLAGS_LIBRARY)if(GFLAGS_FOUND)set(GFLAGS_INCLUDE_DIRS ${GFLAGS_INCLUDE_DIR})set(GFLAGS_LIBRARIES ${GFLAGS_LIBRARY})message(STATUS "Found gflags (include: ${GFLAGS_INCLUDE_DIR}, library: ${GFLAGS_LIBRARY})")mark_as_advanced(GFLAGS_LIBRARY_DEBUG GFLAGS_LIBRARY_RELEASEGFLAGS_LIBRARY GFLAGS_INCLUDE_DIR GFLAGS_ROOT_DIR)endif()
FindGlog.cmake:
# - Try to find Glog## The following variables are optionally searched for defaults# GLOG_ROOT_DIR: Base directory where all GLOG components are found## The following are set after configuration is done:# GLOG_FOUND# GLOG_INCLUDE_DIRS# GLOG_LIBRARIES# GLOG_LIBRARYRARY_DIRSinclude(FindPackageHandleStandardArgs)# modified by guyadongset(GLOG_ROOT_DIR "" CACHE PATH "Folder contains Google glog")if(WIN32)# 增加代码find_path(GLOG_INCLUDE_DIR glog/logging.hPATHS ${GLOG_ROOT_DIR}/src/windows NO_DEFAULT_PATH)else()# 增加代码find_path(GLOG_INCLUDE_DIR glog/logging.hPATHS ${GLOG_ROOT_DIR}/includeNO_DEFAULT_PATH)endif()if(MSVC)find_library(GLOG_LIBRARY_RELEASE libglog_staticPATHS ${GLOG_ROOT_DIR}/libPATH_SUFFIXES Release NO_DEFAULT_PATH)find_library(GLOG_LIBRARY_DEBUG libglog_staticPATHS ${GLOG_ROOT_DIR}/libPATH_SUFFIXES Debug NO_DEFAULT_PATH)set(GLOG_LIBRARY optimized ${GLOG_LIBRARY_RELEASE} debug ${GLOG_LIBRARY_DEBUG})else()# 增加代码find_library(GLOG_LIBRARY glogPATHS ${GLOG_ROOT_DIR}/libNO_DEFAULT_PATH)endif()find_package_handle_standard_args(Glog DEFAULT_MSG GLOG_INCLUDE_DIR GLOG_LIBRARY)if(GLOG_FOUND)set(GLOG_INCLUDE_DIRS ${GLOG_INCLUDE_DIR})set(GLOG_LIBRARIES ${GLOG_LIBRARY})message(STATUS "Found glog (include: ${GLOG_INCLUDE_DIR}, library: ${GLOG_LIBRARY})")mark_as_advanced(GLOG_ROOT_DIR GLOG_LIBRARY_RELEASE GLOG_LIBRARY_DEBUGGLOG_LIBRARY GLOG_INCLUDE_DIR)endif()
这时再使用cmake编译caffe即可找到glog和gflag。具体原因参见:
/10km/article/details/72967656
5.安装protobuf
从网址下载:/google/protobuf/releases
或wget下载。
cd tempwget /google/protobuf/releases/download/v3.5.1/protobuf-cpp-3.5.1.tar.gztar zxvf protobuf-cpp-3.5.1.tar.gzcd protobuf-3.5.1./autogen.sh./configure --prefix=/your_root_path/localmakemake install
6.安装hdf5
cd tempwget /ftp/HDF5/current/src/hdf5-1.10.1.tartar xvf hdf5-1.10.1.tarcd hdf5-1.10.1./autogen.sh./configure --prefix=/your_root_path/localmake -j32make checkmake installmake check-install
PS:用cmake编译caffe时,寻找hdf5的路径有bug,至今没弄明白其原因。尽管设定DHDF5_ROOT=/your_root_path/local,仍然会提示找不到hdf5的头文件:
Could NOT find HDF5 (missing: HDF5_INCLUDE_DIRS) (found version "1.10.1")
对于这个问题我很绝望,我的解决方法有两个:一个是找公司管理员要权限,apt-get在默认路径装hdf5,这样cmake在调用其FindHDF5.cmake时就能找到。第二个方法是给cmake降级,我删除了我的3.11.1的cmake,使用2.8.12版本的cmake来编译caffe,同样能找到hdf5。不是很懂为什么高等级的cmake不行而低等级的可以。希望有其他解决方法的朋友可以告诉我。
7.安装snappy
cd tempgit clone /google/snappy.gitcd snappymkdir buildcd buildcmake -D CMAKE_INSTALL_PREFIX=/your_root_path/local ..make -j32make install
PS:snappy在cmake时要求cmake版本大于3.4,因此如果之前装hdf5时采用cmake降级的方法,这里没法通过,因此要再升回去。。辛苦辛苦。
8.安装lapack
(要在装opencv之前装,这时opencv必须的库,不然装opencv会报错)
官网手动下载/lapack/
或wget下载
cd tempwget /lapack/lapack-3.8.0.tar.gztar zxvf lapack-3.8.0.tar.gzcd lapack-3.8.0mkdir buildcd buildcmake -D CMAKE_INSTALL_PREFIX=/your_root_path/local ..make -j32make install
9.安装openblas
cd tempgit clone /xianyi/OpenBLAS.gitcd OpenBLASmakemake install PREFIX=/your_root_path/local
也可以用cmake,和之前的其他库的安装方法相同。
10.安装opencv3.2.0
这里我安装的时3.2.0版本,因为公司都用这个版本。
cd tempwget /opencv/opencv/archive/3.2.0.zipunzip 3.2.0cd opencv-3.2.0mkdir buildcd buildcmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/your_root_path/local ..make make install
注意,这里在cmake或者make时可能会出现几个问题,会报错,具体如下:
a) 如果你的cuda版本是9.0及以上,是一定会报错的,因为cuda9不再支持2.0架构。
报错如下:
CMake Error: The following variables are used in this project, but they are set to NOTFOUND.Please set them or make sure they are set and tested correctly in the CMake files:CUDA_nppi_LIBRARY (ADVANCED)linked by target "opencv_cudev" in directory /home/jiajinrang/temp/opencv-3.2.0/modules/cudevlinked by target "opencv_cudev" in directory /home/jiajinrang/temp/opencv-3.2.0/modules/cudev.....
解决方式请看链接:/u014613745/article/details/78310916
b)在解决了问题a)之后,cmake会可以通过,然而在make -j32是可能还会报如下面的错:
nvcc fatal : Unsupported gpu architecture 'compute_20'CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:208 (message):
解决方法是使用下面的语句重新cmake:
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/your_root_path/local -D CUDA_GENERATION=Kepler ..
c) 不要盲目为了加速而使用make -j32
代替make
,多核同时编译opencv有时会报错,我猜是因为库编译的先后顺序不对。因此如果你使用make -j32
报库相关的错并且你确定这个库你已经正确安装了的话,那还是老实儿用make
慢慢来吧。
4)make或make install时报如下错误:
fatal error: LAPACKE_H_PATH-NOTFOUND/lapacke.h: No such file or directory #include "LAPACKE_H_PATH-NOTFOUND/lapacke.h"
解决方式:
将opencv的build目录下的opencv_lapack.h
文件打开,将其中的第二行#include "LAPACKE_H_PATH-NOTFOUND/lapacke.h"
改成#include "lapacke.h"
或你之前安装的lapacke.h的路径。一般来说在openblas的头文件路径下也会有lapacke.h,这种情况久可以改成```#include “openblas/lapacke.h”
11.安装cudnn
cudnn不需要编译,直接下载后解压,然后将对应的文件拷贝到local目录下对应的文件夹里就行了。下载cudnn注意和自己的cuda版本相对应。官网下载需要注册账号。
cudnn下载地址(官网):/rdp/cudnn-archive
之后解压,复制到local对应文件夹即可。
12.安装lmdb
cd tempgit clone /mdb/mdb.gitmakemake prefix=/your_root_path/local install
PS:这里我没装leveldb,因为lmdb和leveldb功能相似,有一个就行了。所以要将caffe根目录下的CMakeLists.txt中
caffe_option(USE_LEVELDB "Build with levelDB" ON)
改为
caffe_option(USE_LEVELDB "Build with levelDB" OFF)
终于所需要的依赖都装完了,上面所有步骤可以因人而异,不需要的库可以不装,只要在CMakeList.txt把对应的ON改成OFF即可。
下面进入caffe根目录:
mkdir buildcd buildcmake .. -G "Unix Makefiles" -DGLOG_ROOT_DIR=/home/jiajinrang/local -DGFLAGS_ROOT_DIR=/home/jiajinrang/local -DCMAKE_PREFIX_PATH=/home/jiajinrang/local -DBLAS=openmake all -j8make install
ok,希望大家不要再报错了。。