build_from_source.md 7.9 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3
Build and Install
=================

L
liaogang 已提交
4 5 6 7 8
* [1. Requirement](#Requirement)
* [2. Build on Ubuntu](#ubuntu)
* [3. Build on Mac OS X](#mac)

## <span id="Requirement">Requirement</span>
Z
zhangjinchao01 已提交
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

### Dependents

- **CMake**: required for 2.8+ version
- **g++**: a recent c++ compiler supporting c++11, >= 4.6, < 5
- **BLAS library**: such as openBLAS, MKL, ATLAS
- **protobuf**: required for 2.4+ version, 3.x is not supported
- **python**: currently only 2.7 version is supported

### Optional

PaddlePaddle also support some build options, you have to install related libraries. 

- **WITH_GPU**: Compile with gpu mode
  - The GPU version works best with Cuda Toolkit 7.5 and cuDNN v5
  - Other versions Cuda Toolkit 6.5, 7.0 and cuDNN v2, v3, v4 are also supported
  - Note: to utilize cuDNN v5, Cuda Toolkit 7.5 is prerequisite and vice versa
- **WITH_DOUBLE**: Compile with double precision, otherwise use single precision 
- **WITH_GLOG**: Compile with glog, otherwise use a log implement internally
- **WITH_GFLAGS**: Compile with gflags, otherwise use a flag implement internally
- **WITH_TESTING**: Compile with gtest and run unittest for PaddlePaddle 
- **WITH_DOC**: Compile with documentation
- **WITH_SWIG_PY**: Compile with python predict api
- **WITH_STYLE_CHECK**: Style check for source code


L
liaogang 已提交
35
## <span id="ubuntu">Building on Ubuntu14.04</span>
Z
zhangjinchao01 已提交
36 37 38 39 40 41 42 43

### Install Dependencies

- **CPU Dependencies**

```bash
# necessary
sudo apt-get update
44
sudo apt-get install -y g++ make cmake build-essential libatlas-base-dev python python-pip libpython-dev m4 libprotobuf-dev protobuf-compiler python-protobuf python-numpy git
Z
zhangjinchao01 已提交
45 46 47 48
# optional
sudo apt-get install libgoogle-glog-dev
sudo apt-get install libgflags-dev
sudo apt-get install libgtest-dev
49
sudo pip install wheel
Z
zhangjinchao01 已提交
50
pushd /usr/src/gtest
L
liaogang 已提交
51
cmake .
Z
zhangjinchao01 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
make
sudo cp *.a /usr/lib
popd
```
    
  
- **GPU Dependencies(optional)**

If you need to build GPU version, the first thing you need is a machine that has GPU and CUDA installed.
And you also need to install cuDNN.

You can download CUDA toolkit and cuDNN from nvidia website:
    
```bash
https://developer.nvidia.com/cuda-downloads
https://developer.nvidia.com/cudnn
```
You can copy cuDNN files into the CUDA toolkit directory, such as:

```bash
sudo tar -xzf cudnn-7.5-linux-x64-v5.1.tgz -C /usr/local
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
```
Then you need to set LD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc.

```bash
Q
qijun 已提交
78
export LD_LIBRARY_PATH=/usr/local/cuda/lib64:$LD_LIBRARY_PATH
Z
zhangjinchao01 已提交
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
export CUDA_HOME=/usr/local/cuda
export PATH=/usr/local/cuda/bin:$PATH
```
- **Python Dependencies(optional)**

If you want to compile PaddlePaddle with python predict api, you need to add -DWITH_SWIG_PY=ON in cmake command and install these first:

```bash
sudo apt-get install swig
```

- **Doc Dependencies(optional)**

If you want to compile PaddlePaddle with doc, you need to add -DWITH_DOC=ON in cmake command and install these first:

```bash
Q
qijun 已提交
95
pip install 'sphinx>=1.4.0'
Z
zhangjinchao01 已提交
96
pip install sphinx_rtd_theme breathe recommonmark
Q
qijun 已提交
97
sudo apt-get install doxygen 
Z
zhangjinchao01 已提交
98 99
```

100
### Build and Install
Z
zhangjinchao01 已提交
101 102 103 104 105 106 107 108

CMake will find dependent libraries in system default paths first. After installing some optional libraries, corresponding build option will automatically be on(such as glog, gtest and gflags). And if libraries are not found, you have to set following variables manually in cmake command(CUDNN_ROOT, ATLAS_ROOT, MKL_ROOT, OPENBLAS_ROOT).

Here are some examples of cmake command with different options:

**only cpu**

```bash
L
liaogang 已提交
109
cmake -DWITH_GPU=OFF -DWITH_DOC=OFF
Z
zhangjinchao01 已提交
110 111 112 113 114
```

**gpu**

```bash
L
liaogang 已提交
115
cmake -DWITH_GPU=ON -DWITH_DOC=OFF
Z
zhangjinchao01 已提交
116 117 118 119 120
```

**gpu with doc and swig**

```bash
L
liaogang 已提交
121
cmake -DWITH_GPU=ON -DWITH_DOC=ON -DWITH_SWIG_PY=ON
Z
zhangjinchao01 已提交
122 123 124 125 126 127 128 129 130 131 132
``` 

Finally, you can download source code and build:

```bash
git clone https://github.com/baidu/Paddle paddle
cd paddle
mkdir build
cd build
# you can add build option here, such as:    
cmake -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX=<path to install> ..
W
wangmiao1981 已提交
133 134
# please use sudo make install, if you want
# to install PaddlePaddle into the system
Z
zhangjinchao01 已提交
135
make -j `nproc` && make install
L
liaogang 已提交
136 137
# PaddlePaddle installation path
export PATH=<path to install>/bin:$PATH
Z
zhangjinchao01 已提交
138 139 140 141 142 143 144 145
```
**Note**

And if you set WITH_SWIG_PY=ON, you have to install related python predict api at the same time:

```bash
pip install <path to install>/opt/paddle/share/wheels/*.whl
```
L
liaogang 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## <span id="mac">Building on Mac OS X</span>

### Prerequisites
This guide is based on Mac OS X 10.11 (El Capitan). Note that if you are running an up to date version of OS X, 
you will already have Python 2.7.10 and Numpy 1.8 installed.

The best option is to use the package manager homebrew to handle installations and upgrades for you.
To install homebrew, first open a terminal window (you can find Terminal in the Utilities folder in Applications), and issue the command:

```bash
# install brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
# install pip
easy_install pip
```

### Install Dependencies

- **CPU Dependencies**

```bash
# Install fundamental dependents 
brew install glog gflags cmake protobuf openblas

# Install google test on Mac OS X
# Download gtest 1.7.0
wget https://github.com/google/googletest/archive/release-1.7.0.tar.gz
tar -xvf googletest-release-1.7.0.tar.gz && cd googletest-release-1.7.0
# Build gtest
mkdir build && cmake ..
make
# Install gtest library
sudo cp -r ../include/gtest /usr/local/include/
sudo cp lib*.a /usr/local/lib
```
    
  
- **GPU Dependencies(optional)**

If you need to build GPU version, the first thing you need is a machine that has NVIDIA GPU and CUDA installed.
And you also need to install cuDNN.

You can download CUDA toolkit and cuDNN from nvidia website:
    
```bash
https://developer.nvidia.com/cuda-downloads
https://developer.nvidia.com/cudnn
```
You can copy cuDNN files into the CUDA toolkit directory, for instance:

```bash
sudo tar -xzf cudnn-7.5-osx-x64-v5.0-ga.tgz -C /usr/local
sudo chmod a+r /usr/local/cuda/include/cudnn.h /usr/local/cuda/lib64/libcudnn*
```
Then you need to set DYLD\_LIBRARY\_PATH, CUDA\_HOME and PATH environment variables in ~/.bashrc.

```bash
export DYLD_LIBRARY_PATH=/usr/local/cuda/lib:$DYLD_LIBRARY_PATH
export PATH=/usr/local/cuda/bin:$PATH
```
- **Python Dependencies(optional)**

If you want to compile PaddlePaddle with python predict API, you need to add -DWITH_SWIG_PY=ON in cmake command and install these first:

```bash
brew install swig
```

- **Doc Dependencies(optional)**

If you want to compile PaddlePaddle with doc, you need to add -DWITH_DOC=ON in cmake command and install these first:

```bash
pip install 'sphinx>=1.4.0'
pip install sphinx_rtd_theme breathe recommonmark
brew install doxygen 
```

### Build and Install

CMake can find dependent libraries in system default paths firstly.
After installing some optional libraries, corresponding build option will be on automatically (for instance, glog, gtest and gflags).
If not found, you have to set following variables manually via CMake command (CUDNN_ROOT, ATLAS_ROOT, MKL_ROOT, OPENBLAS_ROOT).

Here are some examples of CMake command with different options:

**only cpu**

```bash
L
liaogang 已提交
235
cmake -DWITH_GPU=OFF -DWITH_DOC=OFF
L
liaogang 已提交
236 237 238 239 240
```

**gpu**

```bash
L
liaogang 已提交
241
cmake -DWITH_GPU=ON -DWITH_DOC=OFF
L
liaogang 已提交
242 243 244 245 246
```

**gpu with doc and swig**

```bash
L
liaogang 已提交
247
cmake -DWITH_GPU=ON -DWITH_DOC=ON -DWITH_SWIG_PY=ON
L
liaogang 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
``` 

Finally, you can download source code and build:

```bash
git clone https://github.com/baidu/Paddle paddle
cd paddle
mkdir build
cd build
# you can add build option here, such as:    
cmake -DWITH_GPU=ON -DWITH_DOC=OFF -DCMAKE_INSTALL_PREFIX=<path to install> ..
# please use sudo make install, if you want
# to install PaddlePaddle into the system
make -j `nproc` && make install
# PaddlePaddle installation path
export PATH=<path to install>/bin:$PATH
```
**Note**

And if you set WITH_SWIG_PY=ON, you have to install related python predict api at the same time:

```bash
sudo pip install <path to install>/opt/paddle/share/wheels/*.whl
```