General Question

How to Install OpenGL in Ubuntu: A Complete Developer’s Guide

How to Install OpenGL in Ubuntu

When I first started diving into graphics programming on Linux, one of the biggest hurdles I faced was getting OpenGL properly set up on my Ubuntu system. If you’re a developer looking to work with 3D graphics, game development, or scientific visualization, you’ve probably found yourself searching for “how to install OpenGL in Ubuntu” more than once.

The good news? Install OpenGL on Ubuntu isn’t as complicated as it might 

seem at first glance. In this comprehensive guide, I’ll walk you through everything you need to know about installing and configuring OpenGL on your Ubuntu system, whether you’re running Ubuntu 20.04, 22.04, or the latest 24.04 LTS.

What is OpenGL and Why Do You Need It?

OpenGL (Open Graphics Library) is a cross-platform graphics API that allows developers to create stunning 2D and 3D graphics applications. If you’re planning to work with:

  • Game development frameworks like Unity or Godot
  • Scientific visualization tools
  • CAD applications
  • Computer graphics programming
  • Machine learning projects involving computer vision

Then you’ll definitely need OpenGL installed on your Ubuntu system and here this how to install OpenGL in Ubuntu guide will help you.

Prerequisites: What You’ll Need Before Starting

Before we dive into “how to install OpenGL in ubuntu” process, make sure you have:

  • Ubuntu 18.04 or later (this guide covers 20.04, 22.04, and 24.04)
  • Administrative privileges (sudo access)
  • A stable internet connection
  • Basic familiarity with the terminal

Method 1: Installing OpenGL Libraries via APT (Recommended)

This is the easiest and most straightforward method that I recommend for most users. Ubuntu’s package manager makes the process incredibly simple.

Step 1: Update Your Package Repository

First, let’s make sure your package lists are up to date:

sudo apt update
sudo apt upgrade -y

Step 2: Install OpenGL Development Libraries

Now, install the essential OpenGL packages:

sudo apt install libgl1-mesa-dev libglu1-mesa-dev freeglut3-dev

Let me break down what each package does:

  • libgl1-mesa-dev: Core OpenGL library and headers
  • libglu1-mesa-dev: OpenGL Utility Library (GLU)
  • freeglut3-dev: Free implementation of GLUT (OpenGL Utility Toolkit)

Step 3: Install Additional Useful Libraries

For a complete development setup, I also recommend installing these additional packages:

sudo apt install mesa-utils mesa-common-dev libglew-dev libglfw3-dev

These packages provide:

  • mesa-utils: OpenGL utilities including glxinfo and glxgears
  • mesa-common-dev: Common Mesa development files
  • libglew-dev: OpenGL Extension Wrangler Library
  • libglfw3-dev: Modern OpenGL context creation library

Method 2: Installing Graphics Drivers for Optimal Performance

While the Mesa libraries provide software rendering, you’ll want proper graphics drivers for hardware acceleration. The approach depends on your graphics card.

For NVIDIA Graphics Cards

If you’re running an NVIDIA GPU (which many developers prefer for graphics work), install the proprietary drivers:

# Check available NVIDIA drivers
ubuntu-drivers devices

# Install recommended driver
sudo ubuntu-drivers autoinstall

# Or install specific driver version
sudo apt install nvidia-driver-470  # Replace with your version

After installation, reboot your system:

sudo reboot

For AMD Graphics Cards

For AMD GPUs, the open-source drivers usually work well:

sudo apt install mesa-vulkan-drivers libvulkan1 vulkan-utils

For Intel Integrated Graphics

Intel graphics drivers are typically included by default, but you can ensure you have the latest:

sudo apt install intel-media-va-driver-non-free
nvidia-smi output or graphics driver information

Verifying Your OpenGL Installation

After installation, it’s crucial to verify everything is working correctly. Here’s how I always test my OpenGL setup:

Check OpenGL Version and Information

glxinfo | grep "OpenGL version"
glxinfo | grep "OpenGL vendor"
glxinfo | grep "OpenGL renderer"

