Linux on MacBook Pro 2016 / 2017
Tags: linux, unix, ubuntu, opensource
Published: 2020-05-11 02:13:39

Hi there! :wave: This is my first post on dev.to. I didn't know what to write about so I just decided to talk about what I'm doing right now, **installing Linux on my MacBook Pro 2016 (model 13,3 - dual gpu with touchbar)**. This might also help anyone trying to install Linux on any MacBook without T2 chip (2017 or older).

---

# :open_book: Context
It all started a few months ago when I became curious about [tiling window managers](https://en.wikipedia.org/wiki/Tiling_window_manager) and how I could increase my day to day productivity. I'm a software developer and I have A LOT of open windows and programs running at the same time, so I decided to give tiling window managers a shot. Unfortunately there are not a lot of choices available for macOS, I found [yabai](https://github.com/koekeishiya/yabai) which is great! but as I started to tweak config files to be more comfortable working with my laptop I started to feel that it wasn't enough.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8lqj3q4myay2sfax7mig.png)

After a month or so of using yabai I met [i3-wm](https://i3wm.org/), and fell in love with it. The only problem... it's only for linux. So I though, what the heck, let's install Linux on my MacBook... and so the journey began.

---

# :warning: Disclaimer
There are a lot of well written guides on GitHub on how to do this (I'll put the links below), so I'm just going to write down the steps I followed in my particular case and the problems I faced.

# :shopping_cart: What you'll need
1. MacBook Pro 13,3 2016 (with touchbar - no T2 chip)
2. USB stick of at least 8GB
3. macOS. It doesn't really matter which version but it's important to keep it installed to make the touchbar and some other stuff work.

This MacBook has a few components that will make things a bit more complicated than usual, that is the Touchbar and the AMD dedicated GPU.

Some amazing people worked on drivers to make everything work the best way possible, so fear not, they got us covered :muscle:

---

# :penguin: Distro
I have an i7 CPU, 16 GB of RAM. I've tried Manjaro, Ubuntu and Linux Mint and stayed with the latter, it's much more stable in my opinion, but you can pick whatever you want as long as the kernel is 5.0.0 or greater, this is important because some stuff already work out of the box. Also, amdgpu seems not to be working on 5.6, [read more here](https://bugzilla.kernel.org/show_bug.cgi?id=207589).

---

# :cd: Partitioning & Linux installation 

### 1. Disable System Integrity Protection (SIP)
In order to perform some future steps required to install and boot Linux properly, we need to disable SIP (System Integrity Protection).

-   Reboot your Mac. When the startup screen shows up, press and hold the `Command + R` until the Apple logo appears on your screen.
-   Once it finishes loading and brings you to Recovery Mode, click “Utilities -> Terminal.”
-   In the Terminal window, type in `csrutil disable` and press Enter.
-   Restart your Mac.

### 2. Create a new partition
*I've taken all the images from this [amazing guide](https://www.maketecheasier.com/install-dual-boot-ubuntu-mac/) since it's beautifully explained and since I'm not writing this from macOS*

- Open Disk Utility from the “/Applications/Utilities” folder.
- Select your boot disk in the sidebar and click the “Partition” button.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/al5efo6l71yuzf518fx9.png)
- Click “Partition” in the dialog box to confirm.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/8vrni309l5clglcij4kp.png)
- Click the “+” button to add a partition

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/4cpsyublx5jhnuw97ue7.png)
- Set the size and name. Choose “MS-DOS (FAT)” for your partition type. This will be erased by the Ubuntu installer.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/0hnaoir9u2tq5qz1uqoa.png)
- Click “Apply,” then “Partition” to execute.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5l4qddopyil86ww2ur57.png)
### 3. Creating a bootable USB with Linux

