chroot allows us have a separate and temporary system for development and development without high overhead such as with virtual machines.

An Ubuntu chroot environment could easily be created using debootstrap tool. This is basically a similar process to building a chroot environment for Debian.

Steps to build Ubuntu chroot environment:

  1. Launch terminal application.
  2. Update apt's package repository.
    $ sudo apt update
    [sudo] password for user:
    Hit:1 http://jp.archive.ubuntu.com/ubuntu focal InRelease
    Hit:2 http://jp.archive.ubuntu.com/ubuntu focal-updates InRelease
    Hit:3 http://jp.archive.ubuntu.com/ubuntu focal-backports InRelease
    Hit:4 http://jp.archive.ubuntu.com/ubuntu focal-security InRelease
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    All packages are up to date.
  3. Install debootstrap using apt.
    $ sudo apt install --assume-yes debootstrap
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    Suggested packages:
      arch-test squid-deb-proxy-client
    The following NEW packages will be installed:
      debootstrap
    0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
    Need to get 39.4 kB of archives.
    After this operation, 299 kB of additional disk space will be used.
    Get:1 http://jp.archive.ubuntu.com/ubuntu focal-updates/main amd64 debootstrap all 1.0.118ubuntu1.1 [39.4 kB]
    Fetched 39.4 kB in 1s (66.1 kB/s)
    Selecting previously unselected package debootstrap.
    (Reading database ... 107133 files and directories currently installed.)
    Preparing to unpack .../debootstrap_1.0.118ubuntu1.1_all.deb ...
    Unpacking debootstrap (1.0.118ubuntu1.1) ...
    Setting up debootstrap (1.0.118ubuntu1.1) ...
    Processing triggers for man-db (2.9.1-1) ...
  4. Create a chroot folder.
    $ mkdir chroot-ubuntu
  5. Create a base Ubuntu system using debootstrap on the newly-created folder.
     $ sudo debootstrap --variant=buildd focal chroot-ubuntu
    I: Retrieving InRelease
    I: Checking Release signature
    I: Valid Release signature (key id F6ECB3762474EDA9D21B7022871920D1991BC93C)
    I: Retrieving Packages
    I: Validating Packages
    I: Resolving dependencies of required packages...
    I: Resolving dependencies of base packages...
    I: Checking component main on http://archive.ubuntu.com/ubuntu...
    I: Retrieving adduser 3.118ubuntu2
    I: Validating adduser 3.118ubuntu2
    I: Retrieving apt 2.0.2
    I: Validating apt 2.0.2
    I: Retrieving base-files 11ubuntu5
    I: Validating base-files 11ubuntu5
    ##### snipped

    Change focal to any Ubuntu release code name that's still supported.
    List of Ubuntu releases

    More options for debootstrap.

    $ debootstrap --help
    Usage: debootstrap [OPTION]... <suite> <target> [<mirror> [<script>]]
    Bootstrap a Debian base system into a target directory.
    
          --help                 display this help and exit
          --version              display version information and exit
          --verbose              don't turn off the output of wget
    
          --download-only        download packages, but don't perform installation
          --print-debs           print the packages to be installed, and exit
    
          --arch=A               set the architecture to install (use if no dpkg)
                                   [ --arch=powerpc ]
    
          --include=A,B,C        adds specified names to the list of base packages
          --exclude=A,B,C        removes specified packages from the list
          --extra-suites=A,B,C   also use packages from the listed suites of the
                                 archive
          --components=A,B,C     use packages from the listed components of the
                                 archive
          --variant=X            use variant X of the bootstrap scripts
                                 (currently supported variants: buildd, fakechroot,
                                  minbase)
          --no-merged-usr        do not make /{bin,sbin,lib}/ symlinks to /usr/
          --keyring=K            check Release files against keyring K
          --no-check-gpg         avoid checking Release file signatures
          --force-check-gpg      force checking Release file signatures
                                 (also disables automatic fallback to HTTPS in case
                                 of a missing keyring), aborting otherwise
          --no-resolve-deps      don't try to resolve dependencies automatically
          --log-extra-deps       record extra dependency info in debootstrap.log
          --cache-dir=DIR        Use specified directory as package cache directory
    
          --unpack-tarball=T     acquire .debs from a tarball instead of http
          --make-tarball=T       download .debs and create a gzipped tarball
          --second-stage-target=DIR
                                 Run second stage in a subdirectory instead of root
                                   (can be used to create a foreign chroot)
                                   (requires --second-stage)
          --extractor=TYPE       override automatic .deb extractor selection
                                   (supported: dpkg-deb ar)
          --debian-installer     used for internal purposes by debian-installer
          --private-key=file     read the private key from file
          --certificate=file     use the client certificate stored in file (PEM)
          --no-check-certificate do not check certificate against certificate authorities
  6. Mount proc, sys and dev filesystem on to the base system.
    $ sudo mount -t proc /proc chroot-ubuntu/proc
    $ sudo mount --rbind /sys chroot-ubuntu/sys
    $ sudo mount --rbind /dev chroot-ubuntu/dev
  7. chroot to the folder .
    $ sudo chroot chroot-ubuntu /bin/bash
    bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
    [email protected]:/#
  8. Use the chroot environment as required.
    [email protected]:/# ls
    bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var
    [email protected]:/# uname -a
    Linux host 5.4.0-33-generic #37-Ubuntu SMP Thu May 21 12:53:59 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
    [email protected]:/# df -h
    Filesystem      Size  Used Avail Use% Mounted on
    tmpfs           2.0G     0  2.0G   0% /sys/fs/cgroup
    udev            1.9G     0  1.9G   0% /dev
    tmpfs           2.0G     0  2.0G   0% /dev/shm
  9. Unmount the mounted proc, sys and dev filesystem once exiting the chroot environment.
    $ sudo umount chroot-ubuntu/proc chroot-ubuntu/sys chroot-ubuntu/dev 
  10. Remove the chroot folder if necessary.
    $ sudo rm -rf chroot-folder

Guide compatibility:

Operating System
Ubuntu 16.04 LTS (Xenial Xerus)
Ubuntu 16.10 (Yakkety Yak)
Ubuntu 17.04 (Zesty Zapus)
Ubuntu 17.10 (Artful Aardvark)
Ubuntu 18.04 LTS (Bionic Beaver)
Ubuntu 18.10 (Cosmic Cuttlefish)
Ubuntu 19.04 (Disco Dingo)
Discuss the article:

Comment anonymously. Login not required.

Share!