How to install an Android factory image onto a Nexus device

Flashing an Android system image can be complicated. It will also reset your device back to factory settings. Good thing there are easy step-by-step instructions for doing it.

First, head to Google’s Factory Image site and find the latest version for your device (currently Android 5.0.1/5.0.2 depending on the smartphone/tablet).

To flash a system image:

  1. Download the appropriate system image for your device below, then unzip it to a safe directory.
  2. Connect your device to your computer over USB.
  3. Start the device in fastboot mode with one of the following methods:
    • Using the adb tool: With the device powered on, execute:
      adb reboot bootloader
    • Using a key combo: Turn the device off, then turn it on and immediately hold down the relevant key combination for your device. For example, to put a Nexus 5 (“hammerhead”) into fastboot mode, press and hold Volume Up + Volume Down + Power as the device begins booting up.
  4. If necessary, unlock the device’s bootloader by running:
    fastboot oem unlock

    The target device will show you a confirmation screen. (This erases all data on the target device.)

  5. Open a terminal and navigate to the unzipped system image directory.
  6. Execute the flash-all script. This script installs the necessary bootloader, baseband firmware(s), and operating system.

Once the script finishes, your device reboots. You should now lock the bootloader for security:

  1. Start the device in fastboot mode again, as described above.
  2. Execute:
    fastboot oem lock

Some recent Nexus devices may throw an error when attempting to flash-all. In that case, you’re going to want to extract the .zip file containing all the individual .img files and flash them individually.

Here is the order to flash them:

  • fastboot flash system system.img
  • fastboot flash recovery recovery.img
  • fastboot flash cache cache.img
  • fastboot flash boot boot.img
  • fastboot flash userdata vendor.img (if it’s included)
  • reboot

Locking the bootloader does not wipe any data, but when you want to flash it again, you must run fastboot oem unlock again, which will wipe the data.

Remember, flashing a factory image overwrites any data on the device, including media, photos and documents.