Ubuntu 20.04 Linux on mid-2017 MacBookPro

This article describes how to install Ubuntu 20.04 Linux on your MacBookPro.

Ubuntu 20.04 Linux on mid-2017 MacBookPro

Last updated on 26 April 2021.

Intro

This article describes how to install Ubuntu Linux on your MacBookPro.

The old version of article can be found here https://nixaid.com/linux-on-macbookpro-old/ (for Ubuntu 18.04).

Hardware

Apple MacBook Pro "Core i7 2.9 15" Touch/Mid-2017 with AMD Radeon Pro 560

Target Kernel/Distro

Linux >=5.0 on Ubuntu 20.04

Not working but can be worked around

- Audio input & output

Use bluetooth headphones/speakers for audio as a workaround.

If you just want the ISO and skip all the hard work, you can get it here:

Download 3.7G: https://files.nixaid.com/ubuntu4mac.iso
SHA256: 8262d84f54dc1f62bae3f46507e461cc0b4980ba261ac8dad52b8a3ec2eb9a31


Preparing the disk

Partition for your Linux installation

You would need at least 25 GB free disk space for the Ubuntu 20.04 installation.

Reduce your Macintosh HD partition from macOS by using Disk Utility.
The freed space will be used by the Ubuntu Linux installer.

Remastering Ubuntu distro

I used another laptop running Ubuntu to apply the steps described in this section.

There are few drivers and firmware which you need to add to your Linux in order to avoid the unnecessary hassle with connecting external USB keyboard/mouse/stick as most of the howto's suggest.

apple-ib-tb, apple-ibridge, applespi (for the Touchbar, Touchpad, Keyboard) and brcm firmware (for MBP WiFi adapter).

By creating your own remastered Live Ubuntu Linux distribution, you will also have a handy recovery tool you can use at any time.

Get Ubuntu

Download it from its official website https://www.ubuntu.com/download/desktop

Extract the image

Extract the Ubuntu image before you can start remastering it.

cd ~/Downloads
sudo mkdir /mnt/iso
sudo mount ubuntu-20.04.2.0-desktop-amd64.iso /mnt/iso
mkdir customiso
rsync -a --exclude=casper/filesystem.squashfs /mnt/iso/ customiso/
sudo unsquashfs /mnt/iso/casper/filesystem.squashfs
sudo umount /mnt/iso

Chroot to the image

Once the image is extracted, you will need to continue remastering it from inside. To do so, you will need to use chroot.

sudo mount --bind /dev squashfs-root/dev/
sudo chroot squashfs-root/
PS1="(chroot) $PS1"
LC_ALL=C
HOME=/root
export PS1 HOME LC_ALL

mount -t proc none /proc
mount -t sysfs none /sys
mount -t devpts none /dev/pts

Update the image

It is a good idea to keep the software updated.

mv /etc/resolv.conf /etc/resolv.conf.bak
echo 'nameserver 8.8.8.8' | tee /etc/resolv.conf

apt-get update
apt-get -y dist-upgrade
apt-get -y autoremove

DKMS: Keyboard, touchpad and touchbar

To avoid hassle with plugging the external keyboard/mouse, you can already build in the keyboard, touchpad and touchbar drivers.

apt-get install git dkms

cd ~
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 .
CASPER_GENERATE_UUID=1 dkms install -m applespi -v 0.1 -k $(realpath /boot/vmlinuz | sed 's#/boot/vmlinuz-##g')

# lsinitramfs /boot/initrd.img | grep -i "dkms/apple"
usr/lib/modules/5.8.0-50-generic/updates/dkms/apple-ib-tb.ko
usr/lib/modules/5.8.0-50-generic/updates/dkms/apple-ibridge.ko
usr/lib/modules/5.8.0-50-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:

echo 'options apple_ib_tb fnmode=2' | tee /etc/modprobe.d/apple_ib_tb.conf
CASPER_GENERATE_UUID=1 update-initramfs -u -k $(realpath /boot/vmlinuz | sed 's#/boot/vmlinuz-##g')

You can also swap fn and a control keys:

echo 'options applespi fnremap=1' | tee /etc/modprobe.d/applespi.conf
CASPER_GENERATE_UUID=1 update-initramfs -u -k $(realpath /boot/vmlinuz | sed 's#/boot/vmlinuz-##g')

Or remap it to any other key:

