Objective

Install the latest stable version of Mesa on Debian Stretch.

Distributions

Debian 9 Stretch

Requirements

A working install of Debian Stretch with root access.

Difficulty

Medium

Conventions

  • # - requires given command to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - given command to be executed as a regular non-privileged user

Introduction

Mesa is a big deal if you're running open source graphics drivers. It can be the difference between a smooth experience and an awful one. Mesa is under active development, and it sees constant noticeable performance improvements. That means it's really worthwhile to stay on top of the latest releases. Debian usually doesn't, though, so Debian users have to take matters into their own hands. If you're running Debian Stretch and want the latest stable version of Mesa, building it yourself is your only option. Don't worry, it's actually not that bad.

Install The Dependencies

Before you can build Mesa, there are dependencies that you need to install. Mesa is a large and complex program, so there are more than a few, but they're all available through apt. Start off by grabbing build-essential and having apt pull Mesa's hard build dependencies.
# apt install build-essential
# apt build-dep mesa libdrm2
Once you have those, you need LLVM and Clang. Install version 3.9 and the dependencies related to them.
# apt install llvm-3.9 llvm-3.9-dev clang-3.9 clang-3.9-dev libxvmc-dev libxcb-xvmc0-dev libvdpau-dev libomxil-bellagio-dev
LLVM-3.9 isn't the default version in Stretch even though the other available versions are ridiculously out-of-date. To avoid any weirdness, create a symlink between LLVM-3.9's config and the default LLVM config.
# ln -s /usr/bin/llvm-config-3.9 /usr/bin/llvm-config

Install Libdrm from Sid

Because Debian is so awesomely out-of-date, you can't build Mesa without first installing the Libdrm packages available from Sid. Don't worry. This uses apt-pinning to ensure that only the Libdrm packages are installed from Sid, so your system won't become unstable. Start off by adding the following lines to /etc/apt/sources.list
deb http://ftp.us.debian.org/debian/ sid main
deb-src http://ftp.us.debian.org/debian sid main
Next, you need to create a new file at /etc/apt/preferences. Make it look like the example below.
Package: *
Pin: release a=stable
Pin-Priority: 1000

Package: *
Pin: release a=unstable
Pin-Priority: 2

Package: libdrm2
Pin: release a=unstable
Pin-Priority: 1001

Package: libdrm-*
Pin: release a=unstable
Pin-Priority: 1001
Update apt and install the updated packages.
# apt update
# apt install libdrm2 libdrm-dev

Clone Mesa From Git

Now, you can clone the Mesa source code from Git. Head into the directory where you want to build it and clone.
$ cd ~/Downloads
$ git clone -b 17.1 git://anongit.freedesktop.org/mesa/mesa
Right now, Mesa 17.1 is the latest stable version. If that changes, clone that branch. If you want to run the absolute latest code, leave out -b flag, but that may be unstable. After it finishes cloning, cd into the mesa directory.
$ cd mesa

Build And Install Mesa

You can configure and compile Mesa now. The autogen example below is designed to be as robust as possible and to work in nearly all situations. If you want to tailor it to your system, look up all of the options available, or cut back the things you know you don't need.
./autogen.sh --prefix=/usr --enable-texture-float --libdir=/usr/lib/x86_64-linux-gnu/ --with-gallium-drivers=i915,r300,r600,radeonsi,nouveau,swrast --with-egl-platforms=drm,x11 --enable-glx-tls --enable-shared-glapi --enable-glx --enable-driglx-direct --enable-gles1 --enable-gles2 --enable-gbm --enable-openmax --enable-xa --enable-osmesa --with-radeonsi-llvm-compiler --enable-sysfs --enable-vdpau --enable-xvmc --enable-openmax --enable-nine
Once the configuration finishes, you can build and install Mesa. Use the -j flag followed by the number of cores on the computer plus 1 to speed up compiling.
$ make -j5
# make install
The compile will take some time. When it's done and installed, restart the X server or your computer. When you reboot, you will be running your new version of Mesa.

Closing Thoughts

This method really isn't ideal. You are responsible for maintaining both Mesa and Libdrm on your computer. On the other hand, you can make sure that your open source graphics drivers are always up-to-date, maximizing your computer's performance in graphically intensive tasks like games.