GRUB and LILO both fail to install to NVMe hard disk when installing Debian

Here is what worked for me, using Debian jessie (stable). I basically took the instructions from this wiki post, and stripped out all the steps about dual-booting with Windows, since those didn't apply to my case.

  1. In the BIOS, set "UEFI only" boot.

  2. Using Gparted, create a FAT32 partition at the beginning of the disk with the boot and esp flags. (The Debian installer should be able to do this too, but since the installer incorrectly recognized the size of the disk, I prefer to use Gparted). In my case, the FAT32 partition is /dev/nvme0n1p1.

  3. During the installation, make sure you have a network connection configured (manually or automatically, doesn't matter). Otherwise, the next step will fail.

  4. At the installation stage where GRUB fails to install, open a shell and run the following commands:

    mount --bind /dev /target/dev
    mount --bind /dev/pts /target/dev/pts
    mount --bind /proc /target/proc
    mount --bind /sys /target/sys
    cp /etc/resolv.conf /target/etc
    chroot /target /bin/bash
    
    aptitude update
    aptitude install grub-efi-amd64
    update-grub
    grub-install --target=x86_64-efi /dev/nvme0n1
    

    Exit the shell and select "Continue without installing a bootloader." You'll see a warning message that gives you boot commands to use; you can ignore this.

  5. Once the installation completes, boot into the system. Add "nvme" to /etc/initramfs-tools/modules, then run update-initramfs -u as root.

  6. Edit /etc/default/grub and add this line

    GRUB_CMDLINE_LINUX="intel_pstate=no_hwp"
    

    and add "nomodeset" to the GRUB_CMDLINE_LINUX_DEFAULT so it looks like this:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet nomodeset"
    
  7. Run update-grub.

The last few commands (initramfs onward) are necessary to prevent disk not found errors the second time you try to boot into the new system.