Select Page

Introduction

There are times when you create some heavy programs in your computer with capable programming languages like C and C++, you should be no stranger to the never-dying Memory leakage issue. It is something every beginner has to get on a tough time with. But luckily, there are many tools available which will help you ease the process of doing so.

In the midst of all the mess, there came the program called Valgrind, in July 2002, and it instantaneously became a popular go-to tool for C/C++ programmers, for the level of memory-allocation control and leak detection the program offered. Since then, the program has grown to become even more capable, but some users are dreaded by the following error message when the try to run the command:

$ valgrind: command not found

Or maybe even something like this (in the ZSH shell):

$ zsh: command not found: valgrind

While this is relevant to many users, there are also people who don’t see this exact error message, but something similar when they try to run the command on their programs. Which is why in this article, I’ll try my best to help you (Yes, you!) eradicate this error message and get the program up and running!


Why is valgrind not found?

The valgrind command is not found because your system cannot trace for any program or command named valgrind, which essentially means that it is not installed. Yes, the only reason as to why you’d be unable to run the command is not having it installed in the first place. And even if you do, there’s a high chance that you’re running the command wrong, which causes the error to appear out of nowhere. 


Install valgrind in Linux

To install valgrind on your Linux machine, you need to use the package manager for your specific Linux distribution. This is because different Linux distributions use different package managers, and many have their very own repositories. Just in case you asked, there are currently more than 600 active Linux distributions. BUt the good news is that, most of these distributions are forks of a few common ones, and knowing their commands alone should help you do the job as the commands across forked systems are exactly the same.

Here is a curated list of the commands you’d use on the most commonly used Linux distributions to install valgrind on your machine:

Arch Linux

$ sudo pacman -Syu
$ sudo pacman -S valgrind

Ubuntu

$ sudo apt-get update
$ sudo apt-get install valgrind

Debian

$ sudo apt-get update
$ sudo apt-get install valgrind

Fedora

$ sudo yum makecache
$ sudo yum install valgrind

RedHat

$ sudo dnf makecache
$ sudo dnf install valgrind

CentOS

$ sudo dnf makecache
$ sudo dnf install valgrind

The first line of command is quite crucial as it is required to update the system repositories. If you’re wondering why you need to do that, your packages will not be installed sometimes if the rest of the packages in your system have reached the date when it is marked outdated and needs to be checked for new updates. Moving on, the second line of command will install the actual valgrind package on your system. 

If your distribution is not listed here, you should first check out which package manager your distribution uses. If it seems similar to the ones mentioned here, then you can be rest assured that the command will work as it is basically based on these. If your system’s package manager does not match (which is not impossible), then you need to see if your distribution’s repository has the valgrind package, and where you can install it.


Install valgrind from source code

If you’re unable to use any package managers to install valgrind on your machine, don’t be disheartened just yet! Because there’s another way for you to install it, as long as you’re determined enough as this is quite a cumbersome process. You can install the package from its source code, and for that you’ll need build tools installed in your machine for you to be able to compile it. These tools include:

  • make
  • autoconf
  • gcc

As soon as you’ve ensured that you have these packages installed in your machine, you’re good to go!

The first thing you’d have to do is download the latest source code archive from the following website:

https://valgrind.org/downloads/current.html

Or you can directly click on the following link to download the latest version, as of now, which is v3.20.0:

https://sourceware.org/pub/valgrind/valgrind-3.20.0.tar.bz2

Now navigate to the directory where the archive got downloaded, and extract it using the tar command in your terminal:

$ tar -xf valgrind-3.20.0.tar.bz2

Once done, enter the directory that got extracted, and run the autogen.sh found inside this directory:

$ ./autogen.sh

Afterwards, run the configure script to create the configuration files required to build the package:

$ ./configure

Once the files have been made, you can now run the make command to compile the package:

$ make

Lastly, install the package system-wide so that you can run the valgrind command from any directory you wish:

$ sudo make install

Now run the following command to check whether the package has properly been installed:

$ valgrind --version

Execute programs using valgrind the right way

Besides installing the valgrind command, there’s one other thing you need to ensure. That is, when you execute your own C/C++ project with the valgrind command, you need to make sure that you execute the command like below:

$ valgrind --tool=memcheck ./yourfile

Notice the “./” before the name of your file. Without using this, you’d not be able to execute the program, and you’d end up getting an even weirder error like this:

$ valgrind: yourfile: command not found

Also, you need to be in the directory where your code file is located. If you’re trying to run it from any other directory, it is not going to work. If you wish to run your code file from any directory and without having to use “./”, then you need to copy that file to the /usr/bin/ folder of your system, but it’s best we don’t go that far.

I’m hopeful that after having followed the above solutions properly, you’ve been able to run the valgrind command with the error message no longer disturbing your workflow!