- Download the ISO file from the [Linux Mint page](https://www.linuxmint.com/download.php) or your distro of preference.
- Download and Install [Etcher](https://www.balena.io/etcher/) in order to burn the ISO in our USB stick.
- Now plug the USB stick, open Etcher and select the ISO file to burn it.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gbxj0nl7iyx3bibze7og.jpeg)- Click on "Flash!" button.

### 4. Booting from the USB stick
Now we have Linux on our USB stick, it's time to begin the installation. I didn't need to use any external keyboard / mouse but keep in mind that you might require them if for some reason the built-in set doesn't work with the live USB.

- Restart the Mac with the USB stick inserted **while** the `Option/alt` _(⌥)_ key is pressed. This will launch Apple’s ‘Startup Manager’ which shows bootable devices connected to the machine. Your USB stick should appear as gold/yellow and labelled ‘EFI Boot’. Selecting this will lead you to the distro boot menu.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u4yjcjo6ifqoamgkjosj.png)
### 5. Running the installer
- Connect to your wireless network (if you can) and choose to install third-party software.
- At the installation selection screen, choose “Something Else” from the bottom.
- Select the FAT-32 partition you created earlier. Click the “–” button to delete it.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2rdo3ofvvl6ude03awbi.png)
- With the free space selected, click the “+” to create a new partition.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/cmeyyrg08mudcrgnuked.png)
- Set the size to 4000 MB and “Use as” to “swap.” (Some people say that for hibernation to work properly, the size of your swap should be around 1.5 times the size of your RAM. Not sure if this is the case, but hibernation didn't work for me anyway).
 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/akzrljmeyj13sibnum6t.png)
- Create another new partition with the “+” button. Use all the available free space. Set “Use as” to “Ext4 journaling file system.” Set the mount point to “/.”

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3z4vjq3dwkpu3kj4skw2.png)
- Choose the ext4 partition under “device for bootloader installation.”

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mfs5d1egxx3m21nidck0.png)
- Click through the remaining steps to create your user and finish the installation.

---

# :mage: Linux tweaks

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1l6mxgd2yrokzwrleoo6.jpg)

So far you should have Linux installed on your MacBook just fine, the built-in display, keyboard and trackpad should be working out of the box. Now we're going to fix the following:
1. :signal_strength: Wifi
2. :detective: Spoof macOS
3. :vhs: Switch to the iGPU
4. :loud_sound: Audio
5. :point_right: Touchbar

*Keep in mind that Wifi won't work until you reboot.*