# 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)

Touchpad configuration

Next, you need to set the proper dpi for the touchpad:

cat > /etc/udev/hwdb.d/61-evdev-local.hwdb << 'EOF'
# MacBook8,1 (2015), MacBook9,1 (2016), MacBook10,1 (2017)
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBook8,1:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBook9,1:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBook10,1:*
 EVDEV_ABS_00=::95
 EVDEV_ABS_01=::90
 EVDEV_ABS_35=::95
 EVDEV_ABS_36=::90

# MacBookPro13,* (Late 2016), MacBookPro14,* (Mid 2017)
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro13,1:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro13,2:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro14,1:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro14,2:*
 EVDEV_ABS_00=::96
 EVDEV_ABS_01=::94
 EVDEV_ABS_35=::96
 EVDEV_ABS_36=::94

evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro13,3:*
evdev:name:Apple SPI Touchpad:dmi:*:svnAppleInc.:pnMacBookPro14,3:*
 EVDEV_ABS_00=::96
 EVDEV_ABS_01=::95
 EVDEV_ABS_35=::96
 EVDEV_ABS_36=::95
EOF
cat > /etc/udev/hwdb.d/61-libinput-local.hwdb << 'EOF'
libinput:name:*Apple SPI Touchpad*:dmi:*
 LIBINPUT_MODEL_APPLE_TOUCHPAD=1
 LIBINPUT_ATTR_KEYBOARD_INTEGRATION=internal
 LIBINPUT_ATTR_TOUCH_SIZE_RANGE=200:150
 LIBINPUT_ATTR_PALM_SIZE_THRESHOLD=1200
EOF

(OPTIONAL): You can skip this section! There is a way to get your MBP's wifi adapter working, so you do not need the USB WiFi TP-LINK dongle. See the next section Enable MBP's WiFi adapter.

TP-Link TL-WN722N v2 support has been added to the Linux kernel starting v4.13-rc2 https://github.com/torvalds/linux/commit/5a1d4c5dd4eb2f1f8a9b30e61762f3b3b564df70
So you can skip this section entirely if you happened to be using a recent kernel.

In order to enable the external TP-LINK TL-WN722N USB WiFi adapter, you need to make sure the Ubuntu image has the necessary driver.

apt-get install git dkms

cd ~
echo -e "\n# TP-LINK TL-WN722N\n8188eu" >> /etc/initramfs-tools/modules

git clone https://github.com/lwfinger/rtl8188eu.git
cd rtl8188eu
make all
make install
CASPER_GENERATE_UUID=1 update-initramfs -u -k $(realpath /boot/vmlinuz | sed 's#/boot/vmlinuz-##g')

# lsinitramfs /boot/initrd.img | grep -i 8188eu
usr/lib/modules/5.8.0-50-generic/kernel/drivers/net/wireless/8188eu.ko

Enable MBP's WiFi adapter

MBP's Broadcom wifi adapter working in Linux!
All that needs to be done is placing the right brcmfmac43602-pcie.txt file into a /lib/firmware/brcm/ directory.

Get brcmfmac43602-pcie.txt from https://bugzilla.kernel.org/show_bug.cgi?id=193121#c74  (should it disappear one day, let me know, I'll share / re-upload it)

(chroot) root@z390-linux:~# ls -la /lib/firmware/brcm/brcmfmac43602-pcie.txt
-rw-rw-r-- 1 root root 6051 Apr 23 11:34 /lib/firmware/brcm/brcmfmac43602-pcie.txt
(chroot) root@z390-linux:~# sha256sum /lib/firmware/brcm/brcmfmac43602-pcie.txt
a74cbb0abc4e8a437e383442dd44a96263079c102787fbe0758e67614ca79df2  /lib/firmware/brcm/brcmfmac43602-pcie.txt

Make sure it gets to the initramfs:

cat > /etc/initramfs-tools/hooks/copy-missing-brcm-firmware << 'EOF'
#!/bin/sh -e
# Copy missing firmware files for brcmfmac driver
PREREQ=""
prereqs () { echo "${PREREQ}"; }
case "${1}" in prereqs) prereqs; exit 0 ;; esac ;
. /usr/share/initramfs-tools/hook-functions
echo "Copying /lib/firmware/brcm to the initramfs..."
cp -pr /lib/firmware/brcm ${DESTDIR}/lib/firmware/
EOF

