title: Run OpenGL on AWS GPU instances with CentOS
body: <p>I need to execute some off-screen rendering program on AWS EC2 GPU instance with CentOS. However, while I found that Ubuntu is very easy to setup, I cannot let CentOS work properly. </p>
  
  <p>The goal is to run some essential utility/test tool on EC2 GPU instance (without screen or X client). In the following article, I will describe how the Ubuntu can be setup and how CentOS/Amazon Linux AMI fails. </p>
  
  <h2>Ubuntu</h2>
  
  <p>On ubuntu 12.04, everything works very smoothly. The EC2 environment I used are: </p>
  
  <ul>
  <li>Instance type: Both CG1 and G2 were tested and worked properly. </li>
  <li>AMI image: Ubuntu Server 12.04.3 LTS for HVM Instances (ami-b93264d0 in US-East)</li>
  <li>Most of the other settings are default. </li>
  </ul>
  
  <p>After the instance is launched, the following commands are executed:</p>
  
  <pre><code># Install the Nvidia driver
  sudo apt-add-repository ppa:ubuntu-x-swat/x-updates
  sudo apt-get update
  sudo apt-get install nvidia-current
  # Driver installation needs reboot
  sudo reboot now
  
  # Install and configure X window with virtual screen
  sudo apt-get install xserver-xorg libglu1-mesa-dev freeglut3-dev mesa-common-dev libxmu-dev libxi-dev
  sudo nvidia-xconfig -a --use-display-device=None --virtual=1280x1024
  sudo /usr/bin/X :0 &amp;
  
  # OpenGL programs are now workable. Ex. glxinfo, glxgears
  DISPLAY=:0 glxinfo
  </code></pre>
  
  <p>The <code>glxgears</code> can also run in the background without physical screen or X client:</p>
  
  <pre><code>$ DISPLAY=:0 glxgears
  95297 frames in 5.0 seconds = 19059.236 FPS
  95559 frames in 5.0 seconds = 19111.727 FPS
  94173 frames in 5.0 seconds = 18834.510 FPS
  </code></pre>
  
  <h2>CentOS or Amazon Linux AMI</h2>
  
  <p>Both "CentOS" and "Amazon Linux AMI" are derived from Red Hat Enterprise edition. However, I cannot make any of them work.</p>
  
  <p>A few days ago, AWS <a href="http://aws.typepad.com/aws/2013/11/build-3d-streaming-applications-with-ec2s-new-g2-instance-type.html">announced their new G2 instance type</a>. In this article, the <a href="https://aws.amazon.com/marketplace/pp/B00FYCDDTE">Amazon Linux AMI with NVIDIA Drivers</a> is recommended for Linux platform. In this AMI, the Nvidia driver, X window and OpenGL libraries are all installed. However, I just get GLX error messages when trying to execute OpenGL programs. </p>
  
  <p>The EC2 instance is launched with the following setting: </p>
  
  <ul>
  <li>AMI image: Amazon Linux AMI with NVIDIA GRID GPU Driver (ami-637c220a in US-East)</li>
  <li>Instance type: G2</li>
  <li>Most of the other settings are default</li>
  </ul>
  
  <p>After booted, the steps to reproduce this issue is very simple:</p>
  
  <pre><code>sudo X :0 &amp; # Start the X window
  glxinfo
  glxgears
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>$ glxinfo
  name of display: :0
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Error: couldn't find RGB GLX visual or fbconfig
  
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  Xlib:  extension "GLX" missing on display ":0".
  
  $ glxgears
  Xlib:  extension "GLX" missing on display ":0".
  Error: couldn't get an RGB, Double-buffered visual
  </code></pre>
  
  <p>The following error is found in <code>/var/log/Xorg.0.log</code>:</p>
  
  <pre><code>[139017.484] (EE) Failed to initialize GLX extension (Compatible NVIDIA X driver not found)
  </code></pre>
  
  <p>I have googled and tried a lot of possible solution, such as:</p>
  
  <ul>
  <li>Use the clean CentOS HVM AMI and install Nvidia driver manually</li>
  <li>Tried both CG1/G2 instance types</li>
  <li>Regenerate the X window config with nvidia-xconfig</li>
  <li>Use Xvfb instead of X window</li>
  <li>Reinstall Nvidia driver after mesa libraries are installed</li>
  </ul>
  
  <p>... but none of them works. </p>
  
  <p>Does anyone have a concrete solution for this issue? Everything I mentioned should be reproducible (I tried many times). I'll appreciate if you can provide reproducible instructions to make OpenGL (GLX) works on EC2 GPU instances with CentOS/Amazon Linux AMI. </p>