Skip to content

Apache MXNet - Installing MXNet

To get started with Apache MXNet, you need to install it on your system. MXNet supports a variety of platforms, including Linux, macOS, and Windows. It also supports Python 3 (Python 2 is no longer supported).

The primary method for installing MXNet is using pip. You can choose a package tailored to your hardware (CPU or GPU) and, for GPUs, your CUDA version.

MXNet offers different pip packages. The base package mxnet is for CPU-only execution. For NVIDIA GPU support, you’ll need a package specific to your CUDA toolkit version (e.g., mxnet-cu112 for CUDA 11.2). MXNet also provides packages built with oneDNN (formerly MKL-DNN) for enhanced performance on Intel hardware.

Before installing, it’s recommended to set up a virtual environment (e.g., using venv or conda).

# Basic CPU version
pip install mxnet
# CPU version with oneDNN (for Intel CPUs)
pip install mxnet-mkl
  1. Ensure you have compatible NVIDIA drivers and the CUDA Toolkit installed. Check the official MXNet documentation for the list of supported CUDA versions for the MXNet version you intend to install.

  2. Install the appropriate MXNet package. For example, if you have CUDA 11.2 installed:

# For CUDA 11.2 (replace with your CUDA version, e.g., cu110, cu117, cu121)
pip install mxnet-cu112
# For CUDA 11.2 with oneDNN support
pip install mxnet-cu112mkl

To find the correct package name for your CUDA version, consult the MXNet installation guide on its official website or PyPI page (pypi.org/project/mxnet).

After installation, you can verify it by importing MXNet in Python and checking its version and context (CPU/GPU):

import mxnet as mx
print(f"MXNet version: {mx.__version__}")
try:
# Attempt to use GPU context
a = mx.nd.ones((2,3), ctx=mx.gpu())
print("MXNet is using GPU.")
print(a)
except mx.MXNetError:
# Fallback to CPU if GPU context fails
a = mx.nd.ones((2,3), ctx=mx.cpu())
print("MXNet is using CPU.")
print(a)

Docker provides a convenient way to use MXNet in a pre-configured containerized environment. Official MXNet Docker images are available on Docker Hub (hub.docker.com/r/apache/mxnet or legacy mxnet/python).

  1. Install Docker on your system following instructions from docs.docker.com.

  2. For GPU support within Docker, install the NVIDIA Container Toolkit (nvidia-docker2).

  3. Pull an MXNet Docker image. Example:

# For CPU-only (latest stable version)
# docker pull apache/mxnet:latest_cpu
# For GPU with a specific CUDA version (e.g., CUDA 11.2, Python 3.8)
# docker pull apache/mxnet:1.9.1_gpu_cu112_py38
# Check Docker Hub for the latest available tags.
  1. Run the Docker container:
# For CPU
# docker run -it apache/mxnet:latest_cpu bash
# For GPU
# docker run --gpus all -it apache/mxnet:1.9.1_gpu_cu112_py38 bash

Building from source provides maximum customization but is more complex. This is generally recommended for developers or when specific build options are needed. The exact dependencies and build steps can change, so always refer to the official MXNet build from source guide.

  • A C++11 compliant compiler (e.g., GCC, Clang, MSVC).
  • CMake (version 3.13 or newer recommended).
  • Git.
  • BLAS library (e.g., OpenBLAS, MKL).
  • Optional: OpenCV for image processing support.
  • Optional: CUDA Toolkit and cuDNN for NVIDIA GPU support.
  • Optional: oneDNN (MKL-DNN) for Intel hardware acceleration.
  1. Clone the MXNet repository:
git clone --recursive https://github.com/apache/mxnet.git
cd mxnet
  1. Create a build directory and configure with CMake:
mkdir build && cd build
# Example for CPU build with OpenBLAS and OpenCV:
# cmake -DUSE_CUDA=OFF -DUSE_OPENCV=ON -DUSE_OPENMP=ON -DUSE_BLAS=openblas ..
# Example for GPU build (CUDA, cuDNN, OpenCV, oneDNN):
# cmake -DUSE_CUDA=ON -DUSE_CUDNN=ON -DUSE_OPENCV=ON -DUSE_MKL_IF_AVAILABLE=ON ..
# Add other flags like -DUSE_CPP_PACKAGE=1 for C++ package, etc.
  1. Compile:
cmake --build . --parallel $(nproc) # For Linux/macOS, adjust $(nproc) as needed
# For Windows using MSBuild, after CMake generates solution files:
# msbuild mxnet.sln /p:Configuration=Release;Platform=x64 /maxcpucount
  1. After building the shared library (e.g., libmxnet.so on Linux, libmxnet.dylib on macOS, mxnet.dll on Windows), you can install the Python bindings:
