CARLA on Ubuntu 20.04 with Docker
By: Antoine Caillot
Tags: Carla, Autonomous Cars, Ubuntu, Python, Docker

Today, CARLA is compatible with Ubuntu 18.04 but not with the 20.04 version yet. In this document, we run CARLA in a docker image and run python scripts using Carla’s PythonAPI on our machine.

This document has originally been written with the CARLA version 0.9.10 on Ubuntu 20.04. Updated for CARLA 0.9.11.

At the end of this article, a script to set up a CARLA dev environment automatically is available.

Docker

Here, we just have to load the docker made by the CARLA teams from the Docker Hub. However, to use CARLA in Docker, you will have to install Nvidia-docker-2 which instructions are available here. Afterwards, you just need to run:

docker pull carlasim/carla:latest

If you require the version tested with this document :

docker pull carlasim/carla:0.9.10.1

Open the container

Now we just have to open the container and run a bash from it.

Container’s parameters

To enable all the required features, we need to set some parameters :

Ports used : 2000 to 2002 -p 2000-2002:2000-2002

Runtime: nvidia --runtime=nvidia

GPUs used: all --gpus all

Create a terminal and read the inputs -ti

Display:

Redirect the display stream: -e DISPLAY=$DISPLAY

Redirect the X11 pipeline: -v /tmp/.X11-unix:/tmp/.X11-unix (Share the X11 folder in a volume)

Final command to run

Finally, the command to run the container is as follow :

docker run \

-p 2000-2002:2000-2002 \

--runtime=nvidia \

--gpus all \

-e DISPLAY=$DISPLAY \

-v /tmp/.X11-unix:/tmp/.X11-unix \

-it \

carlasim/carla \

bash

You should be entering into the bash of the CARLA and your terminal may look like :

carla@45a52fa744e1:~$

We will need the container’s ID for later (for me, it is 45a52fa744e1), so you can copy it ( Ctrl + Shift + C ).

Note : To close it, you can use the shortcut Ctrl + D or the command exit .

Setting up the local environment

Grabbing the egg

Within the docker image, the CARLA teams give yon an egg file. If you look in the container, you will find it in ~/PythonAPI/carla/dist and for me it looks like ~/PythonAPI/carla/dist/carla-0.9.10-py3.7-linux-x86_64.egg . Thus, we are going to copy the PythonAPI folder on the host file system.

Here, we will need the container’s ID we found in the last section. If you closed the container before keeping it, you can get it back with the command.

$ docker ps -a CONTAINER ID 45a52fa744e1

IMAGE carlasim/carla

COMMAND "bash"

CREATED About an hour ago

STATUS Exited (0) About a minute ago

PORTS

NAMES crazy_jackson

....

Note: I reordered the output for better readability.

Now we can use the following command template to export the PythonAPI folder :

docker cp <container id>:<src path> <dst path>

In my case :

docker cp 45a52fa744e1:/home/carla/PythonAPI ~/Documents/

We can now move to the PythonAPI on our host machine : cd ~/Documents/PythonAPI/ .

Python 3.7

You certainly noticed the mention py3.7 in the egg file name. It means we need to use the py3.7version 3.7 of python (3.8 doesn’t work).

To install that version we can add the following PPA :

add-apt-repository ppa:deadsnakes/ppa

And then install Python 3.7 :

sudo apt-get install python3.7

As well as pip3:

sudo apt-get install python3-pip

We will also need to set up a virtual environment and hence need virtualenv through pip3.

pip install virtualenv

Now we have to set up the virtual environment in the folder PythonAPI. In the PythonAPI directory (for me ~/Documents/PythonAPI/ )

virtualenv --python=/usr/bin/python3.7 venv

We now need to install numpy and pygame

source venv/bin/activate

pip3 --version #it should display (python 3.7)

pip3 install pygame numpy

deactivate

You are now all set to run CARLA and a script to spawn 80 cars in the environment!

Run CARLA Script

We need two terminals (I expect you closed the previous one we created).

Terminal 1:

In this terminal, we will run CARLA and get a graphical interface.

You can run CARLA from the container :

Run CARLA in the Docker container

You may not need the graphical interface and can deal with an “headless mode”. in that case run the following command:

Run CARLA in headless mode from the Docker container

At this step, you should have a window with an environment but there is no car or anything yet. Unless you run in headless mode

Terminal 2:

In this terminal, we will run the scripts to spawn our 80 cars.

Move to the PythonAPI directory: cd ~/Documents/PythonAPI Enter in the python virtual environment : source venv/bin/activate Move to the examples scripts folder : cd ./examples/ Run the script: ./spawn_npc.py -n 80

If you go back to the CARLA window, you can see some animation now!

UPDATE: Use the script to setup automatically any version of CARLA

First you have to install the required stuff as explained earlier:

Install Nvidia-docker-2 which instructions are available here. Install Python 3.7:

add-apt-repository ppa:deadsnakes/ppa

sudo apt-get install python3.7

sudo apt-get install python3-pip

pip install virtualenv

You are now all set, you can copy the script in a shell file and run it. It will create a folder (CARLA_X.X.X) with everything required in the folder where you ran the script.

Grab the CARLA image, setup everything.

It will pull the CARLA image at the version you wish. To change version, just modify the line 2.

After running the script you can run the simulator with one of the script created ( runCARLA.sh or runCARLA-Headless.sh ) to instantiate the simulator. In a second terminal, navigate to the PythonAPI folder to run an example:

source venv/bin/activate

cd ./examples

./spawn_npc.py -n 80

Here you are, everything is working if vehicles are moving in your CARLA window.

Bibliography

Instructions from : Carla Documentation

More info on a GitHub answer