Building from Source
How to build Deeplearning4j from source — prerequisites, Maven build, libnd4j compilation, and common build issues
Most users should use the releases on Maven Central and do not need to build from source. Building from source is intended for contributors, those testing unreleased features, or teams maintaining a custom fork. Custom layers, loss functions, and activation functions can all be added without modifying DL4J directly.
Overview
The DL4J stack is a monorepo at github.com/eclipse/deeplearning4j. A full source build produces the following components:
libnd4j — native C++ backend (compiled with CMake)
nd4j — Java bindings and math backend
datavec — data pipeline and ETL
deeplearning4j — neural network training framework and utilities
Prerequisites
Required Tools
JDK
11
JDK 17 recommended for M2.1
Maven
3.6.3
Earlier 3.x releases have known issues with the monorepo
Git
2.x
Any recent version
CMake
3.9
Required to compile libnd4j
gcc / g++
7.x
Clang is not supported on Linux
Platform-Specific Setup
Linux (Ubuntu / Debian)
Verify versions before proceeding:
Linux (CentOS / RHEL)
macOS
Install Xcode command-line tools and Homebrew, then:
Note: The system clang is used on macOS for the JNI layer, but libnd4j's native C++ code requires GCC. Install GCC via Homebrew if needed:
Windows
libnd4j requires a Unix-compatible toolchain. Install MSYS2 and then run inside the MSYS2 shell:
Add C:\msys64\mingw64\bin to your system PATH. Restart any open IDE after doing so.
CPU Math Libraries
Choose one of the following for CPU computation:
OpenBLAS (recommended, open source):
Intel MKL (best single-machine CPU performance): Download from Intel's developer site. After installation, add the MKL library directory to LD_LIBRARY_PATH (Linux) or PATH (Windows).
To link MKL at runtime against an OpenBLAS-linked binary, create symbolic links:
Cloning the Repository
To work on a specific release tag (for example, the M2.1 tag):
Building libnd4j (Native Backend)
libnd4j is the C++ compute engine. It must be compiled before the Java modules can be built.
After compilation, export the path so Maven can locate the native libraries:
Common libnd4j Build Flags
-c cuda
Build with CUDA GPU support
-cc <arch>
Target a specific CUDA compute capability (e.g., -cc 86 for Ampere)
-j <n>
Use n parallel compiler jobs
--build-type Release
Release build (default); use Debug for debugging
Example for CUDA with compute capability 8.6 (RTX 30xx):
Building with CUDA
Prerequisites
NVIDIA CUDA Toolkit 11.x or 12.x (M2.1 supports both)
A supported NVIDIA GPU driver
On Windows: Visual Studio 2019 or 2022 (Community edition is sufficient)
Install CUDA from developer.nvidia.com/cuda-downloads.
Linux / macOS Build
Find your GPU's compute capability at developer.nvidia.com/cuda-gpus. For example, an RTX 4090 uses -cc 89.
Windows Build
Open a standard
cmd.exeprompt and run:From that same prompt, launch the MSYS2 shell:
Inside the MSYS2 shell:
Full Maven Build
After libnd4j is built and LIBND4J_HOME is set, build all Java modules from the repo root:
This installs all artifacts to your local Maven repository (~/.m2/repository). A first build typically takes 15–45 minutes depending on hardware.
Building Specific Modules
Maven's -pl flag restricts the build to specific submodules:
Use the -am flag (--also-make) to include transitive dependencies of a module:
Using the Build Script
A convenience script is provided for building the entire stack:
Running Tests
DL4J tests depend on a separate test-resources repository (~10 GB). Clone it with a shallow fetch if history is not needed:
Run tests against the native CPU backend:
Run tests for a single module:
IDE Setup: IntelliJ IDEA
IntelliJ IDEA is the recommended IDE.
Clone the repository and open it as a Maven project (File > Open, select
pom.xmlat the repo root).Install the Lombok plugin: Settings > Plugins > search "Lombok" > Install. Without it, the IDE will show false errors throughout the codebase.
Enable annotation processing: Settings > Build, Execution, Deployment > Compiler > Annotation Processors > check "Enable annotation processing".
Import Maven profiles as needed for your backend (e.g.,
nativeorcuda).If working on the Scala API or the DL4J UI, install the Scala plugin.
For large monorepo projects, increase IntelliJ's JVM heap: Help > Change Memory Settings > set 4096 MB or higher.
Using Local Build Artifacts
After a successful build, use the local snapshot in a downstream project by matching the version in your pom.xml:
Check the current snapshot version in the root POM.
Common Build Issues
LIBND4J_HOME not set
Symptom: Maven build fails with errors about missing native libraries or JNI classes.
Fix: Export the variable pointing to the compiled libnd4j directory:
CMake version too old
Symptom: cmake: command not found or CMake policy errors.
Fix: Install CMake 3.9 or newer. On older Ubuntu/CentOS systems, install via pip install cmake or download from cmake.org.
clang used instead of gcc on macOS
Symptom: Linker errors or missing OpenMP support.
Fix: Force GCC explicitly:
Out-of-memory during Maven build
Symptom: Build fails with java.lang.OutOfMemoryError or GC overhead limit exceeded.
Fix:
CUDA version mismatch
Symptom: Errors like no kernel image is available for execution on the device.
Fix: Ensure the -cc flag matches your GPU's actual compute capability. Check it with:
Windows: DLL not found at runtime
Symptom: Can't find dependent libraries when launching from an IDE.
Fix: Add C:\msys64\mingw64\bin to your system PATH and restart the IDE.
Support
If you encounter build issues not covered here, please reach out on the DL4J GitHub Discussions or the community Gitter channel.
Last updated
Was this helpful?