Containerize MATLAB with systemd-nspawn

Install MATLAB in Debian container inside Arch Linux to avoid compatibility bugs

Posted by Xiping Hu on December 3, 2020

Set up Debian 9 environment

Change directory

1
2
sudo su
cd /var/lib/machines

Install debootstrap and debian-archive-keyring

1
sudo pacman -S debootstrap debian-archive-keyring

Use debootstrap to set up a debian environment

1
debootstrap --include=systemd-container --components=main,universe stretch matlab-debian9 https://mirrors.163.com/debian

Set password for Debian

1
2
systemd-nspawn -D ./matlab-debian9/
passwd

Install Firefox and run

Edit /etc/apt/sources.list

1
2
systemd-nspawn -D ./matlab-debian9
deb https://mirrors.163.com/debian stretch main contrib non-free

Install Firefox

1
apt install firefox-esr

Exit the container

1
exit

And enable xhost

1
xhost +local:

Then run Firefox inside container

1
2
3
4
5
systemd-nspawn --setenv=DISPLAY=:0 \
               --setenv=XAUTHORITY=~/.Xauthority \
               --bind-ro=$HOME/.Xauthority:/root/.Xauthority \
               --bind=/tmp/.X11-unix \
               -D /var/lib/machines/matlab-debian9 firefox

Install MATLAB

mount the MATLAB iso

1
2
sudo mkdir /mnt/matlab
sudo mount -t iso9660 'Matlab98R2020a_Linux 64.iso' /mnt/matlab

boot into the container

1
sudo systemd-nspawn -b -D /var/lib/machines/matlab-debian9

install requisite libraries

1
apt-get install xorg build-essential libgtk2.0-0 libnss3 libasound2

set a lower limit of number of open files by

1
ulimit -n 100000

and then change directory into /mnt/matlab and install MATLAB

then install and run MATLAB

1
/usr/local/Polyspace/R2020a/bin/matlab