# Is it possible to reduce the size and run time when building OpenCV from source?

**URL:** <https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938>\
**Category:** Uncategorized\
**Tags:** build, cuda\
**Created:** [April 23, 2021, 9:51am UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938 "2021-04-23T09:51:59Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 23, 2021, 9:51am UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/1 "2021-04-23T09:51:59Z")

</div>

I’m building OpenCV 4.5.1 from source with the following configuration:

```
  -D WITH_CUDA=ON \
  -D WITH_CUDNN=ON \
  -D WITH_CUBLAS=1 \
  -D OPENCV_DNN_CUDA=ON \
  -D ENABLE_FAST_MATH=1 \
  -D CUDA_FAST_MATH=1 \
  -D CUDA_ARCH_BIN=7.0 \
  -D CUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.0 \
  -D OPENCV_EXTRA_MODULES_PATH=/opencv_compile/opencv_contrib-4.5.1/modules \
  -D OPENCV_GENERATE_PKGCONFIG=YES \
  -D BUILD_opencv_python3=ON \
  -D PYTHON3_EXECUTABLE=/usr/bin/python3 \
  -D PYTHON3_NUMPY_INCLUDE_DIR=/usr/lib64/python3.6/site-packages/numpy/core/include \
  -D PYTHON3_PACKAGES_PATH=/opt/opencv_python \
  -D CMAKE_BUILD_TYPE=RELEASE \
  -D CMAKE_INSTALL_PREFIX=/opt/opencv .. && \

```

I’m building this in a Docker image which takes approx. 1hr 45mins and creates a file 8Gb in size.  
I need to ship this image to another user.

Is there anyway that the build can be slimmed down?

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 23, 2021, 9:59am UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/2 "2021-04-23T09:59:21Z")

</div>

which opencv modules do you actually need ? (all of them ?)

if you’re only interested in the python bindings, you should add

```
-D BUILD_SHARED_LIBS=OFF

```

to the cmake cmdline, to have cv2 _statically_ linked against the opencv libs  
(you neither need those, nor the corresponding .so’s later, then)

it’s also unclear, what exactly you’re shipping  
(statically linked cv2 would be ~10mb, dynamically linked ~2mb + ~100mb libs)  
((let’s hope, you don’t ship all build artefacts, which indeed would be some GB 😉 ))

[edit]

ah, wait this is all without CUDA …

---

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 23, 2021, 2:42pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/3 "2021-04-23T14:42:29Z")

</div>

@berak thanks for your reply

> [@berak](#):
>
> which opencv modules do you actually need ? (all of them ?)

I need the following:

Image file reading and writing (imread, imwrite)  
Drawing Functions (rectangle, putText, FONT\_HERSHEY\_SIMPLEX)  
Color Space Conversions (cvtColor, COLOR\_BGR2RGB)  
Deep Neural Network (dnn)

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [April 23, 2021, 4:00pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/5 "2021-04-23T16:00:37Z")

</div>

what about cuda ???

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [April 23, 2021, 4:12pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/6 "2021-04-23T16:12:30Z")

</div>

As @berak said if you are processing on the CPU and only need CUDA for the DNN stuff you should be able to remove the remaining CUDA modules

```auto
-DBUILD_opencv_cudaarithm=OFF -DBUILD_opencv_cudabgsegm=OFF -DBUILD_opencv_cudafeatures2d=OFF -DBUILD_opencv_cudafilters=OFF -DBUILD_opencv_cudaimgproc=OFF -DBUILD_opencv_cudalegacy=OFF -DBUILD_opencv_cudaobjdetect=OFF -DBUILD_opencv_cudaoptflow=OFF -DBUILD_opencv_cudastereo=OFF -DBUILD_opencv_cudawarping=OFF -DBUILD_opencv_cudacodec=OFF

```

---

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 23, 2021, 8:29pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/7 "2021-04-23T20:29:10Z")

</div>

> [@berak](#):
>
> what about cuda ???

@berak I need Cuda as I’m using the DNN module for object detection inference on an Nvidia GPU card (v100).

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [April 24, 2021, 11:48am UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/8 "2021-04-24T11:48:50Z")

</div>

I anticipated your response that’s why I clarified what @berak meant above.

---

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 24, 2021, 12:27pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/9 "2021-04-24T12:27:17Z")

</div>

@cudawarped I am doing some of the processing on the CPU but need to use the GPU for the DNN module.

---

<div class="post-metadata">

**Author:** ![cudawarped](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@cudawarped](https://forum.opencv.org/u/cudawarped)\
**Post date:** [April 24, 2021, 12:49pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/10 "2021-04-24T12:49:49Z")

</div>

Cool you should be able to disable all the other cuda modules then.

---

<div class="post-metadata">

**Author:** ![nekro](https://avatars.discourse-cdn.com/v4/letter/n/46a35a/32.png) [@nekro](https://forum.opencv.org/u/nekro)\
**Post date:** [April 26, 2021, 1:58pm UTC](https://forum.opencv.org/t/is-it-possible-to-reduce-the-size-and-run-time-when-building-opencv-from-source/2938/11 "2021-04-26T13:58:47Z")

</div>

@berak @cudawarped thanks both for your guidance.

Build time has reduced from 105mins to 20mins and size of my Docker image from 8Gb to 4Gb. This is much more manageable now and my code still seems to work.
