Skip to content

Instantly share code, notes, and snippets.

@rob-hills
Last active May 3, 2024 17:33
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rob-hills/9134b7352ee7471c4d4f4fbd6454c4b9 to your computer and use it in GitHub Desktop.
Save rob-hills/9134b7352ee7471c4d4f4fbd6454c4b9 to your computer and use it in GitHub Desktop.
Ubuntu 22.04 on MacBook Pro 2017 (A1707, MBP 14,3)

Summary

Random set of notes and links that have helped me in the saga to get Ubuntu 22.04 up and running on my 2017 MacBook Pro These notes are mainly for myself in the event that I have to do this again some day. But if you find them helpful, that's great.

Important update

Unfortunately the MacBookPro that I bought second hand turned out to be a dud with some serious hardware flaws that didn't show up until a couple of months after I'd bought it.

I've since purhcased another laptop so I'm not going to be able to maintain this GIST.

I'll leave it here for now in case others want to contribute updates, or clone it. If you feel it's out of date or misleading or a waste of time, let me know in the comments and I'll remove it.

Details

My MacBook Pro:

root@rob-MacBookPro:~# sudo dmidecode | grep -i mac
	Product Name: MacBookPro14,3
	Family: MacBook Pro
	Product Name: Mac-551B86E5744E2388
	Version: MacBookPro14,3
	Version: Mac-551B86E5744E2388
		MCE (Machine check exception)
		MCA (Machine check architecture)

Hmmm, curious, it says MBP 15.4/3 on the box.

Useful References (not mentioned in text)

Out of the Box

What worked

A lot of online info about running Ubuntu on MBP indicated that you needed to build drivers and install them just to get the basics running. For Ubuntu 22.04LTS and my MBP 1707 this was not the case. Things that worked for me with the standard Ubuntu 22.04LTS Live USB and after initial installation included:

  • The basic keyboard (not the Touchbar with Esc and Function keys though);
  • The touchpad;
  • My ASUS USB-C docking station, providing important infrastructure such as full external keyboard with all keys functional, LAN connection, external monitor;
  • NVMe SSD

What didn't work

  • Wifi: Actually it was partly functional, but unusable. It would find my WLAN access point and would sometimes connect for brief periods, sometimes not. Usually the connection would be extremely slow and would drop out after brief intervals;
  • The Touchbar

The following notes mostly document what worked to get Wifi and the Touchbar working

WiFi

Extensive Googling indicated that the Broadcom driver installed with Ubuntu doesn't work properly on most newer MBPs. However, after installing the recommended driver, I experienced small improvements but was still not getting reliable WiFi.

Eventually I came across a Kernel Bugzilla discussion that led me to a solution. The discussion about this bug is long and technical, but well worth the time to read through from beginning to end.

In particular, Sontal.santan's comment explaining how to create a configuration file for the driver, where to put it and what parameters to tweak was the first step in getting my WiFi to work.

The configuration file needed for my hardware is: /usr/lib/firmware/brcm/brcmfmac43602-pcie.txt

Andy Holst's example configuration files, first one here and final iteration here were ideal and only required a little tweaking for my system.

WiFi Driver Configuration tweaks

I made the following changes to Andy Holst's /usr/lib/firmware/brcm/brcmfmac43602-pcie.txt

  • macaddr - set to the mac address of my WiFi card
  • ccode - set to your country code, in my case AU (for Australia)

Touchbar

Many thanks to RoadRunner2 for his Gist on getting Ubuntu running on the MBP and for all his work on drivers.

Unfortunately his drivers wouldn't compile against the current Linux kernel in Ubuntu 22.04. Eventually I found Patrick Verner's fork of the macbook12-spi-driver which did compile. But still my touchbar wasn't working.

Reading through the discussion on RoadRunner2's version, I came across others with the same issue and this pointed me towards this workaround which finally lit up my touchbar!

Step by Step touchbar

Become the superuser

sudo su

Ubuntu uses initramfs so add our new modules to the list to be loaded

