Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 40 ยท Getting Started

Installing JAX and Choosing Devices

Install JAX for CPU, NVIDIA GPU or TPU with pip, verify the installation, list devices, understand platforms and backends, and fix common setup problems.

JAX is installed with pip, but unlike a pure-Python package it comes in two parts: the jax package (Python code) and jaxlib (the compiled C++ extension that talks to XLA and your hardware). Choosing the right install command decides whether your code runs on the CPU, an NVIDIA GPU or a Google TPU.

This lesson shows the common install commands, how to confirm that JAX sees your hardware, and how to select a device. Remember that the live editors on this site cannot import JAX (see What Is JAX?); commands here are for your own machine.

Pick an install

Always use a virtual environment so JAX's dependencies stay isolated.

Terminal
python -m venv .venv
source .venv/bin/activate        # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip

Then pick one of:

Terminal
# CPU only (Linux, macOS, and Windows)
pip install -U jax

# NVIDIA GPU with CUDA 12 libraries installed by pip (Linux)
pip install -U "jax[cuda12]"

# Google Cloud TPU VM
pip install -U "jax[tpu]"

Notes you should know:

  • The plain pip install jax gives you a CPU build. It is the right choice for learning, small experiments and CI.
  • GPU builds use NVIDIA's CUDA libraries. On Linux with a recent driver, the jax[cuda12] extra installs the matching CUDA runtime from pip, so you do not need a separate CUDA toolkit. You still need a working NVIDIA driver.
  • Native Windows GPU support is limited; the common route for a Windows GPU is WSL 2 (Linux inside Windows).
  • Apple Silicon GPUs have an experimental plugin (jax-metal). For learning, the CPU build works well on a Mac.
  • Package versions move quickly. If a tutorial and your install disagree, check the official installation guide for the current extras.

Verify the installation

Run this in a Python session or script. It is a read-only block, because it needs real JAX.

Python
import jax
import jax.numpy as jnp

print(jax.__version__)
print(jax.devices())                 # e.g. [CpuDevice(id=0)]
print(jax.default_backend())         # 'cpu', 'gpu' or 'tpu'

x = jnp.arange(5.0)
print(x * 2)                         # [0. 2. 4. 6. 8.]
print(x.devices())                   # where the array lives

On a machine with a GPU, jax.devices() lists GPU devices (their printed name depends on the version), and jax.default_backend() returns 'gpu'. If you installed a GPU build but see only a CPU device, JAX could not load the GPU libraries; it silently falls back to the CPU and usually prints a warning.

Devices, platforms and backends

Three words appear everywhere in the docs:

Term Meaning
Backend / platform The kind of hardware family: cpu, gpu or tpu
Device One concrete chip, such as a single GPU. jax.devices() lists them
Host The machine running your Python process; it may control several devices
flowchart TD
    P["Python process (host)"] --> B["backend: gpu"]
    B --> D0["device 0"]
    B --> D1["device 1"]
    B --> D2["device 2"]

One host, one backend, several devices. jax.devices() returns the devices of the default backend; jax.devices("cpu") asks for a specific one.

Arrays are placed on the default device unless you say otherwise:

Python
cpu = jax.devices("cpu")[0]
x = jax.device_put(jnp.ones(3), cpu)      # move an array explicitly

with jax.default_device(cpu):             # a context for a block of code
    y = jnp.zeros(3)

You can also force a platform for the whole process with an environment variable before Python starts, which is handy for debugging a GPU problem on the CPU:

Terminal
JAX_PLATFORMS=cpu python my_script.py

Precision: 32-bit by default

JAX defaults to 32-bit numbers: float32 and int32. This is intentional, because GPUs and TPUs are fastest with 32-bit (or lower) precision. If you need float64, enable it once, at the very start of your program, before creating any arrays:

Python
import jax
jax.config.update("jax_enable_x64", True)

You will study dtypes in JAX Arrays and Data Types.

Checking your environment (this one runs)

The editor below is plain Python. It does something real and useful: it asks the interpreter whether jax can be found, and prints what NumPy is available. In this browser runtime it will report that JAX is missing, which is exactly the honest answer. On your own machine the same snippet tells you whether a virtual environment is set up correctly.

Python Is JAX importable here?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Run it again after installing JAX in a local environment; every line should say installed.

Common problems and fixes

  • ModuleNotFoundError: No module named 'jax'. You are in the wrong environment. Check which python (or where python on Windows) and reinstall inside the activated venv.
  • GPU not found. Check nvidia-smi works, that your driver is recent enough, and that you installed the CUDA extra rather than the CPU package. Installing both CPU-only and GPU packages in one environment causes confusion; start from a fresh venv.
  • Version mismatch between jax and jaxlib. Upgrade them together: pip install -U jax jaxlib. Mixing old and new versions leads to import errors.
  • Out-of-memory on the GPU. JAX preallocates most GPU memory by default. Set XLA_PYTHON_CLIENT_PREALLOCATE=false to allocate on demand, or XLA_PYTHON_CLIENT_MEM_FRACTION=.5 to limit it.
  • Surprising float32 results. That is the default precision, not a bug; see above.

When asking for help, paste the output of jax.print_environment_info(). It lists versions, devices and the platform in one go.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which command installs a CPU-only build of JAX?

    A plain pip install jax gives the CPU build. The extras [cuda12] and [tpu] select GPU and TPU builds.
  2. 2. What does jax.devices() return?

    It lists the concrete chips (for example CpuDevice or GPU devices) that JAX can use on the default backend.
  3. 3. You need float64 numbers. What should you do?

    JAX defaults to 32-bit types. Enabling x64 must happen before arrays are created.
  4. 4. JAX finds only a CPU device even though you installed a GPU build. What is the likely cause?

    When the GPU backend fails to initialize, JAX falls back to the CPU and normally prints a warning.

Summary

  • JAX = the jax Python package + the compiled jaxlib; pick CPU, [cuda12] or [tpu] installs.
  • Use a virtual environment; upgrade jax and jaxlib together.
  • jax.devices() and jax.default_backend() show what JAX sees; jax.device_put moves arrays.
  • JAX uses 32-bit numbers by default; enable x64 at startup if you need 64-bit.
  • JAX_PLATFORMS=cpu forces the CPU, which helps when debugging.

Next: JAX vs NumPy