使用 choco 安装软件

相信从 DOS、Win9x 年代甚至更早玩过来的用户来说, 黑乎乎的命令行是一个或多或少都接触过的工具。 现在图形界面的大量普及, 看上去挺高深的命令行变成了很多人眼中的极客工具, 但其实命令行是人机交互最简单直接的方式, 我们现在也可以用命令行完成一些日常工作,比如用命令行管理常用软件

更新

我现在多数时间使用 scoop 而不是 choco

前言

现在我们安装软件,要么去各种各样的网站下载安装包, 要么使用一些系统管家,前者有可能下载到不安全的或不需要的安装包, 后者则要忍受各种全家桶的困扰,但其实,通过一个软件, 我们就可以用命令行安装各种常用软件了,这就是我们要介绍的 chocolatey.

安装 chocolatey

chocolatey 官网 上提供了多种安装的方法,简单起见我们只介绍使用 powershell 安装 chocolatey.

打开 powershell 命令行

打开开始菜单,用键盘输入 powershell,然后右键选择「使用管理员身份」运行,如弹出 用户帐户控制(UAC) 窗口 ,选择

https://i.loli.net/2018/07/14/5b49930ca3d5c.png

安装 chocolatey

复制以下命令到 powershell 命令行,回车执行

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

等待安装完成后,执行以下命令

choco -V

如正确显示了版本号信息,说明 choco 已经正确安装了, 接下来我们就可以使用 choco 安装软件啦,我们以 7zip 为例

使用 choco 安装 7zip

7zip 是一个免费开源的压缩软件,支持压缩成压缩率较高的 7z 格式, 也支持解压 zip、rar 格式的压缩包,比较常用,我们现在就用 choco 来安装 7zip,来熟悉一下 choco 的操作

我们可以使用下面的命令来搜索一下 choco 中名字包含 7zip 的软件, 注意使用 choco 时要打开 使用管理员权限的 powershell 命令行, 否则可能会导致安装失败

choco search 7zip
Chocolatey v0.10.11
7zip 18.5 [Approved]
7zip.install 18.5 [Approved]
7zip.commandline 16.02.0.20170209 [Approved]
7zip.portable 18.5 [Approved]
peazip 6.6.0 [Approved]
peazip.install 6.6.0 [Approved]
TotalCommander 9.20 [Approved]
peazip.portable 6.0.3 [Approved] Downloads cached for licensed users
Easy7zip 0.1.6 [Approved] Downloads cached for licensed users
kellyelton.devenvironment 1.0.0.11 - Possibly broken
freearc.install 0.666 [Approved] Downloads cached for licensed users - Possibly broken for FOSS users (due to original download location changes by vendor)
freearc 0.666 [Approved] - Possibly broken
freearc.portable 0.666 [Approved] Downloads cached for licensed users - Possibly broken for FOSS users (due to original download location changes by vendor)
jivkok.tools 1.1.0.7 - Possibly broken
winmerge-7z 0028.465.920 [Approved]
UAdevelopers.utils 1.9 - Possibly broken
shaman-dokan-archive 1.0 [Approved] Downloads cached for licensed users
17 packages found.

标有 [Approved] 字样的软件是可以正常安装的,而 Possibly broken 则是很久没有维护的软件,虽然有不少软件列出来, 但是根据名字, 最符合我们需求的软件应该就是名为 7zip 的软件, 我们可以用以下命令查看一下 7zip 的详细信息

