Creating a 20.04 environment with ROS noetic on an 18.04 system

From http://wiki.ros.org/ROS/Tutorials/InstallingIndigoInChroot

This setup is for installing 20.04 and ROS noetic alongside an 18.04 system on the same machine, without the need to reboot to a different mounting point. If you want it the other way around (hosting a 20.04 system and creating a 18.04 environment) go to this page instead.

Setup schroot env

Install debootstrap and schroot, which enable switching mounting points and environments on the fly.

sudo apt-get install debootstrap schroot

Describe the new environment to build (in this case its 20.04 focal). Open a new conf file first.

sudo gedit /etc/schroot/chroot.d/focal.conf

Now put the following settings into it. Don't forget to replace your own username at 'users' and to remove the comment after the user name.

[focal]
description=ubuntu 20.04
type=directory
directory=/srv/chroot/focal
users=cram ## put the username of your host-machine here
root-groups=root
root-users=root
profile=default

Create the mounting point for the new environment.

sudo mkdir -p /srv/chroot/focal

Initialize the new environment. This will install the base system of the desired OS.

sudo debootstrap --variant=buildd --arch=amd64 focal /srv/chroot/focal http://archive.ubuntu.com/ubuntu/

Use this to check for existing schroot environments on your machine.

schroot -l

Change the environment to the new root.

sudo schroot -c focal

You are now logged in to the new environment as root and can start installing all the stuff you need, starting with sudo and git (and vim, why not). Install bash-completion to tab-complete apt packages.

apt install sudo vim git bash-completion
apt update
apt install lsb-release
apt clean all

This will leave the environment. On the next login you won't be root per default anymore.

exit
schroot -c focal

Setting up ROS in the environment

Login to the schroot environment again. We're going to do a full ROS install in here. First get some general archives.

sudo sh -c 'echo "deb http://archive.ubuntu.com/ubuntu focal universe" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb http://archive.ubuntu.com/ubuntu focal multiverse" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb http://archive.ubuntu.com/ubuntu focal restricted" >> /etc/apt/sources.list'

Now for the ROS install do everything you'd usually do to install ROS on your machine. On this new 20.04 environment we'll need ROS noetic.

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'
sudo apt install curl 
sudo apt install gnupg
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
sudo apt update
sudo apt install ros-noetic-desktop-full

ROS is now installed in the schroot environment. Now come some python packages for rosdep and build-scripts. 20.04 focal needs the python3 packages.

sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential
sudo rosdep init
rosdep update

Final touches

For a little bit more convenience, put the following in your host-machine's bash setup (.bashrc in the home directory). Streaming to /dev/null only prevents these calls from producing output. The conditional (== “focal”) depends on the name of your environment, which was set in the file at the beginning of this page. The PYTHONPATH needs to be reset because focal and bionic use different default versions of python. Also, the ssh-agent needs a restart when the environment changes.

if [[ ${SCHROOT_CHROOT_NAME} == "focal" ]]; then
  echo "Ubuntu 20.04"
  unset PYTHONPATH 
  source /opt/ros/noetic/setup.bash > /dev/null
  eval `ssh-agent -s` > /dev/null
else
  echo "Ubuntu 18.04"
  source /opt/ros/melodic/setup.bash
fi

The following aliases can be used to create a persistent schroot session, which you can easily connect to.

alias noetic='schroot -r -p -c noetic' # connects to the schroot session
alias start_noetic='schroot -b -p -c focal -n noetic' # creates a persistent schroot session
alias stop_noetic='schroot -e -c noetic' # terminates the schroot session

If the following message appears when calling 'start_melodic', a session already exists.

Failed to lock chroot: /var/lib/schroot/session/melodic: Failed to write session file: File exists

Congratulations, you can now connect to the 20.04 environment from your 18.04 machine. Run packages for noetic while in your 20.04 environment, and those for melodic on your host machine.