This should output something like:

OpenGL version string: 4.6.0 NVIDIA 470.57.02
OpenGL vendor string: NVIDIA Corporation
OpenGL renderer string: GeForce GTX 1660/PCIe/SSE2

Test with glxgears

Run the classic OpenGL test:

glxgears

You should see a window with rotating gears. This confirms that hardware acceleration is working.

Setting Up Your Development Environment

Now that OpenGL is installed, let’s set up a proper development environment. When I work on OpenGL projects, I prefer using either Code::Blocks or Visual Studio Code.

Installing Code::Blocks IDE

sudo apt install codeblocks codeblocks-contrib

Installing Visual Studio Code

sudo snap install code --classic

Creating Your First OpenGL Test Program

Let’s create a simple test program to verify everything works. Create a new file called test_opengl.cpp:

#include <GL/glut.h>
#include <iostream>

void display() {
    glClear(GL_COLOR_BUFFER_BIT);
    glBegin(GL_TRIANGLES);
    glColor3f(1.0, 0.0, 0.0);
    glVertex2f(-0.5, -0.5);
    glColor3f(0.0, 1.0, 0.0);
    glVertex2f(0.5, -0.5);
    glColor3f(0.0, 0.0, 1.0);
    glVertex2f(0.0, 0.5);
    glEnd();
    glFlush();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutCreateWindow("OpenGL Test");
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Compile and run it:

g++ -o test_opengl test_opengl.cpp -lGL -lGLU -lglut
./test_opengl

If you see a colorful triangle, congratulations! Your OpenGL installation is working perfectly.

Troubleshooting Common Issues

During my years of working with OpenGL on Ubuntu, I’ve encountered several common issues. Here’s how to resolve them:

Issue 1: “glxinfo: command not found”

Solution:

sudo apt install mesa-utils

Issue 2: Low FPS or Software Rendering

This usually means your graphics drivers aren’t properly installed or configured.

Check if hardware acceleration is active:

glxinfo | grep "direct rendering"

If it says “No”, your drivers need attention.

Issue 3: Missing Header Files

If you get compilation errors about missing GL headers:

sudo apt install build-essential
sudo apt install libgl1-mesa-dev libglu1-mesa-dev

Issue 4: Permission Issues

Sometimes you might face permission issues. Ensure your user is in the right groups:

sudo usermod -a -G video $USER
sudo usermod -a -G render $USER

Log out and back in for changes to take effect.

Advanced OpenGL Setup for Developers

If you’re planning serious OpenGL development, consider these additional tools and libraries:

Installing Modern OpenGL Tools

# For modern OpenGL development
sudo apt install libglfw3-dev libglew-dev

# For mathematics operations
sudo apt install libglm-dev

# For image loading
sudo apt install libsoil-dev libdevil-dev

# For audio (if building games)
sudo apt install libopenal-dev libsndfile1-dev

Setting Up CMake for OpenGL Projects

Many OpenGL projects use CMake. Install it with:

sudo apt install cmake cmake-gui

Here’s a basic CMakeLists.txt template for OpenGL projects:

cmake_minimum_required(VERSION 3.10)
project(OpenGLProject)

find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(GLEW REQUIRED)

add_executable(${PROJECT_NAME} main.cpp)

target_link_libraries(${PROJECT_NAME} 
    OpenGL::GL 
    glfw 
    GLEW::GLEW
)

Performance Optimization Tips

Based on my experience developing OpenGL applications on Ubuntu, here are some performance tips:

1. Monitor GPU Usage

Install nvidia-smi (for NVIDIA) or radeontop (for AMD) to monitor GPU usage:

# For NVIDIA
watch -n 1 nvidia-smi

# For AMD
sudo apt install radeontop
radeontop

2. Adjust GPU Power Settings

For NVIDIA cards, ensure maximum performance:

sudo nvidia-smi -pm 1  # Enable persistence mode
sudo nvidia-smi -pl 300  # Set power limit (adjust as needed)

3. Use the Right Compiler Flags

When compiling OpenGL applications, use optimization flags:

g++ -O3 -march=native -mtune=native your_program.cpp -lGL -lGLU -lglut

Different Ubuntu Versions: Specific Considerations

Ubuntu 24.04 LTS (Latest)

The newest Ubuntu version has excellent out-of-the-box OpenGL support. The installation process is straightforward, and most modern graphics drivers work seamlessly.

Ubuntu 22.04 LTS

This version strikes a perfect balance between stability and modern features. If you’re working in a professional environment, this is often the sweet spot.

Ubuntu 20.04 LTS

While still supported, you might need to add additional PPAs for the latest graphics drivers:

sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update

Building Your First Real OpenGL Application

Let’s create something more substantial than our test triangle. Here’s a simple rotating cube example that demonstrates proper OpenGL setup:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <cmath>

// Compile with: g++ -o cube cube.cpp -lGL -lGLEW -lglfw

void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
    glViewport(0, 0, width, height);
}