choco info 7zip
Chocolatey v0.10.11
7zip 18.5 [Approved]
 Title: 7-Zip | Published: 5/1/2018
 Package approved as a trusted package on Jul 05 2018 07:25:11.
 Package testing status: Passing on May 01 2018 06:45:31.
 Number of Downloads: 1548368 | Downloads for this version: 225540
 Package url
 Chocolatey Package Source: https://github.com/chocolatey/chocolatey-coreteampackages/tree/master/automatic/7zip
 Package Checksum: 'WRXHCumB6IrgpdbURqoRePgqsqqZSA8jjEedqqKwivAOugjTTdvnjNdE64zivYtgdC8S0ehYqVHfXyWKlJ9znQ==' (SHA512)
 Tags: 7zip zip archiver admin foss
 Software Site: http://www.7-zip.org/
 Software License: http://www.7-zip.org/license.txt
 Documentation: http://www.7-zip.org/faq.html
 Mailing List: https://sourceforge.net/p/sevenzip/discussion/45797/
 Issues: https://sourceforge.net/p/sevenzip/_list/tickets?source=navbar
 Summary: 7-Zip is a file archiver with a high compression ratio.
 Description: 7-Zip is a file archiver with a high compression ratio.

  ## Features
  - High compression ratio in [7z format](http://www.7-zip.org/7z.html) with **LZMA** and **LZMA2** compression
  - Supported formats:
  - Packing / unpacking: 7z, XZ, BZIP2, GZIP, TAR, ZIP and WIM
  - Unpacking only: AR, ARJ, CAB, CHM, CPIO, CramFS, DMG, EXT, FAT, GPT, HFS, IHEX, ISO, LZH, LZMA, MBR, MSI, NSIS, NTFS, QCOW2, RAR, RPM, SquashFS, UDF, UEFI, VDI, VHD, VMDK, WIM, XAR and Z.
  - For ZIP and GZIP formats, **7-Zip** provides a compression ratio that is 2-10 % better than the ratio provided by PKZip and WinZip
  - Strong AES-256 encryption in 7z and ZIP formats
  - Self-extracting capability for 7z format
  - Integration with Windows Shell
  - Powerful File Manager
  - Powerful command line version
  - Plugin for FAR Manager
  - Localizations for 87 languages

  ## Notes
  - The installer for 7-Zip is known to close the explorer process.
  This means you may lose current work. If it doesn't automatically restart explorer, type `explorer` on the command shell to restart it.
 Release Notes: http://www.7-zip.org/history.txt

1 packages found.

Did you know Pro / Business automatically syncs with Programs and Features? Learn more about Package Synchronizer at https://chocolatey.org/compare

看介绍和下载数,这应该就是我们要寻找的安装包了, 通常不确定安装包是否是我们所需的时候,使用 info 可以让我们知道包的详细信息。接下来我们用以下命令来安装 7zip

choco install 7zip
Chocolatey v0.10.11
Installing the following packages:
7zip
By installing you accept licenses for the packages.
Progress: Downloading 7zip.install 18.5... 100%
Progress: Downloading chocolatey-core.extension 1.3.3... 100%
Progress: Downloading 7zip 18.5... 100%

chocolatey-core.extension v1.3.3 [Approved]
chocolatey-core.extension package files install completed. Performing other installation steps.
 Installed/updated chocolatey-core extensions.
 The install of chocolatey-core.extension was successful.
  Software installed to 'C:\ProgramData\chocolatey\extensions\chocolatey-core'

7zip.install v18.5 [Approved]
7zip.install package files install completed. Performing other installation steps.
The package 7zip.install wants to run 'chocolateyInstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint):

输入 y 执行安装脚本,或者在安装命令后面增加参数 -y 会自动确认操作

Installing 64 bit version
Installing 7zip.install...
7zip.install has been installed.
7zip installed to 'C:\Program Files\7-Zip'
Added C:\ProgramData\chocolatey\bin\7z.exe shim pointed to 'c:\program files\7-zip\7z.exe'.
  7zip.install may be able to be automatically uninstalled.
 The install of 7zip.install was successful.
  Software installed to 'C:\Program Files\7-Zip\'

7zip v18.5 [Approved]
7zip package files install completed. Performing other installation steps.
 The install of 7zip was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

Chocolatey installed 3/3 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

我们可以看到,choco 安装了三个包,包括 choco 自己的扩展, 7zip 的安装包, 以及 7zip 本身,这样 7zip 就已经正确安装了, 终于我们不用去官网或下载站寻找下载连接, 然后再自己执行安装包了,一个命令行命令全搞定

使用 choco 卸载 7zip

choco 卸载 7zip 同样是使用命令行,但是只能卸载使用 choco 安装的软件

choco uninstall 7zip
Chocolatey v0.10.11
Uninstalling the following packages:
7zip
You are uninstalling 7zip, which is likely a metapackage for an
 *.install/*.portable package that it installed
 (7zip represents discoverability).
Would you like to uninstall 7zip.install as well?([Y]es/[N]o):

输入 y 同时卸载安装脚本

7zip v18.5
 Skipping auto uninstaller - No registry snapshot.
 7zip has been successfully uninstalled.

7zip.install v18.5
The package 7zip.install wants to run 'chocolateyUninstall.ps1'.
Note: If you don't run this script, the installation will fail.
Note: To confirm automatically next time, use '-y' or consider:
choco feature enable -n allowGlobalConfirmation
Do you want to run the script?([Y]es/[N]o/[P]rint):

Input 'y' again to uninstall 7zip packages

Uninstalling 7zip.install...
0
7zip.install has been uninstalled.
 Running auto uninstaller...
 Skipping auto uninstaller - '7-Zip 18.05 (x64)' appears to have been uninstalled already by other means.
 7zip.install has been successfully uninstalled.

Chocolatey uninstalled 2/2 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

这样,7zip 和 7zip 的安装包都已经卸载了,但是 choco 的核心扩展保留了下来,那么这个核心扩展是干什么的呢, 从 官网包资料 中我们可以知道,这个是安装包的一个基础依赖项, 很多安装包都会用到,所以我们可以予以保留, 而且再安装时也不需要重新安装这个核心扩展,比如我们再次安装 7zip

choco install 7zip -y
Chocolatey v0.10.11
Installing the following packages:
7zip
By installing you accept licenses for the packages.
Progress: Downloading 7zip 18.5... 100%

7zip.install v18.5 [Approved]
7zip.install package files install completed. Performing other installation steps.
Installing 64 bit version
Installing 7zip.install...
7zip.install has been installed.
7zip installed to 'C:\Program Files\7-Zip'
Added C:\ProgramData\chocolatey\bin\7z.exe shim pointed to 'c:\program files\7-zip\7z.exe'.
  7zip.install may be able to be automatically uninstalled.
 The install of 7zip.install was successful.
  Software installed to 'C:\Program Files\7-Zip\'

7zip v18.5 [Approved]
7zip package files install completed. Performing other installation steps.
 The install of 7zip was successful.
  Software install location not explicitly set, could be in package or
  default install location if installer.

Chocolatey installed 2/2 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).

我们可以看到这次只安装了两个包,7zip 和 7zip 的安装包

总结

有了 choco 这个一站式的命令行工具,我们安装时只要敲几个命令, 安装包就会自动下载并执行了,是不是感觉安装软件变得比较简单明了了呢? 总结一下使用到的命令

choco search 7zip 	# 查找含有 7zip 内容的包
choco info 7zip 	# 查看 7zip 包的资料
choco install 7zip 	# 安装 7zip
choco uninstall 7zip 	# 卸载 7zip