Setting up an Ubuntu chroot on your Linux distro with schroot
By: Vincent Wang
Tags: Chroot, Ros, Ubuntu

Sometimes, you just need Ubuntu.

In my daily life, I usually use Arch Linux as my daily driver (barring GNUless November). It works amazing, the AUR is a blessing from Harambe, etc etc, all the stuff you’ve probably already heard from Arch users. The problem is that unfortunately, not all things work on Arch; for example, I occasionally need to mess with ROS (Robot Operating System), and the community-maintained AUR distribution seems to always have dependency issues, so I wanted to install the official Ubuntu distribution but still be able to use it cleanly within my Arch system.

This article assumes you’re using an amd64/x86_64 system.

Why not a container?

Yes, containers are nice and pretty epic, but for my use case, I thought it was too much of a hassle — containers are too isolated in this case for my tastes. So for this specific purpose, I wanted to run something a bit lighter and less isolated than a container; chroots are perfect for that.

Enter: schroot+debootstrap

schroot and debootstrap are 2 tools that make managing Debian-based chroots incredibly simple. schroot manages setting up the chroot (it can even mount your home directory, so all your configs are there!), while debootstrap allows you to basically bootstrap your chroot with a Debian derivative with one command.

First, you’ll need to actually install the two tools. Since i use Arch:

pacman -S schroot debootstrap

Next, set up the chroot definition. Edit /etc/schroot/chroot.d/<name>.conf (you can name the chroot whatever you want). Next, put in this content:

[<chroot name>]

description=<description>

directory=/srv/chroot/<chroot name>

root-users=<your user>

type=directory

users=<your user>

This will set up a new chroot at /srv/chroot/<chroot-name>. We can actually make as many chroots as we want; just put another config file in the directory.

Next, make the chroot directory:

sudo mkdir -p /srv/chroot/<chroot name>

And bootstrap it using debootstrap.

sudo debootstrap --arch=amd64 focal /srv/chroot/<chroot name>/ http://archive.ubuntu.com/ubuntu/

This command will actually bootstrap from any Debian derivative, not just Ubuntu. You just need to change the mirror (http://archive.ubuntu.com/ubuntu/) and distribution version (focal). This specific command bootstraps Ubuntu 20.04 (focal).

That’s more or less it! You now have a working Ubuntu chroot. You can run schroot -c <chroot name> to enter the chroot. However, we still need to set some stuff up before it works properly.

Post Setup

After the chroot is set up, we need to do a few more things:

Fix network config (do only once)

Add Ubuntu sandbox users and groups on host system (so apt works) (do only once)

Enable repos

Give your user sudo

Fix the network config by commenting out networks in /etc/schroot/default/nssdatabases. This tells schroot to not copy the networks from host system. If you leave this on in Arch it breaks the network.

Add the sandbox users and groups (run this on the Arch host, not inside the chroot):

sudo useradd -u 124 _apt

sudo useradd -u 939 geoclue

sudo useradd -u 694 man

sudo groupadd crontab

sudo groupadd messagebus

(For the astute, we’re specifying UIDs under 1000 so they don’t show up on your login screen. They’re random numbers.)

Next, actually enter your chroot as root with sudo schroot -c <chroot name> , and add your user to sudo with

usermod -a -G sudo <user>

Next, exit and re-enter your chroot as your normal user ( sudo schroot -c <chroot name> ), and add the universe, multiverse and restricted repos (you don’t have to do this but you probably want to in order to get most of Ubuntu’s packages):

sudo apt update

sudo apt install software-properties-common

sudo add-apt-repository universe

sudo add-apt-repository multiverse

sudo add-apt-repository restricted

You probably want to add focal-security as well, for more up to date packages. Run this command:

echo "deb http://security.ubuntu.com/ubuntu focal-security main" >> /etc/apt/sources.list

as root (sudo -s).

That should be it! Now your Ubuntu chroot will basically work exactly like an actual Ubuntu installation, and you can install all the Ubuntu programs you need without ever leaving your distro of choice. Hope this guide helped!