Baeldung Pro – Linux – NPI EA (cat = Baeldung on Linux)
announcement - icon

Learn through the super-clean Baeldung Pro experience:

>> Membership and Baeldung Pro.

No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.

Partner – Orkes – NPI EA (tag=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

Valgrind is a powerful instrumentation framework that enables users to analyze programs, especially those written in low-level languages. It helps detect memory leaks, memory corruption, threading issues, and performance bottlenecks. Many Linux developers and system administrators choose Valgrind for debugging and profiling.

In this tutorial, we’ll explore different ways to properly install Valgrind on a Linux system. For demonstration purposes, we’ll use Ubuntu 24.04.

2. Features of Valgrind

Before diving into the installation processes, let’s highlight some key features of Valgrind:

  • automatically detects memory management and threading errors
  • supports numerous platforms, including PPC32/Linux and AMD64/Linux
  • offers dynamic binary instrumentation for analyzing compiled programs
  • provides detailed reports on memory leaks and memory access errors
  • improves performance by helping optimize cache usage and function calls
  • enables debugging and profiling of multi-threaded applications

Overall, Valgrind offers Linux users seven tools, including a memory error detector, thread error detectors, profilers for cache and function calls, and heap memory profilers, so users can ensure software quality without going through hours of bug-hunting.

3. Installing Valgrind on Linux

So, let’s cover different methods for installing the Valgrind framework on Ubuntu. If we want a quick and easy installation, we might prefer the official repository or Snap methods. On the other hand, advanced users may require the latest version with custom options, thereby choosing the source code method.

3.1. Official Repository

To begin with, we can install the Valgrind stable version directly from the official repository of Ubuntu by running a fairly simple apt install command:

$ sudo apt install valgrind -y
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Suggested packages:
  valgrind-dbg valgrind-mpi kcachegrind alleyoop valkyrie
The following NEW packages will be installed:
  valgrind
...

Of course, this method targets Linux systems that use APT as their native package management solution.

After installing Valgrind, we can check its version by running the valgrind –version command:

$ valgrind --version
valgrind-3.18.1

At this point, Valgrind is installed, so we can continue with its use.

3.2. Snap

Alternatively, we can install Valgrind on most Linux systems by executing a snap command:

$ sudo snap install valgrind --classic
valgrind 3.24.0 from Roger Light (ralight) installed

However, before running the above Snap command, we should make sure Snap is installed on the system.

After installing Valgrind, we again verify its version:

$ valgrind --version
valgrind-3.24.0

Here, we confirm that we successfully installed a more recent Valgrind version via Snap.

3.3. Source Code

If we wish to properly install the latest version of Valgrind on Linux, we can opt for manual installation via source code. While this method may not be as straightforward as others, it’s the one that guarantees we can access the newest features.

First, we should manually install the required dependencies for Valgrind:

$ sudo apt install build-essential
$ sudo apt install manpages-dev

Again, the way to install these packages and their names may depend on the particular Linux flavor as well.

Afterward, we download the Valgrind tarball file using the wget command:

$ wget https://sourceware.org/pub/valgrind/valgrind-3.24.0.tar.bz2
--2025-02-15 08:13:03--  https://sourceware.org/pub/valgrind/valgrind-3.24.0.tar.bz2
...
valgrind-3.24.0.tar 100%[===================>]  15.86M   138KB/s    in 2m 12s  

2025-02-15 08:15:18 (123 KB/s) - 'valgrind-3.24.0.tar.bz2' saved [16632387/16632387]

Next, we extract the contents of the Valgrind tarball file:

$ tar -xvf valgrind-3.24.0.tar.bz2
valgrind-3.24.0/
valgrind-3.24.0/docs/
valgrind-3.24.0/docs/xml/
...

Now, let’s navigate to the extracted Valgrind directory using the cd command:

$ cd valgrind-3.24.0/

At this point, we run the ./configure command to set up the process and ensure the system is ready to compile and install Valgrind properly:

$ ./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
...

Once ready, we compile the Valgrind source code:

$ make -j$(nproc)
echo "# This is a generated file, composed of the following suppression rules:" > default.supp
echo "# " ./xfree-3.supp ./xfree-4.supp glibc-2.X-drd.supp glibc-2.X-helgrind.supp glibc-2.X.supp  >> default.supp
...

Finally, we install Valgrind by running the make install command:

$ sudo make install

Thus, we successfully installed the latest Valgrind version on a Linux system.

4. Uninstalling Valgrind From Linux

Since we discussed three methods for installing Valgrind on Linux, let’s consider the steps to uninstall the framework with each.

4.1. Official Repository

If we installed Valgrind using the official repository method, we can remove it by executing the appropriate remove command:

$ sudo apt remove valgrind

The exact command depends on the package manager.

4.2. Snap

Naturally, the respective remove subcommand command for the Snap package as well:

$ sudo snap remove valgrind

Now, let’s see how to handle source code installations.

4.3. Source Code

However, if we installed Valgrind using source code, we should remove it by running the make uninstall command in the Valgrind directory.

If the Valgrind directory path isn’t known, we can use the which command to find it out:

$ which valgrind
/usr/local/bin/valgrind

Now, we head to the Valgrind directory using the cd command and uninstall Valgrind:

$ sudo make uninstall
Making uninstall in include
make[1]: Entering directory '/home/linuxfellas/valgrind-3.24.0/include'
...

Thus, we removed Valgrind from Ubuntu.

However, if we don’t want to use the make uninstall command for any reason, we should alternatively be able to run several commands one by one to manually remove Valgrind:

$ sudo rm -rf /usr/local/bin/valgrind
$ sudo rm -rf /usr/local/lib/valgrind
$ sudo rm -rf /usr/local/share/doc/valgrind
$ sudo rm -rf /usr/local/include/valgrind

Finally, we run find to locate and remove any files associated with Valgrind from the system:

$ sudo find /usr/local -name '*valgrind*' -exec rm -rf {} +

Thus, we saw how to install and uninstall Valgrind on Ubuntu using various methods.

5. Conclusion

In this article, we briefly discussed a powerful must-have instrumentation framework, Valgrind, and ways to install and uninstall it on Linux.

Valgrind enables Linux users to detect memory leaks and profile applications. Overall, it aids in maintaining the quality of applications.

We explored different methods for installing Valgrind properly on a Linux system. If we wish to install its stable version, we should opt for the official repository or Snap method. Alternatively, experienced users can use the manual installation via source code as the best method for installing the latest version of Valgrind. Lastly, we covered the uninstallation commands for each method.