schedule2021-04-25

WindowsのパッケージマネジメントツールのChocolateyをインストールする

RedhatのyumやDebianのaptのように、WindowsでもCLIでパッケージを管理するツールがあります。 それがChocolateyです。 よく利用しているNode.jsやPythonなどのバージョン管理もChocolateyで出来るようです。 Chocolateyを導入していきます。

インストール済のアプリがある場合はchocolateyでアップデート出来るかも試しました。

Contents

WindowsのパッケージマネージャとChocolatey

chocolatey.org

chocolatey

Windowsでアプリをインストールのは、基本的に、Windowsストアやアプリのサイトへ行ってインストーラーをダウンロードしてポチポチNEXTをクリックしてインストールしています。 アップデートするときもインストーラーを探してダウンロードして…って手順を繰り越してました。

CentoOSのyumやDebianのapt、MacのhomebrewのようにCLIで管理できればコマンドひとつで導入できますし、スクリプトを書いて環境構築を自動化することも可能になります。

Windowsのパッケージマネージャは今までもScoopOneGetなどが作られてきましたが、構築の速度や扱いやすさからChocolateyが人気なようです。

また、2021年5月から正式リリース予定のwingetがMicrosoft社で開発されています。 ChocolateyとWingetの雑感も書きました。

WindowsのパッケージマネジメントツールChocolateyとWingetの雑感 | suzu6.net

Chocolateyのインストール方法

INSTALLING CHOCOLATEY | chocolatey.org

要件

TLS 1.2の要件は以下の通りです。

  • Windows 7+ / Windows Server 2003+
  • PowerShell v3+
  • .NET Framework 4.5+ (インストールされていない場合は、.NET 4.0のインストールを試みます)

1. PowerShellを管理者権限で開く

PowerShell ドキュメント | docs.microsoft.com

2. インストールコマンドを実行する

次のコマンドを実行します。

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))

ps

((base)となってるのはPythonのanacondaの仮想環境です。)

エラーがなくインストール出来たら、choco -vでバージョンの確認をします。

> choco -v
0.10.15

OK!

chocolateyのパッケージを探してみる

パッケージを探すのであればcommunityのページが便利。現在8,438件が登録されている。 packages | community.chocolatey.org

CLIで検索する場合。 LIST/SEARCH | docs.chocolatey.org

choco search <filter> [<options/switches>]
choco list <filter> [<options/switches>]
clist <filter> [<options/switches>]

例) gitを検索する

> choco search git
Chocolatey v0.10.15
git 2.31.1 [Approved]
git-helper 1.5 - Possibly broken
Git-TF 2.0.3.20131219 [Approved]
git-it 4.4.0 [Approved]

(省略)

210 packages found.

chocolateyでパッケージをインストールしてみる

INSTALL | docs.chocolatey.org

既に手動でアプリをインストールしている場合も、chocoateyで管理できました。

インストーラーでインストール済のアプリをchoco isntallする

gitはインストール済だが、chocolateyではまだ管理されていない。 git

ローカルのchocolateyのリストを確認してみます。

> choco list --local-only
Chocolatey v0.10.15
chocolatey 0.10.15
1 packages installed.

初期状態ではどのアプリもリストにありません。

この状態でchoco installでgitをインストールしてみる。

> choco install git
Chocolatey v0.10.15
Installing the following packages:
git
By installing you accept licenses for the packages.
Progress: Downloading git.install 2.31.1... 100%
Progress: Downloading chocolatey-core.extension 1.3.5.1... 100%
Progress: Downloading git 2.31.1... 100%

chocolatey-core.extension v1.3.5.1 [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'

git.install v2.31.1 [Approved]
git.install package files install completed. Performing other installation steps.
The package git.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/[A]ll - yes to all/[N]o/[P]rint): y

Using Git LFS
Installing 64-bit git.install...
git.install has been installed.
git.install installed to 'C:\Program Files\Git'
  git.install can be automatically uninstalled.
Environment Vars (like PATH) have changed. Close/reopen your shell to
 see the changes (or in powershell/cmd.exe just type `refreshenv`).
 The install of git.install was successful.
  Software installed to 'C:\Program Files\Git\'

git v2.31.1 [Approved]
git package files install completed. Performing other installation steps.
 The install of git 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).

Enjoy using Chocolatey? Explore more amazing features to take your
experience to the next level at
 https://chocolatey.org/compare

既にインストールされていることを認識したうえで、新しいバージョンへ更新できました。

gitのバージョンがv2.29.1からv2.31.1にアップデートしています。

> git --version
git version 2.31.1.windows.1

git-new

Chocolateyのlistにも追加されました。

2> choco list --local-only
Chocolatey v0.10.15
chocolatey 0.10.15
chocolatey-core.extension 1.3.5.1
git 2.31.1
git.install 2.31.1
4 packages installed.

パッケージをアップグレードする

UPGRADE | docs.chocolatey.org

choco upgrade <pkg|all> [<pkg2> <pkgN>] [<options/switches>]
cup <pkg|all> [<pkg2> <pkgN>] [<options/switches>]

choco upgrade chocolatey
choco upgrade notepadplusplus googlechrome atom 7zip
choco upgrade notepadplusplus googlechrome atom 7zip -dvfy
choco upgrade git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'"
choco upgrade git -y --params="'/GitAndUnixToolsOnPath /NoAutoCrlf'" --install-args="'/DIR=C:\git'"
# Params are package parameters, passed to the package
# Install args are installer arguments, appended to the silentArgs
#  in the package for the installer itself
choco upgrade nodejs.install --version 0.10.35
choco upgrade git -s "'https://somewhere/out/there'"
choco upgrade git -s "'https://somewhere/protected'" -u user -p pass
choco upgrade all
choco upgrade all --except="'skype,conemu'"

※ updateコマンドもあるが非推奨になっている。

パッケージをアンインストールする

UNINSTALL | docs.chocolatey.org

choco uninstall <pkg|all> [pkg2 pkgN] [options/switches]
cuninst <pkg|all> [pkg2 pkgN] [options/switches]

choco uninstall git
choco uninstall notepadplusplus googlechrome atom 7zip
choco uninstall notepadplusplus googlechrome atom 7zip -dv
choco uninstall ruby --version 1.8.7.37402
choco uninstall nodejs.install --all-versions

Chocolateyのアンインストール方法

HOW TO UNINSTALL | docs.chocolatey.org

Chocolateyが気に入らない場合は、フォルダー(およびフォルダーが作成する環境変数)を削除するだけでアンインストールできます。

  • フォルダーのパス: C:\ProgramData\chocolatey
  • 環境変数: 以下の4つ
    • ChocolateyInstall
    • ChocolateyToolsLocation
    • ChocolateyLastPathUpdate
    • PATH

また、リンクにはChocolateyを削除するスクリプトがあります。 スクリプトを確認してから実行してください。