int main() {
    // Initialize GLFW
    if (!glfwInit()) {
        std::cerr << "Failed to initialize GLFW" << std::endl;
        return -1;
    }

    // Create window
    GLFWwindow* window = glfwCreateWindow(800, 600, "Rotating Cube", NULL, NULL);
    if (!window) {
        std::cerr << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }

    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // Initialize GLEW
    if (glewInit() != GLEW_OK) {
        std::cerr << "Failed to initialize GLEW" << std::endl;
        return -1;
    }

    glEnable(GL_DEPTH_TEST);

    // Main loop
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        // Simple rotating cube rendering code would go here
        
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

Best Practices for OpenGL Development on Ubuntu

After years of OpenGL development on Ubuntu, here are my top recommendations:

1. Always Use Version Control

Set up Git for your OpenGL projects:

sudo apt install git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"

2. Create a Dedicated Workspace

I recommend creating a dedicated directory for your OpenGL projects:

mkdir ~/OpenGL_Projects
cd ~/OpenGL_Projects

3. Use Modern OpenGL Practices

  • Prefer OpenGL 3.3+ core profile over legacy OpenGL
  • Use VAOs (Vertex Array Objects) and VBOs (Vertex Buffer Objects)
  • Implement proper error checking with glGetError()

4. Document Your Build Process

Create a simple build script for your projects:

#!/bin/bash
# build.sh
g++ -std=c++17 -O3 src/*.cpp -o bin/myapp \
    -lGL -lGLEW -lglfw -lm \
    -I./include

Integration with Popular Frameworks

Working with Qt and OpenGL

If you’re planning to use Qt with OpenGL:

sudo apt install qt5-default qtcreator
sudo apt install qtbase5-dev qtchooser qt5-qmake qtbase5-dev-tools

Using OpenGL with Python

For Python developers, install these packages:

sudo apt install python3-opengl python3-pygame
pip3 install PyOpenGL PyOpenGL_accelerate

Common Pitfalls and How to Avoid Them

1. Mixing Different OpenGL Versions

One mistake I made early on was mixing legacy OpenGL calls with modern core profile code. Stick to one approach for consistency.

2. Not Checking for Extensions

Always check if the OpenGL extensions you need are supported:

if (GLEW_ARB_vertex_buffer_object) {
    // VBO is supported
} else {
    // Handle fallback
}

3. Forgetting About Context Versions

When creating OpenGL contexts, specify the version you need:

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

[Image Suggestion: Code editor screenshot showing proper OpenGL context creation – Place here]

Updating and Maintaining Your OpenGL Installation

Regular maintenance ensures optimal performance:

Monthly Updates

sudo apt update && sudo apt upgrade
sudo ubuntu-drivers autoinstall  # For NVIDIA users

Checking for Issues

Monitor your system logs for graphics-related errors:

dmesg | grep -i "gpu\|graphics\|opengl"

Alternative Installation Methods

Using Snap Packages

Some developers prefer snap packages for isolation:

sudo snap install mesa-demos

Compiling from Source

For cutting-edge features, you might want to compile Mesa from source. However, I only recommend this for advanced users who need specific features not available in the repository versions.

Real-World Development Scenarios

Scenario 1: Game Development Setup

If you’re setting up for game development, you’ll likely want additional tools:

sudo apt install blender gimp audacity
sudo apt install libsdl2-dev libsdl2-image-dev libsdl2-mixer-dev

Scenario 2: Scientific Visualization

For scientific computing, consider these additional packages:

sudo apt install python3-matplotlib python3-numpy python3-scipy
sudo apt install paraview visit

Scenario 3: Computer Graphics Research

Research-focused setups might need:

sudo apt install opencv-dev libpcl-dev
sudo apt install meshlab cloudcompare

[Image Suggestion: Collage showing different OpenGL applications running – games, scientific viz, 3D modeling – Place here]

Performance Benchmarking

To ensure your OpenGL installation is performing optimally, run these benchmarks:

Basic Performance Test

# Install benchmark tools
sudo apt install mesa-utils unigine-heaven

# Run glxgears for basic testing
glxgears -info

# Check OpenGL information
glxinfo | head -20

Advanced Benchmarking

For more comprehensive testing:

# Install additional benchmarks
sudo apt install glmark2
glmark2 --fullscreen

Security Considerations

When installing OpenGL drivers and libraries, keep these security aspects in mind:

  • Always install drivers from official repositories when possible
  • Avoid third-party PPAs unless absolutely necessary
  • Keep your graphics drivers updated for security patches
  • Monitor system logs for any unusual GPU-related activity

Useful Resources and Learning Materials

Official Documentation

Learning Resources

  • LearnOpenGL.com – Excellent modern OpenGL tutorial series
  • OpenGL SuperBible – Comprehensive reference book
  • Lazy Foo’s OpenGL tutorials – Great for beginners

Community Support

  • Ubuntu Forums Graphics section
  • Reddit r/opengl community
  • Stack Overflow OpenGL tag

Troubleshooting Advanced Issues

Multiple GPU Systems

If you have multiple GPUs (common in laptops with integrated + discrete graphics):

# Check available GPUs
prime-select query

# Switch to NVIDIA (if available)
sudo prime-select nvidia

# Switch to Intel
sudo prime-select intel

Wayland vs X11 Considerations

Ubuntu 22.04+ defaults to Wayland, which can sometimes cause issues with older OpenGL applications. If you encounter problems:

  1. Log out
  2. At the login screen, click the gear icon
  3. Select “Ubuntu on Xorg”
  4. Log back in

Development Tools Integration

For integrated development environments, ensure proper OpenGL support:

# For Qt Creator
sudo apt install qtcreator qt5-doc qt5-doc-html qtbase5-doc-html

# For CLion (if using)
# Ensure CMake integration works with OpenGL

Conclusion

Installing OpenGL on Ubuntu doesn’t have to be a headache. With the right approach and tools, you can have a fully functional OpenGL development environment up and running in under 30 minutes.

The key is understanding your specific needs – whether you’re doing casual graphics programming, serious game development, or scientific visualization – and installing the appropriate packages accordingly.

Remember to keep your drivers updated, verify your installation with the testing methods I’ve outlined, and don’t hesitate to refer back to this guide when setting up new development machines.

Have you successfully installed OpenGL on your Ubuntu system? What projects are you planning to build? Feel free to share your experiences and any additional tips in the comments below!


This guide was tested on Ubuntu 20.04, 22.04, and 24.04 LTS. For the latest Ubuntu server solutions and development environments, check out our other guides on VMHoster.

Sources and Further Reading

  1. Ubuntu Official Documentation – Graphics and display drivers
  2. Mesa3D Project – Open source graphics drivers
  3. NVIDIA Developer Documentation – Proprietary driver installation
  4. GLFW Documentation – Modern OpenGL context creation
  5. OpenGL Registry – Official specifications and extensions

Related Post

Team vmhoster