The jq command is a Command Line tool which is primarily used to format JSON files, right from the command line. It is also referred to as a JSON Processor, to that account. The reason why anyone would really wish to use a command line tool to format JSON is because the jq command supposedly views the file in a prettier and human-readable format, with color highlighting and all.
In the meantime, many users seem to have faced issues while trying to run the command, formerly due to this error message that pops up on their terminal screen:
$ jq: command not found
In this article, my aim will be to help you eradicate this error message, whether you’re in Linux, or in MacOS. So sit back, relax, and spread your hands out to the keyboard!
Install jq command in Linux
The one and only reason as to why you’d be unable to run the jq command (if you’re on a Linux or MacOS machine) is due to not having the program installed in your system. Yes, you heard it right. As it is a third-party software, it does not come shipped by default with your operating system of choice. Which is why, to use it, you need to first install it.
You have 2 different ways you could install the command in your machine. The first is to use a package manager, while the second is installation from source code. In MacOS, there is a pre-compiled binary available. But if you’re using Linux, you might need to go about the hard way if you don’t wish to or are unable to use a package manager.
Starting off with Linux using a package manager, the array of package managers available is vast. Different Linux distributions use different package managers, so you’ll need to use the command that matches your OS from the below list. If your distro is not included here, it is probably based on any of the ones mentioned, and you might as well want to check out which one it is.
Here are the commands for installing jq in the most commonly used distributions:
$ sudo pacman -Syu jq
$ sudo apt-get update
$ sudo apt-get install jq
$ sudo yum makecache
$ sudo yum install jq
$ sudo dnf makecache
$ sudo dnf install jq
The first line of command (other than in Arch Linux) is required to update the repositories, otherwise you might be unable to install the program successfully in a few cases. The second line of command installs the jq package.
Install jq command in MacOS
In MacOS, you can use the popular package manager called Homebrew to download and install jq in your machine, directly without any extra effort required, if you have Homebrew installed already.
If you haven’t, paste the following command in your terminal to download the install script:
$ wget https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh
Now navigate to the directory where it got downloaded and run the following command to allow it to be executable by your user account:
$ chmod u+x install.sh
Lastly, run the install script with Bourne Again Shell:
Once the installation is over, you can now close the terminal and start it again, followed by this below command which will install jq in your MacOS machine:
$ brew install jq
If for some random reason, you are unable to install it using homebrew, you might consider using the pre-compiled binary packages, which will be discussed in the next section.
Install jq command from source
To install jq from the source code in Linux, follow the steps below.
Download the latest version of the source code of jq from their official github page. To install the current source version of jq which is v1.6, you can simply click on the below link to download it:
Once downloaded, navigate to the directory where you install the tarball, and extract it using the tar command. If you downloaded v1.6, this is the command you’d be using to extract it:
$ tar -xf jq-1.6.tar.gz
Now enter the directory that got extracted using the cd command:
$ cd jq-1.6
Inside the extracted directory, you’d first want to run the script called configure. Do so by executing:
As soon as the configuration files required to compile the package have been created, you can now run make to build the package:
Lastly, install the package system-wide by running:
$ sudo make install
You should now be able to run the jq command in your Linux machine. To check whether the command got successfully installed, you can run the below command:
$ jq -Version
If the output shows the version of the package, this means that the package has been installed and you should no longer see the error message you would’ve seen otherwise.
In MacOS, you can go through the exact same procedure as the Linux one, if you wish to install it from scratch, as long as you’re sure that all the necessary build tools are already installed in your system. But you probably won’t want to take the hassle, as there are pre-compiled binaries available for it from their official github page:
You can also directly download the latest version, which is v1.6, directly by clicking on the following link:
Alternatives to jq command
Whatever reason it might be, it is not impossible that you don’t personally like the jq command, and are looking for some alternatives, or it simply might be because of your system not being able to run this command, you’d be lucky to know that there actually are some good alternatives to the jq command. Some of best alternatives to consider are:
This pretty much concludes my fix to the error message regarding jq. If none of the solutions have worked, even after you’re sure that you’ve properly followed all the steps, your last resort could be to use one of the available alternatives.
Have a good day!