cat <<EOF | tee -a /etc/initramfs-tools/modules
# drivers for keyboard+touchpad
applespi
apple-ib-tb
intel_lpss_pci
spi_pxa2xx_platform
EOF

Now, install prerequisites, download and build the drivers

apt install dkms

cd {your preferred source download folder}
git clone https://github.com/PatrickVerner/macbook12-spi-driver.git
pushd macbook12-spi-driver
git checkout touchbar-driver-hid-driver
ln -s `pwd` /usr/src/applespi-0.1
dkms install applespi/0.1
popd

Test the drivers by loading them and their dependencies

sudo modprobe intel_lpss_pci spi_pxa2xx_platform applespi apple-ib-tb

Null output indicates success

Reboot

At this point of the process, my touchbar was not working. More Googling led me to someone else logging this as [an issue on RoadRunner2's Driver repository[(roadrunner2/macbook12-spi-driver#42] and in the discussion I found a workaround. The following commands:

echo '1-3' | sudo tee /sys/bus/usb/drivers/usb/unbind
echo '1-3' | sudo tee /sys/bus/usb/drivers/usb/bind

caused my touchbar to light up!!

To save having to run these commands each time I start the computer, I needed to create a "macbook-quirks.service" and have it start up with the computer:

sudo su
cat <<EOF | tee /etc/systemd/system/macbook-quirks.service
[Unit]
Description=Re-enable MacBook 14,3 TouchBar
Before=display-manager.service

[Service]
Type=oneshot
ExecStartPre=/bin/sleep 2
ExecStart=/bin/sh -c "echo '1-3' > /sys/bus/usb/drivers/usb/unbind"
ExecStart=/bin/sh -c "echo '1-3' > /sys/bus/usb/drivers/usb/bind"
RemainAfterExit=yes
TimeoutSec=0

[Install]
WantedBy=multi-user.target
EOF

Now enable with systemctl enable macbook-quirks.service and reboot to check.

My MBP now boots up with the touchbar working!

From the discussion in the link above, it appears this may be due to a bug in Linux and the usbmuxd system so hopefully will be able to remove the workaround in the future, so keep an eye on the relevant issue discussion.

Touchbar Tweaking

I wanted the Function keys to appear by default (instead of the brightness and sound keys) so I needed to pass some parameters to the apple_ib_tb module. 'modinfo apple_ib_tb' lists the parameters and what they do.

sudo su
cat <<EOF | tee /etc/modprobe.d/apple_ib_tb.conf
options apple_ib_tb fnmode=2           # Default to Function Keys, Fn key toggles to "special"
options apple_ib_tb idle_timeout=60    # Turn off touchbar after 60 seconds.
EOF

After reloading the module with

sudo modprobe -r apple_ib_tb
sudo modprobe apple_ib_tb

now the Function keys show by default!

@crp0499
Copy link

crp0499 commented Jul 7, 2023

So sorry, but noob here...the portion above that reads:
cat <<EOF | tee -a /etc/initramfs-tools/modules

drivers for keyboard+touchpad

applespi
apple-ib-tb
intel_lpss_pci
spi_pxa2xx_platform
EOF

Am I just dropping that whole thing into terminal? I'm not sure.

Thank you by the way! :)

@hamishfor
Copy link

Yes. To break the command down, cat will write to stdout, pipe ( | ) will pass the output to another program, while tee will capture stdout and write to a file and stdout at the same time.

You can read more about the bash syntax here: https://stackoverflow.com/questions/2500436/how-does-cat-eof-work-in-bash

@MCMuser1
Copy link

The link for the keyboard (PatrickVerner) appears to be dead. I used 'git clone https://github.com/roadrunner2/macbook12-spi-driver.git' instead which I found elsewhere and seems to be active. When running that section though, I get an error at the end,