cd ../python
pip install -e .

Note: Building on macOS with NVIDIA GPUs is not officially supported by recent CUDA versions. MXNet on macOS primarily targets CPU or potentially Apple’s Metal Performance Shaders (MPS) if community support for it develops sufficiently.

Pip, Docker, and building from source are all well-supported. Ensure build essentials and necessary libraries (OpenBLAS, OpenCV dev packages) are installed if building from source.

# Example dependencies for building on Ubuntu:
sudo apt-get update
sudo apt-get install -y build-essential git ninja-build ccache libopenblas-dev libopencv-dev cmake

Pip and Docker (CPU) are common. Building from source for CPU is also feasible. NVIDIA GPU support on macOS is deprecated by NVIDIA; thus, MXNet GPU support on macOS is not actively maintained.

# Example dependencies for building on macOS (using Homebrew):
# brew install cmake ninja ccache opencv openblas

Pip installation is recommended. Building from source requires Visual Studio (e.g., 2017, 2019, or 2022 with C++ toolchain), CMake, and careful setup of dependencies like CUDA, cuDNN, OpenCV, and BLAS libraries.

Prerequisites for building might include:

  • Visual Studio with C++ Desktop Development workload.
  • CMake.
  • Git.
  • CUDA Toolkit and cuDNN (if GPU build). Ensure compatibility with your Visual Studio version.
  • OpenCV (pre-built or build from source).
  • A BLAS library (OpenBLAS or Intel MKL).

Set environment variables like OpenCV_DIR and OpenBLAS_HOME if needed during CMake configuration.

Major cloud providers offer virtual machines (VMs) and managed services with MXNet pre-installed or easily installable:

  • Amazon Web Services (AWS): AWS Deep Learning AMIs come with MXNet, CUDA, cuDNN, and oneDNN pre-installed. Amazon SageMaker supports MXNet for training and deployment.
  • Microsoft Azure: Azure Machine Learning services and Data Science Virtual Machines (DSVMs) support MXNet.
  • Google Cloud Platform (GCP): GCP’s Deep Learning VM Image includes MXNet. AI Platform also supports MXNet training and prediction.

These cloud environments often provide optimized builds and easy access to GPU instances.

Running MXNet on Raspberry Pi (e.g., 3B+, 4) is possible, primarily for CPU-based inference. Due to limited resources, performance will be constrained.

Options:

  • Pip Wheels: Look for community-provided or official (if available) Python wheels for ARM architecture. This is the easiest method if compatible wheels exist.
  • Docker: If Docker is supported on your Raspberry Pi OS, ARM-compatible MXNet Docker images might be available.
  • Build from Source: This is challenging due to long compile times and potential dependency issues. Cross-compilation might be an alternative for advanced users.
# Example for installing dependencies on Raspbian/Debian-based ARM systems before building:
# sudo apt-get update
# sudo apt-get install -y git cmake build-essential libopenblas-dev python3-dev python3-pip
# Then attempt to build or install a wheel.

NVIDIA Jetson devices (e.g., Nano, Xavier NX, AGX Orin) are powerful embedded platforms with GPUs, suitable for running MXNet with CUDA acceleration.

  1. JetPack SDK: Ensure your Jetson device is flashed with the latest NVIDIA JetPack SDK, which includes CUDA, cuDNN, TensorRT, and other necessary libraries.

  2. Pip Wheels: NVIDIA or the community often provide pre-built MXNet wheels for Jetson. Check NVIDIA NGC, Jetson Zoo, or MXNet forums for these wheels. This is the recommended method.

# Example: (Hypothetical command, find actual wheel URL/name)
# pip3 install mxnet-jetson-jp5.1-cuda11.4.whl
  1. Docker: NVIDIA provides base Docker images for Jetson (e.g., nvcr.io/nvidia/l4t-base). You might find or build MXNet Docker images on top of these.

  2. Build from Source: Possible, but ensure your CMake configuration targets the Jetson’s architecture and CUDA capabilities. Cross-compilation or native compilation on the Jetson can be time-consuming.

# General dependencies for Python usage before installing MXNet wheel/building:
sudo apt-get update
sudo apt-get install -y libatlas-base-dev libopencv-dev python3-pip
pip3 install --upgrade pip setuptools numpy

Always consult the latest documentation from NVIDIA and the MXNet community for installing on specific Jetson hardware and JetPack versions.