Creating an 18.04 environment with ROS melodic on a 20.04 system

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

This setup is for installing an 18.04 environment with ROS melodic on a 20.04 host system. If you want it the other way around (hosting an 18.04 system and creating a 20.04 environment) go back 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 18.04 bionic). Open a new conf file first. Here gedit is used, feel free to use your favorite editor instead.

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

Now put the following settings into it. Don't forget to put the username of your host machine as users.

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

Create the mounting point for the new environment.

sudo mkdir -p /srv/chroot/bionic

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

sudo debootstrap --variant=buildd --arch=amd64 bionic /srv/chroot/bionic 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 bionic

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 bionic

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 bionic universe" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb http://archive.ubuntu.com/ubuntu bionic multiverse" >> /etc/apt/sources.list'
sudo sh -c 'echo "deb http://archive.ubuntu.com/ubuntu bionic restricted" >> /etc/apt/sources.list'

Now for the ROS install do everything you'd usually do to install ROS on your machine. Since it is an 18.04 bionic environment we are installing ROS melodic.

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-melodic-desktop-full

ROS is now installed in the schroot environment. Now come some python packages for rosdep and build-scripts. For 18.04 stick to python 2.7 packages, which is the default on bionic systems.

sudo apt install python-rosdep python-rosinstall python-rosinstall-generator python-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 (== “bionic”) 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} == "bionic" ]]; then
  echo "Ubuntu 18.04"
  unset PYTHONPATH
  source /opt/ros/melodic/setup.bash > /dev/null
  eval `ssh-agent -s` > /dev/null
else
  echo "Ubuntu 20.04"
  source /opt/ros/noetic/setup.bash
fi

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

alias melodic='schroot -r -p -c melodic' # connects to the schroot session
alias start_melodic='schroot -b -p -c bionic -n melodic' # creates a persistent schroot session
alias stop_melodic='schroot -e -c melodic' # 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 18.04 environment from your 20.04 machine. Run packages for melodic while in your 18.04 environment, and those for noetic on your host machine.