Install Carla Simulator in Ubuntu 18.04

Hi there, dear reader! Today I would like to share with you my experience of installing the Carla simulator in Ubuntu 18.04.

Note: Soon I will be publishing a new guide on how to install Carla-ROS-Bridge together with this version of the simulator. Stick around :)

Edit: The Carla-ROS bridge installation guide is already published. Please check it here!

What is the Carla Simulator?

For those of you who are curious about what Carla simulator is, in its purest form, it is an open-source hyper-realistic autonomous driving simulator built for helping researchers to easily test autonomous driving features. Some of its main features include scalability due to its client-server architecture, a flexible client API, different traffic scenarios and maps support as well as ROS integration. These make it an extremely useful and fun tool to explore and research about autonomous driving with realistic behaviors.

If you would like to know more in-depth about this tool please take a look at the official documentation here which explains in detail what Carla offers and how it is internally designed.

Installation Goals

  • Docker: v 20.3
  • NVIDIA-Docker: v
  • Carla Simulator: v 0.9.9
  • Pip: v 9.0.1

For your convenience (so that you don’t need to open new tabs and navigate through different web pages) I pasted throughout this guide all the commands required for successful installation at the time of writing.

Prerequisites

  1. Ubuntu 18.04
  2. NVIDIA GPU with at least 4GB
  3. NVIDIA drivers > 361.93
  4. Free Disk Space > 30GB (this is the minimum preferred amount so that your system behaves properly)

Docker Installation

We will be installing the simulator using a docker image as it provides an easier and faster setup process. Note that this is intended for users who don’t care about the internals of Carla and just wish to run it stress-free. If on the other hand you wish to do a full linux build please see the official docs.

  1. Setup repository + Docker engine installation

The installation of Docker is pretty straightforward and awesomely explained in the official homepage.

$ sudo apt-get update

$ sudo apt-get install \
apt-transport-https \
ca-certificates \
curl \
gnupg-agent \
software-properties-common
$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -$ sudo apt-key fingerprint 0EBFCD88$ sudo add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) \
stable"
$ sudo apt-get update$ sudo apt-get install docker-ce docker-ce-cli containerd.io// NOTE REQUIRED: the following command is used to test if your installation was successfully.$ sudo docker run hello-world

2. Configure docker to be run without “sudo”.

By default, docker can be only run with root privileges. From the docs:

The Docker daemon binds to a Unix socket instead of a TCP port. By default that Unix socket is owned by the user root and other users can only access it using sudo. The Docker daemon always runs as the root user.

If you don’t want to preface the docker command with sudo, create a Unix group called docker and add users to it. When the Docker daemon starts, it creates a Unix socket accessible by members of the docker group.

The following commands are necessary for you to be able to run docker as a non-root user.

$ sudo groupadd docker$ sudo usermod -aG docker $USER$ newgrp docker

After running the above commands you will be able to successfully run docker as a non-root user:

$ docker --versionDocker version 20.10.3, build 48d30b5

NVIDIA-Docker Installation

The next step on the list is the installation of NVIDIA-docker which allows users to build and run GPU accelerated containers such as carla. To install it please run the following commands.

$ distribution=$(. /etc/os-release;echo $ID$VERSION_ID) \
&& curl -s -L https://nvidia.github.io/nvidia-docker/gpgkey | sudo apt-key add - \
&& curl -s -L https://nvidia.github.io/nvidia-docker/$distribution/nvidia-docker.list | sudo tee /etc/apt/sources.list.d/nvidia-docker.list
$ sudo apt-get update$ sudo apt-get install -y nvidia-docker2$ sudo systemctl restart docker

After you have installed nvidia-docker2, let’s make sure that it is successfully set up by running a base CUDA container:

$ sudo docker run --rm --gpus all nvidia/cuda:11.0-base nvidia-smi      
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 460.32.03 Driver Version: 460.32.03 CUDA Version: 11.2 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 GeForce RTX 2060 Off | 00000000:08:00.0 On | N/A |
| 0% 44C P8 19W / 170W | 493MiB / 5931MiB | 6% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=============================================================================|
+-----------------------------------------------------------------------------+

If you have received a similar output then the installation was successful and you are now able to run GPU-accelarated docker containers.

Carla Installation

Now that we have installed the required requirements to successfully run a Carla docker container we are ready to install the simulator.

In this case we will install version 0.9.9 of Carla. If you want to install a different version you just need to change the version number to the one you want. Note however that a different version might cause compatibility issues with Carla-ROS-Bridge and consequently cause malfunctioning of the latter. Here you can access the list of all available Carla docker images.

  1. Pull Carla docker image
$ docker pull carlasim/carla:0.9.9# Verify that image was successfully installed$ docker imagesREPOSITORY       TAG         IMAGE ID       CREATED         SIZE
nvidia/cuda 11.0-base 2ec708416bb8 6 months ago 122MB
carlasim/carla 0.9.9 13f7e2eebaf1 10 months ago 7.94GB
hello-world latest bf756fb1ae65 13 months ago 13.3kB

Now that Carla’s image is successfully installed we need to start the Carla server so that we can later copy Carla’s python client in order to be able to run the simulator.

2. Run the simulation server

Now it is time to start a detached container running the simulation server.

$ sudo docker run -d -p 2000-2002:2000-2002 --runtime=nvidia -e NVIDIA_VISIBLE_DEVICES=0 carlasim/carla:0.9.9 /bin/bash CarlaUE4.sh

3. Copy Python client to folder

In order to run the python clients available we will have to copy the contents of the PythonAPI folder to our own directory space in order to successfully run them. To do so, lets copy its contents using “docker cp” command.

$ cd ~/Documents# Note that pedantic_cray is the name of the container. Use can use the container ID instead. To know your container ID/name run 'docker ps -a'.$ sudo docker cp pedantic_cray:/home/carla/PythonAPI ./

4. Install the required dependencies for running the Python client

Note that we will use python2 instead because of Carla-ROS-Bridge compatibility. If you don’t care about running Carla-ROS-Bridge you may install python3 instead.

$ sudo apt install python

Install “pip” and the required dependencies:

$ sudo apt install python-pip$ pip install setuptools pygame numpy

5. Run simulation client

Afterwards we are able to run the simulator. To do so:

$ cd ~/Documents/CarlaSim/PythonAPI/examples$ python manual_control.py

If everything went well you should be able to see a new Pygame window with the ongoing simulation similar to the one below:

Carla simulator client running

Note that if you get any missing package error just make sure to install it with pip.

And this was it! These were all the steps it took to have Carla successfully running in a freshly installed Ubuntu 18.04. Please remember that I wrote a follow-up guide on how to setup Carla-ROS-Bridge after you have successfully installed Carla simulator so to that you are able to leverage even more your interaction with the Carla simulator.

Hope you liked it and now it is time to explore and enjoy this amazing tool! If you have any questions feel free to comment below, I am happy to try helping you with your new Carla setup or just to know your experience with it. See you next time!

Programming enthusiast :)