Enable SPI on UBUNTU 20.04 for Raspberry Pi

What I am trying to do

As part of setting up a robot with ubuntu the library I call to access spi is called spidev. When I do the following lines, I get the following error:

>>> import spidev
>>> spi = spidev.SpiDev()
>>> spi.open(0,1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied
>>>

When I further examine things, I see this:

[onboard:mr2]~/catkin_ws$ ls -la /dev/spi*
crw------- 1 root root 153, 0 Apr  1  2020 /dev/spidev0.0
crw------- 1 root root 153, 1 Apr  1  2020 /dev/spidev0.1

Meaning that they seem to only be accessible to root. I believe it is a really bad idea to change file permissions of the /dev files. So my question is:

How do I enable /dev/spi* on Ubuntu 20.04 on Raspberry Pi?

  • It seems that the common advice is to run raspi-config. However, everything I have seen is that raspi-config is not available, or functional, on Ubuntu just on Raspian.

  • The other more promising advice is to modify one of the various config files. But for Ubuntu 20.04 server for Pi, I cannot find the corresponding ones.

  • One workaround is if I log in as root. So it kind of proves that it's a file permissions issue. But I don't know which file and again, it seems a bad idea to muck with permissions set by the system.

So I think I am barking up the wrong tree, but I don't know what else to try.


Create /etc/udev/rules.d/90-gpio-spi.rules with:

KERNEL=="spidev0.0", OWNER="root", GROUP="spi"
KERNEL=="spidev0.1", OWNER="root", GROUP="spi"

Create the group itself and assign it to an existing user "ubuntu":

sudo groupadd -f --system spi
sudo usermod -a -G spi ubuntu

Restart

sudo shutdown -r now

And try again.

Source relating to issue with GPIO on Raspberry Pi