chmod +x /etc/initramfs-tools/hooks/copy-missing-brcm-firmware

CASPER_GENERATE_UUID=1 update-initramfs -u -k $(realpath /boot/vmlinuz | sed 's#/boot/vmlinuz-##g')

(chroot) root@z390-linux:~# lsinitramfs /boot/initrd.img | grep -i brcmfmac43602-pcie
usr/lib/firmware/brcm/brcmfmac43602-pcie.ap.bin
usr/lib/firmware/brcm/brcmfmac43602-pcie.bin
usr/lib/firmware/brcm/brcmfmac43602-pcie.txt

Exit chroot environment

Exit the chroot environment when you are done modifying the Ubuntu image.

rm /var/lib/dbus/machine-id
rm /etc/resolv.conf
mv /etc/resolv.conf.bak /etc/resolv.conf
umount /dev/pts
umount /sys
umount /proc
rm /root/.bash_history
unset HISTFILE
exit
sudo umount squashfs-root/dev/

Update the kernel

To ensure the remastered Ubuntu boots the latest Linux kernel which you have installed in the chrooted system.

sudo cp squashfs-root/boot/vmlinuz customiso/casper/vmlinuz
sudo cp squashfs-root/boot/initrd.img customiso/casper/initrd

Build remastered Ubuntu image

The following steps are to build the remastered Ubuntu image.
When running xorriso, keep in mind the order of arguments is important.

sudo rm customiso/casper/filesystem.squashfs
sudo mksquashfs squashfs-root customiso/casper/filesystem.squashfs

# without the correct `casper-uuid-generic` casper will fail to boot.
# alternatively, you can add `ignore_uuid` to kernel cmdline.
unmkinitramfs customiso/casper/initrd /tmp/z
sudo cp /tmp/z/main/conf/uuid.conf customiso/.disk/casper-uuid-generic
rm -rf /tmp/z

cd customiso
sudo rm md5sum.txt
sudo find -type f -print0 | xargs -0 sudo md5sum | grep -Ev "./md5sum.txt|./isolinux/" | sudo tee md5sum.txt
cd ..

sudo apt -y install xorriso isolinux

rm ubuntu4mac.iso
sudo xorriso -as mkisofs \
  -r -V "UBUNTU4MAC" -R -l -o ubuntu4mac.iso \
  -c isolinux/boot.cat -b isolinux/isolinux.bin \
  -no-emul-boot -boot-load-size 4 -boot-info-table \
  -isohybrid-mbr /usr/lib/ISOLINUX/isohdpfx.bin \
  -eltorito-alt-boot \
  -e boot/grub/efi.img \
  -no-emul-boot -isohybrid-gpt-basdat customiso/

sudo chown $(id -u):$(id -g) ubuntu4mac.iso

Test the new image

Make sure your remastered image boots, before writing the it to your USB stick.

Out of the following methods, you would likely only need UEFI -> USB.

Install the UEFI firmware and qemu for running virtual machines:

sudo apt-get -y install ovmf qemu-system-x86

Legacy (BIOS)

CDROM: qemu-system-x86_64 -enable-kvm -m 1G -k en-us -cdrom ubuntu4mac.iso
USB: qemu-system-x86_64 -enable-kvm -m 1G -k en-us -device nec-usb-xhci,id=xhci -drive if=none,id=stick,file=ubuntu4mac.iso,format=raw -device usb-storage,bus=xhci.0,drive=stick

UEFI

CDROM: qemu-system-x86_64 -enable-kvm -m 1G -k en-us -bios /usr/share/ovmf/OVMF.fd -cdrom ubuntu4mac.iso
USB: qemu-system-x86_64 -enable-kvm -m 1G -k en-us -bios /usr/share/ovmf/OVMF.fd -device nec-usb-xhci,id=xhci -drive if=none,id=stick,file=ubuntu4mac.iso,format=raw -device usb-storage,bus=xhci.0,drive=stick

Installing the Linux

Boot your MacBookPro from your newly remastered Ubuntu - ubuntu4mac.iso and create partitions you are going to be installing it to.

Install as normal.

All the modifications which were done to the remastered ISO before will be applied to your installation!


Extra hints

Swap command and option keys

Once you have logged-in under your user in the Gnome/Unity session, run the following command to permanently swap command and option keys:

$ gsettings set org.gnome.desktop.input-sources xkb-options "['altwin:swap_alt_win']"

Windows in QEMU/KVM

You can download Windows 10 for free and install/use it in QEMU/KVM.
https://www.microsoft.com/software-download/windows10

Steam in Docker

As a great surprise for some, you can play games in Linux on your MacBookPro using AMD Radeon Pro 560.
For that you can either install Steam on your own or use my Steam image which is much simpler https://hub.docker.com/r/andrey01/steam/

Grub to show options

This is important as one day you may get a Linux kernel that won't boot due to some problem. To prevent this, make sure you are able to choose the kernel when your MBP starts.
To do this, remove the GRUB_HIDDEN_TIMEOUT* lines from the /etc/default/grub file and make sure GRUB_TIMEOUT is set to some reasonable value, e.g. 5 (without quotes).
Then regenerate grub config by running sudo grub-mkconfig -o /boot/grub/grub.cfg command.
This will allow you to see the grub loader options on boot and let you pick a different kernel in case if the last one was having troubles to boot/or any other troubles.

Grub to show macOS High Sierra option

Add the following code to /etc/grub.d/40_custom file:

menuentry "macOS High Sierra (hfsplus)" {
  insmod hfsplus
  search --set=root --file /System/Library/CoreServices/boot.efi
  chainloader /System/Library/CoreServices/boot.efi
}

Then regenerate grub config by running sudo grub-mkconfig -o /boot/grub/grub.cfg command.

After that you do not need to hold the "option" button to load macOS when restarting your MBP.

When upgrading a Linux kernel

Make sure your kernel drivers have been compiled each time you are getting a Linux kernel upgrade. For that, run dkms status command and see whether the module has installed status there.

Might need to repeat the steps in Module: USB WiFi TP-LINK TL-WN722N.

Troubleshooting

macOS got an update and GRUB is not booting anymore

One day you may find yourself in a situation when only macOS is booting, while GRUB boot loader is being completely skipped.

Do not worry, you can set your Linux boot option back from macOS itself.

You can do this by using the bootoption tool

  1. Install Xcode from the App Store.
  2. Install bootoption:
brew upgrade
brew tap vulgo/repo
brew install bootoption
  1. List your UEFI boot preferences:
$ bootoption list
BootCurrent: Boot0080
BootNext: Not set
Timeout: Not set
--: Boot0081 Mac OS X
--: Boot0082
1: Boot0080 Mac OS X
2: Boot0000 ubuntu

You can also see more info for each option:

$ bootoption info Boot0000
Name: Boot0000
Description: ubuntu
Device path: \MEDIA_HARDDRIVE_DP\MEDIA_FILEPATH_DP
Partition UUID: REDACTED
Loader path: \EFI\ubuntu\shimx64.efi

$ bootoption info Boot0080
Name: Boot0080
Description: Mac OS X
Device path: \ACPI_DP\HW_PCI_DP\HW_PCI_DP\MSG_SASEX_DP\MEDIA_HARDDRIVE_DP
Partition UUID: REDACTED
  1. Set ubuntu to be 1st in your boot order:
$ sudo bootoption order 2 to 1
Error: Service unavailable (69)

If you get this error, do not worry, this is due to the SIP (System Integrity Protection). All you need is just to disable it (you can enable it later):

1. Restart the computer, while booting hold down Command-R to boot into recovery mode.
2. Once booted, navigate to the "Utilities > Terminal" in the top menu bar.
3. Enter csrutil disable in the terminal window and hit the return key.
4. Restart the machine and System Integrity Protection will now be disabled.

After that the commands will work as expected:

$ sudo bootoption order 2 to 1
$ bootoption list
BootCurrent: Boot0080
BootNext: Not set
Timeout: Not set
--: Boot0081 Mac OS X
--: Boot0082
1: Boot0000 ubuntu
2: Boot0080 Mac OS X
You can also use sudo bootoption set --bootnext 0000 command for one-time boot.

References

- Ubuntu 18.04 Linux on mid-2017 MacBookPro
- Linux On MBP Late 2016
- MBP 2016 Linux

If you found this useful, please consider making a donation!

Donations

  • crypto3$paystring.crypto.com
  • BTC: bc1qvelutkakstymn244km9kgdd80kdyqnx3wahjcv

Alternative ways to make a donation are in the About section https://nixaid.com/about/