Building from Source
How to build Deeplearning4j from source — prerequisites, Maven build, libnd4j C++ compilation, CUDA and CPU builds, and common 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.
Overview
The DL4J stack is a monorepo at github.com/deeplearning4j/deeplearning4j. A full source build produces:
libnd4j — native C++ compute engine (compiled with CMake, invoked through Maven via JavaCPP)
nd4j — Java ND4J array library, SameDiff autodiff, backend bindings (CPU, CUDA)
deeplearning4j — neural network layers, LLM/VLM pipelines, PEFT, training infrastructure
datavec — data pipeline and ETL
omnihub — model hub and
AutoModelloaderpython4j — embedded CPython from the JVM
Prerequisites
Required Tools
JDK
11
JDK 17 recommended
Maven
3.6.3
Earlier 3.x releases have known issues with the monorepo
CMake
3.19
Required to compile libnd4j
gcc / g++
9.x
Or Clang 12+. Must support C++17
ccache
any
Essential — first build: 30–45 min; subsequent builds with ccache: ~30 seconds
Install ccache
ccache caches compiled object files. Without it, every native build recompiles everything from scratch.
Verify it's working: ccache --show-stats
Never run ccache -C or ccache --clear — this forces a full 30–45 minute rebuild.
Platform-Specific Setup
Linux (Ubuntu / Debian)
Linux (Fedora / RHEL)
macOS
Windows
libnd4j requires a Unix-compatible toolchain. Install MSYS2 and run inside the MSYS2 shell:
Add C:\msys64\mingw64\bin to your system PATH.
Cloning the Repository
Building: CPU
The Maven build compiles both the C++ native backend (libnd4j) and the Java modules in a single invocation. JavaCPP invokes CMake internally — you do not need to run CMake or make directly.
This builds libnd4j for CPU and the Java CPU backend bindings. First build takes 30–45 minutes; subsequent builds with ccache take ~30 seconds for small changes.
After the native backend is built, build the remaining Java modules:
Or build everything at once (slower, compiles all modules):
Building: CUDA
CUDA prerequisites
NVIDIA CUDA Toolkit (default: 12.9, configurable via
-Dcuda.version)Compatible NVIDIA driver (525.60+ for CUDA 12.9)
On Windows: Visual Studio 2019 or 2022
Install CUDA from developer.nvidia.com/cuda-downloads.
Build command
To enable Triton JIT compilation (produces the -compile classifier variant):
Building for a different CUDA version
cuda.version is a Maven property. To build against CUDA 12.6 instead of 12.9:
Note: the pre-built artifacts on Maven Central target CUDA 12.9. Building against a different version requires a source build.
Key build flags
-Dlibnd4j.chip=cuda
Build the CUDA backend (default is CPU)
-Dlibnd4j.buildthreads=N
Parallel compiler jobs for native build
-Dlibnd4j.triton=ON
Enable Triton MLIR JIT compilation stack
-Dlibnd4j.compute=XX
Target specific CUDA compute capability (e.g., 86 for Ampere). Changing this invalidates the ccache for all CUDA objects.
-Dcuda.version=X.Y
Target a specific CUDA toolkit version (default: 12.9)
-Pcpu / -Pcuda
Maven profile for CPU or CUDA build
Building Specific Modules
Maven's -pl flag restricts the build to specific submodules. Use -am (--also-make) to include transitive dependencies:
Running Tests
All tests live in platform-tests/. Tests are never placed in the modules being tested. The individual library modules (nd4j/, deeplearning4j/, datavec/) don't include a concrete backend — platform-tests is the only module that declares one, making it the single place where tests can actually execute against real native code.
Always run from the platform-tests directory:
Never run mvn test from the project root — it triggers full native rebuilds and runs every test suite.
Selecting a backend
The backend.artifactId Maven property controls which backend tests run against:
Memory configuration
The default heap is 32g — override for machines with less RAM:
Selecting tests by tag
Tests are tagged with JUnit 5 tags (samediff, onnx, training, long-running-test, etc.):
Domain test scripts
platform-tests/ includes convenience scripts:
run-onnx-tests.sh
ONNX SameDiff import tests
run-tensorflow-tests.sh
TensorFlow SameDiff import tests
run-keras-tests.sh
Keras model import tests
run-benchmarks.sh
Standalone JUnit launcher with valgrind/compute-sanitizer support
bootstrap-onnx.sh
Downloads ~65 ONNX Zoo models (data setup, not a test runner)
For a comprehensive guide to the platform-tests architecture — backend selection internals, JUnit 5 extensions, base test classes, test tags, memory profiling, and the shaded-JAR benchmark workflow — see Contributing — Platform Tests.
IDE Setup: IntelliJ IDEA
IntelliJ IDEA is the recommended IDE.
Open the repo 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 shows false errors throughout the codebase.
Enable annotation processing: Settings > Build, Execution, Deployment > Compiler > Annotation Processors > check "Enable annotation processing".
Import Maven profiles as needed (
cpuorcuda).Increase IntelliJ's JVM heap: Help > Change Memory Settings > 4096 MB or higher.
Using Local Build Artifacts
After a successful build, use the local snapshot in a downstream project:
Check the current snapshot version in the root POM.
CI Build Artifacts
The project CI builds native artifacts for every platform and configuration. Understanding the classifier system helps when selecting the right JAR for your deployment target.
Artifact classifier format
Pre-built artifacts use classifiers to encode platform, helper library, and SIMD extension:
For example:
nd4j-native-1.0.0-SNAPSHOT-linux-x86_64.jar— base CPUnd4j-native-1.0.0-SNAPSHOT-linux-x86_64-onednn-avx2.jar— oneDNN + AVX2nd4j-native-1.0.0-SNAPSHOT-linux-x86_64-compile.jar— MLIR/Triton compile stack
Supported build matrix
Linux x86_64
onednn, compile
avx2, avx512
Linux ARM64
—
—
macOS ARM64
—
—
Windows x86_64
onednn
avx2, avx512
Android ARM64
—
—
Android x86_64
—
—
Linux x86_64 CUDA 12.6
cudnn, compile
—
Linux x86_64 CUDA 12.9
cudnn, compile
—
Building a specific classifier locally
To build with a helper or extension, pass the corresponding Maven properties:
For full CI/CD infrastructure details (build matrix, test suites, snapshot deployment), see Contributing — CI/CD Build Environment.
Common Build Issues
Native build takes forever (no ccache)
Symptom: Every build takes 30–45 minutes even for small changes.
Fix: Install ccache and verify it's on your PATH. Check ccache --show-stats — the hit rate should be high after the first build.
CMake version too old
Symptom: CMake policy errors or missing CMake features.
Fix: Install CMake 3.19+. On older systems: pip install cmake or download from cmake.org.
Out of memory during Maven build
Symptom: java.lang.OutOfMemoryError or GC overhead limit exceeded.
Fix:
CUDA compute capability mismatch
Symptom: no kernel image is available for execution on the device
Fix: Check your GPU's compute capability and pass it explicitly:
Windows: DLL not found at runtime
Symptom: Can't find dependent libraries when running from an IDE.
Fix: Add C:\msys64\mingw64\bin to your system PATH and restart the IDE.
Related Pages
Contributing — pull request workflow, coding conventions, Eclipse CLA
Maven Setup — using pre-built Maven Central artifacts
GPU and CPU Setup — configuring backends for end users
Last updated
Was this helpful?