Carry your Linux around in a USB, boot it on Mac or PC. (The missing tutorial)

The Need

I was recently using multiple machines for work (Lab machines, friend’s laptop, etc) and I needed Linux. I own a 128GB MacBook Air I could’nt install Linux on it as storage was tiny. I did have a USB 3.0 flash drive which had speeds comparable to some(not-so-fast) harddrives. It struck me that if I install Linux on my flash drive it would make my life a hell lot easier. It was later that I realised it wasn’t so straight forward mainly because of EFI boot and Mac ‘quirks’. I did a lot of googling but could’nt find anything that worked. After reading multiple sources I deduced what was the problem. Since I got it figured out I decided to write this post so that other people can benefit from it.

The problem

  • Modern Macs boot using EFI and their bootloader expects boot partition to be HFS+ or APFS(High Sierra) not EXT4.
  • Ubuntu installer is buggy and always installs bootloader in EFI partition of internal HDD despite being instructed to install it on EFI partition of flash drive.
    • This makes the flash drive only bootable on the mac it was made on

The Solution

Step 1: Preparing live USB for installation
  • Download https://unetbootin.github.io/

  • Download your favourite Ubuntu flavor, Im using Ubuntu Mate

  • Burn the iso to a USB drive(not on your installation flash drive) using UNetbootin

Step 2: Boot using live installation drive
  • Plug both drives and press option+power button

  • Choose EFI boot option

  • Choose Try Ubuntu without Installing

Step 3: Install Linux on target flash drive
  • Once into the live session, open terminal and run ubuquity —no-bootloader , this will start installation wizard in a mode that wont install a bootloader (Dont worry we will take care of it later)

Run installer with no-bootloader option

Fig 1: Run installer with no-bootloader option
  • Keep going next untill an option comes as shown in below image. Choose Something else

Choose this option

Fig 2: Choose this option
  • On your target drive, create a 200MB EFI System Partition as the first partition (Primary)
  • Create a reasonable sized ext4 partition, with mount point = '\' (Primary)

Create partitions similar to this

Fig 3: Sample partitions
  • Click on Install
  • Reboot into Mac after installation finishes
Step 4: Setting up Boot manager

We will be using a super awesome 3rd party boot manager rEFInd. It can detect any operating systems installed in EFI mode and boot them.

  • Download rEFInd zip and extract it
  • Open Terminal and navigate to rEFInd directory
  • Run diskutil list and find the name of your flash drive’s EFI partition. (In my case /dev/disk2s1)
  • Run ./refind-install --usedefault /dev/diskXXX (replace XXX with appropriate name)

output should be similar to this

Fig 4: Output must be similar to this

Now your flash drive is ready to boot on any Mac or EFI compatible PC. Moreover, if you ever mess up your bootloader and are unable to boot rEFInd can help you boot into your OS (if it exists :p)

Testing on Mac and PC

MacBook Air (Early 2015)
  • Press option+power and select EFI Boot
    rEFInd screen
Fig 5: rEFInd screen
  • Select your apropriate Linux to boot
    Booted up
Fig 6: MacBook booted
Asus X550LD (PC)
  • Boot from flash drive in UEFI Mode
    rEFInd screen
Fig 7: rEFInd screen
  • Select your apropriate Linux to boot
    Booted up
Fig 8: PC booted
Written on September 6, 2017