291

I need to know the most efficient way of installing JQ on Mac (El Capitan). The code is downloaded to my Mac but I would like to know how I can install and operate it via the command line.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Ross Brannigan
  • 3,051
  • 2
  • 10
  • 3
  • 8
    Did you try with ``brew`` ? – Zulu Jun 06 '16 at 23:21
  • 2
    When posting questions here, please explain what you already have tried, and the problem you got stuck with. Take a look at this: http://stackoverflow.com/help/how-to-ask – crabbly Jun 07 '16 at 02:06
  • To avoid the downvotes, it helps if you explain what was problematic (show the errors you got) – smci Dec 11 '17 at 03:30
  • "How do I install [some piece of software]?" is not a question about writing code, particularly when the same answers that work for installing a development tool would also work for installing completely unrelated tooling. – Charles Duffy Jun 01 '23 at 14:11

10 Answers10

353

You can install any application/packages with brew on mac. If you want to know the exact command just search your package on https://brewinstall.org and you will get the set of commands needed to install that package.

First open terminal and install brew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null

Now Install jq

brew install jq
Ajeet Khan
  • 8,582
  • 8
  • 42
  • 65
  • I use MacPorts, so I'm trying to install jq without using homebrew. Do you know of any other methods? – Miguel Valencia Feb 13 '19 at 22:29
  • You can build from source. Download from https://stedolan.github.io/jq/download/ then run: ./configure; make; sudo make install – Leo Dagum Oct 02 '19 at 19:48
  • 5
    I disagree that you can install "any aplication/packages with brew". There are many applications you can't install with brew. – Tobias Feil Jul 19 '21 at 17:38
175

On a Mac, the "most efficient" way to install jq would probably be using homebrew, e.g.

brew install jq

If you want the development version, you could try:

brew install --HEAD jq

but this has various pre-requisites.

Detailed instructions are on the "Installation" page of the jq wiki: https://github.com/jqlang/jq/wiki/Installation

The same page also includes details regarding installation from source, and has notes on installing with MacPorts ("ports"), asdf (), and 0install ("zero-install").


Footnote: one can also use brew to install the Go implementation of jq:

brew install gojq
peak
  • 105,803
  • 17
  • 152
  • 177
  • I think brew is the best solution ... pip didn't work for me ... got the error – Rafaf Tahsin Jan 16 '18 at 10:03
  • `error: [Errno 2] No such file or directory ---------------------------------------- Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/private/var/folders/bk/qxg2q6ss39g1ft4470j34tzh0000gn/T/pip-build-AEc_pb/jq/setup.py';f=getattr(tokenize, 'open', open)(__file__);` – Rafaf Tahsin Jan 16 '18 at 10:04
  • `code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /var/folders/bk/qxg2q6ss39g1ft4470j34tzh0000gn/T/pip-utaZJq-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/var/folders/bk/qxg2q6ss39g1ft4470j34tzh0000gn/T/pip-build-AEc_pb/jq/` – Rafaf Tahsin Jan 16 '18 at 10:04
  • Regarding gojq, can you elaborate on why someone might want to download the go implementation of jq? Is it faster? – Alexander Terp Jun 22 '23 at 01:37
  • Unbounded precision integer **arithmetic** would be one. As of right now (June 21 2023), the last official release of jq was version 1.6 in 2018, and the last major update was made some time ago. Although development has since resumed, the current "HEAD" version of jq still has quite a few bugs, and I think it's fair to say that gojq has addressed most of these. OTOH there are solid reasons one might wish to use the C implementation. See https://github.com/jqlang/jq/wiki/Regarding-gojq – peak Jun 22 '23 at 03:03
68

The simplest way to install jq and test that it works is through brew and then using the simplest filter that merely formats the JSON

Install

brew is the easiest way to manage packages on a mac:

brew install jq

Need brew? Run the following command:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Failing that: instructions to install and use are on https://brew.sh/

Test

The . filter takes its input and produces it unchanged as output. This is the identity operator. (quote the docs)

echo '{ "name":"John", "age":31, "city":"New York" }' | jq .

The result should appear like so in your terminal:

{
  "name": "John",
  "age": 31,
  "city": "New York"
}
Stefan Collier
  • 4,314
  • 2
  • 23
  • 33
15

Check if homebrew is installed

brew install jq
  • When I try this, I'm getting this error --> `Warning: No available formula with the name "jq".` and some other errors. – Pavindu Jun 16 '22 at 05:36
6

To make sure homebrew is installed and install jq

git -C "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-cask" fetch --unshallow 
brew update
brew install jq
stevec
  • 41,291
  • 27
  • 223
  • 311
4

install jq without brew: sudo apt install jq

Boshra Jaber
  • 566
  • 1
  • 6
  • 16
3

Nothing agaist brew (I use both) but if you want to you can use macports

sudo port install jq

FWIW, jc's an interesting utility that plays very well with jq (its purpose is to json-ize command outputs). And, yes, brew install jc or sudo port install jc both work.

JL Peyret
  • 10,917
  • 2
  • 54
  • 73
2

For most it is a breeze, however like you I had a difficult time installing jq

The best resources I found are: https://stedolan.github.io/jq/download/ and http://macappstore.org/jq/

However neither worked for me. I run python 2 & 3, and use brew in addition to pip, as well as Jupyter. I was only successful after brew uninstall jq then updating brew and rebooting my system

What worked for me was removing all previous installs then pip install jq

Antoine
  • 33
  • 6
  • `pip install jq` doesn't install jq itself; it only installs a Python wrapper for jq. – Charles Duffy Jun 01 '23 at 14:12
  • Nothing worked for me, Mac complains that I need developer tools to install jq. I want to eventually use this for a build script so jq is needed to be installed from bash script on a Ubuntu docker image. Why can't someone just have a bash script function that works. awk, grep are just not working, as I get file too long. – JPM Sep 01 '23 at 16:19
2

For CentOS, RHEL, Amazon Linux: sudo yum install jq

2

If you are looking for a good alternative to homebrew (whether in a Mac or Linux environment), one such is 0install ("Zero Install"), which indeed has some advantages. For example, it's trivially easy to have multiple versions of jq installed together.

If you already have 0install installed, to add jq it would be sufficient to type:

0install add jq https://apps.0install.net/utils/jq.xml

This installs a tiny script in ~/bin/jq, which you can use as though it were jq itself.

To install a particular version of jq, say jq 1.5, you just have to specify the version number:

0install add --version=1.5 jq1.5 https://apps.0install.net/utils/jq.xml

Further details about the versions of jq that are available using this "feed" for 0install can be obtained by viewing the above-mentioned URL in a browser.

Installing 0install

One option for installing 0install itself would of course be:

 brew install 0install

Currently, a better option is to run:

curl -O https://get.0install.net/0install.sh && chmod +x 0install.sh
./0install.sh

and then follow the directions.

See 0install.net for further details.

peak
  • 105,803
  • 17
  • 152
  • 177
  • 2
    Unless there is something specifically bad about zeroinstall gotta wonder the motivation for the downvote, after 20 people each had to re-explain how `brew` does the trick, providing questionable value after the first? – JL Peyret May 28 '22 at 19:31