## 1. Wifi
This MacBook model uses a Broadcom wireless board which is not great but at least we'll get decent wifi. There's an [issue](https://bugzilla.kernel.org/show_bug.cgi?id=193121) with the brcmfmac module used by the kernel. Here's how to fix it.

- Download [this file](https://gist.github.com/cristianmiranda/6f269797b62076c3414c3baa848dda67#file-brcmfmac43602-pcie-txt)
- And then
`cp brcmfmac43602-pcie.txt /lib/firmware/brcm`

Wifi works generally ok, but there are a few issues you should be aware of:
* Connectivity signal is not the best.
* After suspend/wake 5 GHz networks are not visible, and even if they are you cannot connect to them. 2.4 GHz works fine.

## 2. Spoof macOS
We need to make the laptop thinks she's going to boot macOS, otherwise there are some components that don't get powered, included the integrated Intel GPU. For this we'll use a custom EFI file like this:

```bash
cd /tmp
# See more at https://github.com/0xbb/apple_set_os.efi/issues/20#issuecomment-557352747
wget https://github.com/0xbb/apple_set_os.efi/files/3877561/apple_set_os.zip
unzip apple_set_os.zip

# As root...
sudo su 
mkdir -p /boot/efi/EFI/custom
mv /tmp/apple_set_os.efi /boot/efi/EFI/custom
```
Now we need to edit GRUB

```bash
sudo su
nano /etc/grub.d/40_default

# Add the following lines:
search --no-floppy --set=root --label EFI
chainloader (${root})/EFI/custom/apple_set_os.efi
boot

# Update grub
update-grub
```

***Note:** If you prefer using rEFInd, that's totally fine. Just remember to uncomment the "spoof_osx_version" line in refind.conf. See more at [rEFInd's documentation site](https://www.rodsbooks.com/refind/configfile.html).*

## 3. Switch to the iGPU
This laptop has two GPU boards, one is the integrated Intel Graphics card and the other one is the dedicated ATI Radeon card. Since there are no drivers for this card to work properly we'll have to switch to the integrated one and then power off the dedicated GPU.

```bash
sudo su

# Blacklist amdgpu
echo "blacklist amdgpu" > /etc/modprobe.d/blacklist-amdgpu.conf

# Switch to integrated GPU
cd && git clone https://github.com/0xbb/gpu-switch
cd gpu-switch
sudo ./gpu-switch -i
```
- Reboot
- Now we'll load amdgpu module and turn the card off properly.
```bash
sudo su
gpu-manager | grep 'amdgpu loaded? no' && sudo modprobe amdgpu || echo 'AMD GPU already loaded'
echo OFF > /sys/kernel/debug/vgaswitcheroo/switch
```
See more at https://github.com/Dunedan/mbp-2016-linux/issues/6#issuecomment-416015734

***Note:** Repeat last three steps every time you reboot (can be automated)*

## 4. Audio
Audio doesn't work out of the box, here's how to fix it.
 
```bash
sudo su
cd /tmp
sudo apt-get install wget make gcc linux-headers-generic
git clone https://github.com/leifliddy/snd_hda_macbookpro.git
cd snd_hda_macbookpro
./install.cirrus.driver.sh
```
***Note:** Changes won't take effect unless you reboot the system.*

## 5. Touchbar
```bash
sudo apt-get install git dkms

cd /tmp
echo -e "\n# macbook12-spi-drivers\napplespi\napple_ib_tb\nspi_pxa2xx_platform\nintel_lpss_pci" >> /etc/initramfs-tools/modules

git clone https://github.com/roadrunner2/macbook12-spi-driver.git
cd macbook12-spi-driver
git checkout touchbar-driver-hid-driver
dkms add .
uname -r # check kernel version
dkms install -m applespi -v 0.1 -k 5.3.0-24-generic # replace this with your kernel version

# lsinitramfs /boot/initrd.img-5.3.0-24-generic | grep -i "dkms/apple"
lib/modules/5.3.0-24-generic/updates/dkms/apple-ibridge.ko
lib/modules/5.3.0-24-generic/updates/dkms/apple-ib-tb.ko
lib/modules/5.3.0-24-generic/updates/dkms/applespi.ko
```

If you want the F-keys to be always on by default and the Fn button for switching from F-keys to special keys, then use the following command (if not, fnmode=1):

```bash
echo 'options apple_ib_tb fnmode=2' | sudo tee /etc/modprobe.d/apple_ib_tb.conf
echo 'options apple_ib_tb idle_timeout=60' | sudo tee /etc/modprobe.d/apple_ib_tb.conf
update-initramfs -u -k 5.3.0-24-generic # replace this with your kernel version
```

You can also swap `fn` and a `control` keys:

```bash
echo 'options applespi fnremap=1' | tee /etc/modprobe.d/applespi.conf
update-initramfs -u -k 5.3.0-24-generic # replace this with your kernel version
```

Or remap it to any other key:

```bash
# modinfo applespi | grep -w fnremap
parm:           fnremap:Remap fn key ([0] = no-remap; 1 = left-ctrl, 2 = left-shift, 3 = left-alt, 4 = left-meta, 6 = right-shift, 7 = right-alt, 8 = right-meta) (uint)
```

## 🎉 Bonus #1 - Keyboard
Here's a guide on how to remap your keyboard.
https://medium.com/@petrstepanov/a-macos-like-keyboard-remap-on-ubuntu-linux-cae1d108a97

## 🎉 Bonus #2 - Trackpad
Trackpad is not very good on Linux, here's how to improve it.
https://int3ractive.com/2018/09/make-the-best-of-MacBook-touchpad-on-Ubuntu.html

---

# What doesn't work

* Built-in microphone
* Hibernation
* Audio after suspend
* 5 GHz wifi networks after suspend

---

# Status 

Check out https://github.com/Dunedan/mbp-2016-linux for an updated status of each component.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/l4k11kdxddbgw8tma4ab.png)
# That's all Folks!
I hope you enjoyed this guide. Don't hesitate on commenting if you have any questions or you have any new findings regarding this topic.

Big thanks to all the people that made this possible. It's unbelievable the amount of people spending time on this without any retribution whatsoever :pray:

### Sources

* https://github.com/Dunedan/mbp-2016-linux
* https://www.maketecheasier.com/install-dual-boot-ubuntu-mac/ (also took the cover image from here).
* https://ubuntu.com/tutorials/tutorial-create-a-usb-stick-on-macos#5-etcher-configuration
* https://ubuntu.com/tutorials/tutorial-create-a-usb-stick-on-macos#7-boot-your-mac
* https://bugzilla.kernel.org/show_bug.cgi?id=193121#c62