git clone https://github.com/roadrunner2/macbook12-spi-driver.git
pushd macbook12-spi-driver
git checkout touchbar-driver-hid-driver
ln -s pwd /usr/src/applespi-0.1
dkms install applespi/0.1
popd
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
dkms is already the newest version (2.8.7-2ubuntu2.2).
0 upgraded, 0 newly installed, 0 to remove and 2 not upgraded.
bash: cd: too many arguments
fatal: destination path 'macbook12-spi-driver' already exists and is not an empty directory.
~/macbook12-spi-driver ~
Already on 'touchbar-driver-hid-driver'
Your branch is up to date with 'origin/touchbar-driver-hid-driver'.
ln: failed to create symbolic link '/usr/src/applespi-0.1/macbook12-spi-driver': File exists

Kernel preparation unnecessary for this kernel. Skipping...

Building module:
cleaning build area...
make -j8 KERNELRELEASE=6.2.0-36-generic...(bad exit status: 2)
ERROR (dkms apport): binary package for applespi: 0.1 not found
Error! Bad return status for module build on kernel: 6.2.0-36-generic (x86_64)

@gjvanderheiden
Copy link

https://aur.archlinux.org/packages/macbook12-spi-driver-dkms
I've used a AUR package on Arch Linux. There's a patch file in this package.

I also used a local patch on /usr/lib/udev/rules.d/39-usbmuxd.rules described in
libimobiledevice/usbmuxd#138
after release 1.1.1 of usbmuxd this should be solved. With this patch there's no need for
echo '1-3' | sudo tee /sys/bus/usb/drivers/usb/unbind
echo '1-3' | sudo tee /sys/bus/usb/drivers/usb/bind
And it is solved at the cause of the problem.

@gjvanderheiden
Copy link

On real PITA is suspend. My MacBook14,3 freezes and I have to do a hard hardware reset. Even Hibernate doesn't work

@hamishfor
Copy link

Did you manage to solve suspend @gjvanderheiden?

@gjvanderheiden
Copy link

@hamishfor Sorry for the late response, didn't see you where mentioning me.

When I do this:
have the bios think I run Mac OS
do a switchgpu -i
reboot
switch to iGPU with switcheroo
modprobe -r amdgpu (otherwise doesn't work)
Is does go in hibernate
Then the Broadcom iffy wifi crashes
Then I can do a suspend (not sure if I need to do the switcheroo and modprobe -r again)

So in short, closer of better but working? Not really.

My some tricky stuff with unloading the Broadcom driver before Hibernate sleep.

I found it very difficult to see where the problem is. Is it the gmux, switcheroo, amdgpu driver? When I do the switcheroo stuff it logs into dmesg it has an error going into hibernate / sleep. But it is already powered off with switcheroo.

Also gpuswitch -I then reboot results in a wrong resolution on the terminal. Hyprland works fine (waybar too if you do switcheroo and unload the amdgpu kernel module). If you don't do a gpuswitch -I and run the switcheroo stuff, my screen goes black. External monitor on the amdgpu didn't work last time I tried (you must use amdgpu for external monitor, because of the gmux). I even looked in the kernel source code. The apple gmux code looks very good and has some very useful documentation, it even has mentions chip names used, datasheets and how it is connected. I tried to see if I can set the iGPU in an other display mode or something, but I couldn't figure out how that works on MacOS. Did find the kernel module, but no further luck. I thought maybe I can get the switcheroo to work better with kernnel params, in combination with the gmux, as the gmux setup does some tricky stuff.

Sound doesn't work well with a wired headphone. I'm not enjoying Linux on my laptop very much because of this. The amount of effort to make this laptop work (to some extend) reminds me of the earlier days (2000 decade) of using Linux on a laptop. Meh. I now use Arch Linux on an older 2014 Mac mini, with a ssd mod in it, it runs like a dream. No driver issue and suspends without any issues or effort one my side.

@ev4n32
Copy link

ev4n32 commented Mar 9, 2024

@rob-hills @MCMuser1 Do you have the Patrick Verner driver saved somewhere? The repo is dead.

@bennettforkner
Copy link

@rob-hills @MCMuser1 Do you have the Patrick Verner driver saved somewhere? The repo is dead.

Also wondering on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment