MATLAB: Hardware OpenGL Failure on Ubuntu 16.04.3 – Can not initialize shared resources


First, my system info:
  • 4.13.0-26-generic #29~16.04.2-Ubuntu SMP Tue Jan 9 22:00:44 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Clean installation of Matlab R2016b (64-bit)
  • Video card: AMD/ATI Pitcairn PRO (Radeon HD 7850)
Problems with Vanilla Install
So, I start up Matlab from the command line with no arguments. I've set two environment variables, LIBGL_DEBUG=verbose and MESA_DEBUG=1, to get an idea of any bugs. The software starts with no errors. Next, I issue a simple plot command:
plot(1,1, 'b*'); grid on;
Matlab creates the plot successfully, but issues a series of errors, saying that there was a low-level graphics error. From the command line I see the following messages:
libGL: pci id for fd 570: 1002:6819, driver radeonsi
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/tls/
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/
libGL: dlopen /usr/lib/x86_64-linux-gnu/dri/ failed (/opt/local/MATLAB/R2016b/bin/glnxa64/../../sys/os/glnxa64/ version `GLIBCXX_3.4.21' not found (required by /usr/lib/x86_64-linux-gnu/dri/
There are many more, but the end result is the same: the linker cannot load the graphics drivers with the file Matlab has loaded. To fix this issue, I rename the relevant libraries that come with Matlab to force it to use the system libraries (which include version GLIBCXX_3.4.21):
cd /opt/local/MATLAB/R2016b/sys/os/glnxa64
Problems with Modified Install
With the C++ libraries renamed to force Matlab to link with the system libraries, I restart Matlab. The environment variables are still set, so any libgl or mesa errors should be reported to the terminal. Immediately after the program starts, I receive the following error in Matlab:
com.jogamp.opengl.GLException: X11GLXDrawableFactory - Could not initialize shared resources for X11GraphicsDevice[type .x11, connection :1, unitID 0, handle 0x0, owner false, ResourceToolkitLock[obj 0x7c1bc33e, isOwner false, <2af5b1a, 46860956>[count 0, qsz 0, owner <NULL>]]]
at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(
at Source)
Caused by: java.lang.NullPointerException
at jogamp.opengl.GLContextImpl.makeCurrent(
at jogamp.opengl.GLContextImpl.makeCurrent(
at jogamp.opengl.x11.glx.X11GLXDrawableFactory$SharedResourceImplementation.createSharedResource(
... 2 more
Exception in thread "Startup Class Loader" java.lang.InternalError: XXX0 profile[1]: GL3bc -> profileImpl GL4bc !!! not mapped
at com.jogamp.opengl.GLProfile.computeProfileMap(
at com.jogamp.opengl.GLProfile.initProfilesForDeviceCritical(
at com.jogamp.opengl.GLProfile.initProfilesForDevice(
at com.jogamp.opengl.GLProfile.initProfilesForDefaultDevices(
at com.jogamp.opengl.GLProfile.access$000(
at com.jogamp.opengl.GLProfile$
at Method)
at com.jogamp.opengl.GLProfile.initSingleton(
at com.mathworks.hg.peer.JavaSceneServerPeer.initializeJOGL(
at com.mathworks.hg.peer.JavaSceneServerPeer.<clinit>(
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.mathworks.mde.desk.StartupClassLoader.loadClass(
at com.mathworks.mde.desk.StartupClassLoader.access$900(
at com.mathworks.mde.desk.StartupClassLoader$
at Source)
Additionally, the following messages are displayed in the Terminal output:
libGL: pci id for fd 570: 1002:6819, driver radeonsi
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/tls/
libGL: OpenDriver: trying /usr/lib/x86_64-linux-gnu/dri/
libGL: Using DRI3 for screen 0
These messages don't indicate any troubles; it looks like the radeonsi_dri object is loaded successfully. When I try the same plot command as above, I receive the following message in Matlab:
Caught unexpected fl::except::IInternalException
The figure shows an empty image; the plot has not been created.
Other Notes
  • I've also tried renaming the library in Matlab's directory so that it uses the system version, but that doesn't change anything.
  • There are no errors (EE) in my Xorg.0.log file
  • I've successfully benchmarked OpenGL and the video card with glmark2, i.e., OpenGL runs very well with hardware accelleration on my machine, but Matlab has some issue.
  • Matlab R2016b ships with (in R2016b/sys/opengl/lib/glnxa64) but my OS version is Could this cause an issue? I tried renaming the libGL files shipped with Matlab to force it to use the system versions, but the error messages are still the same.
  • With the modified libstdc++ files, I can run Matlab in software openGL mode via the command matlab -softwareopengl. Plotting works fine, though Mesa spits out the warning/error, "User error: GL_INVALID_ENUM in glGetIntegerv(pname=GL_MAJOR_VERSION)"

Best Answer

The solution from Thad (below) works, but includes some unnecessary complexity if you're not a PsychToolbox User, so I'm summarizing the minimal set of steps below. Step #1 (below) need only be completed once, while steps 2, 3, 4 must be completed for each version of Matlab installed on your machine.
1. Install the libjogl2 library (Updated: no longer requires bleeding-edge version):
# Add libjogl2-java
sudo apt update -y
sudo apt install -y libjogl2-java
2. Rename C++ standard library files (repeated from question, above). This ensures that the graphics drivers are loaded with the same standard library as they need rather than the ones Matlab comes bundled with.
cd $matlabroot/sys/os/glnxa64
3. Change the classpath.txt and librarypath.txt files in $MATLABROOT/toolbox/local. Using my original paths, this path expands to /opt/local/MATLAB/R2017b/toolbox/local
  • In classpath.txt, find and comment out the following two lines:
And replace them with the following
  • In librarypath.txt, add a new line at the end of the file with
4. According to the original script provided above, Matlab should now run without any errors. However, to be sure that the default included libraries do not interfere, rename the following libraries in $matlabroot/bin/glnxa64:
cd $matlabroot/bin/glnxa64
sudo mv
sudo mv
sudo mv
sudo mv
Finally, in $matlabroot/toolbox/local/classpath rename the 3p_jogl file:
cd $matlabroot/toolbox/local/classpath
sudo mv
And that's it! Problem solved! Hopefully those Xenial-Proposed packages will roll out to the stable branch in the not-too-distant future.