在Ubuntu 10.04上安装jq JSON处理器

99

有没有一种方法可以在Ubuntu 10.04上安装jq JSON处理器?

我尝试了通常的sudo apt-get install jq但是出现了错误E:无法找到软件包jq


没有sudo权限怎么办?我的集群不允许我安装它,但是IT部门允许我自己安装东西。 - Charlie Parker
5个回答

177

可以执行sudo apt-get install jq,但需要告诉系统在哪里找到jq。

ℹ️ 注意:Ubuntu 14+用户可以跳过第3步!

安装

  1. 使用文本编辑器打开您的源文件:

     sudo vim /etc/apt/sources.list
    
  2. 在文件末尾添加以下行(注意deb不是一个命令,更多信息):

    deb http://us.archive.ubuntu.com/ubuntu vivid main universe

  3. 然后重新索引apt-get,以便它可以找到jq

     sudo apt-get update
    
    然后进行正常安装,你将会成为自豪的新用户jq
     sudo apt-get install jq
    

测试

测试它是否可用!尝试这个示例以查看它漂亮地打印一些示例 JSON

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

在您的终端中,结果应该看起来像这样:

{
  "name": "John",
  "age": 31,
  "city": "New York"
}

2
在Ubuntu 14上,我不得不使用旧版本源“deb http://old-releases.ubuntu.com/ubuntu vivid main universe”。 - vaichidrewar
3
sudo apt-get update命令会出现一些错误,例如“无法下载某些索引文件。它们已被忽略或者使用旧的文件代替。”执行sudo apt-get install jq仍然失败。如何修复这个问题(Ubuntu 17.04)?自动更新也失败了,告诉我检查我的网络连接,但其他网络访问正常(Git、Firefox等)。顺便说一下,这是在虚拟机中运行的。 - CodeManX
1
我刚刚在树莓派上成功执行了apt-get install jq,而不需要修改sources.list - ᴍᴇʜᴏᴠ
1
哦,太好了!不过,遗憾的是Ubuntu 10.04(以及类似版本)的用户并不那么容易。 - Stefan Collier
3
在执行sudo apt-get update之前,我一直遇到E: Couldn't find package jq错误。因此,对于版本14及以上的系统,最好从第3步开始操作。 - artifex
没有sudo权限怎么办?我的集群不允许我安装它,但是IT部门允许我自己安装东西。 - Charlie Parker

43
自 Ubuntu 16.04LTS 版本以来的 xenial,您无需修改 /etc/apt/sources.list 文件,只需运行即可。
sudo apt-get install jq

jq 1.5已包含在官方DebianUbuntu软件仓库中。


没有sudo权限怎么办?我的集群不允许我安装它,但是IT部门允许我自己安装东西。 - Charlie Parker


4

按照https://stedolan.github.io/jq/download/中所述的方式从源代码下载并构建,具体请参考最后一个章节 "在Linux、OS X、Cygwin和其他类POSIX操作系统上从源代码构建"。


2
我之前并不知道jq在其他回答建议的适当分发渠道中存在,但既然如此,那么其他答案中的一个应该成为首选/被接受的答案。 - Hans Z.

0

如果您没有sudo权限,请执行以下操作:

#!/usr/bin/env bash

# conda activate jq_install_env

# - Install jq without sudo
# Clone the jq repository from GitHub
cd $HOME
git clone https://github.com/stedolan/jq.git $HOME/jq
cd $HOME/jq
git submodule update --init

# Compile jq from source
cd $HOME/jq
autoreconf -fi
./configure --with-oniguruma=builtin --disable-maintainer-mode --prefix=$HOME/.local
make -j8 && make check
make install
ls $HOME/.local
ls $HOME/.local/bin

# Add the directory where the jq binary file is located to your PATH environment variable
echo 'PATH=$PATH:~/.local/bin/' >> $HOME/.bashrc.lfs
export PATH=$PATH:~/.local/bin/
echo $PATH | tr ':' '\n'

# Reload your shell configuration file to update your PATH environment variable
source $HOME/.bashrc.lfs

# Verify that jq is installed and working
jq --version

参考链接:https://suzyahyah.github.io/misc/2020/03/31/jq-without-sudo.html

如果链接失效:

Landing Page...
AboutCategoriesAdvisors
Recipe for building jq from source without admin(sudo) rights
Mar 31, 2020

This took me some time to install. Just putting it out there in case it helps someone.
Get the latest jq from github

git clone https://github.com/stedolan/jq.git

Update submodules (onigurama)

git submodule update --init

Copy missing auxiliary files

autoreconf -fi

Install into {YOUR_HOME_DIR} with onigurama (regex library)

./configure --with-oniguruma=builtin --disable-maintainer-mode --prefix={YOUR_HOME_DIR}.local

Check that we have all the dependencies downloaded and install

make -j8 && make check

make install

Finally, add this to your ~/.bashrc and to call jq from anywhere. Remember to >source ~/.bashrc and you should be good to go.

export PATH=$PATH:~/.local/bin/

« The Sigmoid in Regression, Neural Network Activation and LSTM GatesA minimum keystroke (py)Debugger for Lazy ML/DS people who don't IDE »
Quality means doing it right when no one is looking - Henry Ford
 suzyahyah
 suzyahyah
The best time to plant a tree was 20 years ago. The second best time is now. - Japanese proverb

Since October 2017

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接