Chocolatey のインストール

メモ。

Chocolatey のインストール

公式サイトに手順がある。

chocolatey.org

まず、Win + X を押し、ショートカットメニューが出たら A を押す。これで PowerShell を管理者権限で起動できる

そして、上記の公式サイトに書かれているコマンドをコピーして、PowerShell に貼り付けて実行する

公式サイトでは、その前に Get-ExecutionPolicy を実行するように書かれているが、コピーしたコマンドの冒頭で Set-ExecutionPolicy しているので、やらなくてよさそう。

PS C:\WINDOWS\system32> 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'))
Forcing web requests to allow TLS v1.2 (Required for requests to Chocolatey.org)
Getting latest version of the Chocolatey package for download.
Not using proxy.
Getting Chocolatey from https://chocolatey.org/api/v2/package/chocolatey/0.10.15.
Downloading https://chocolatey.org/api/v2/package/chocolatey/0.10.15 to C:\Users\<ユーザー名>\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip
Not using proxy.
Extracting C:\Users\<ユーザー名>\AppData\Local\Temp\chocolatey\chocoInstall\chocolatey.zip to C:\Users\<ユーザー名>\AppData\Local\Temp\chocolatey\chocoInstall
Installing Chocolatey on the local machine
Creating ChocolateyInstall as an environment variable (targeting 'Machine')
  Setting ChocolateyInstall to 'C:\ProgramData\chocolatey'
WARNING: It's very likely you will need to close and reopen your shell
  before you can use choco.
Restricting write permissions to Administrators
We are setting up the Chocolatey package repository.
The packages themselves go to 'C:\ProgramData\chocolatey\lib'
  (i.e. C:\ProgramData\chocolatey\lib\yourPackageName).
A shim file for the command line goes to 'C:\ProgramData\chocolatey\bin'
  and points to an executable in 'C:\ProgramData\chocolatey\lib\yourPackageName'.

Creating Chocolatey folders if they do not already exist.

WARNING: You can safely ignore errors related to missing log files when
  upgrading from a version of Chocolatey less than 0.9.9.
  'Batch file could not be found' is also safe to ignore.
  'The system cannot find the file specified' - also safe.
chocolatey.nupkg file not installed in lib.
 Attempting to locate it from bootstrapper.
PATH environment variable does not have C:\ProgramData\chocolatey\bin in it. Adding...
警告: Not setting tab completion: Profile file does not exist at
'C:\Users\<ユーザー名>\OneDrive\ドキュメント\WindowsPowerShell\Microsoft.PowerShell_profile.ps1'.
Chocolatey (choco.exe) is now ready.
You can call choco from anywhere, command line or powershell by typing choco.
Run choco /? for a list of functions.
You may need to shut down and restart powershell and/or consoles
 first prior to using choco.
Ensuring Chocolatey commands are on the path
Ensuring chocolatey.nupkg is in the lib folder
PS C:\WINDOWS\system32> 

終わったら、choco を実行してバージョンが表示されればOK。

PS C:\WINDOWS\system32> choco
Chocolatey v0.10.15
Please run 'choco -?' or 'choco <command> -?' for help menu.
PS C:\WINDOWS\system32>

インストール中のメッセージで、choco を使う前に PowerShell ウィンドウを開きなおす必要がありそう、とある。念のため PowerShell を閉じて、Win+X → A で、管理者権限の PowerShell ウィンドウを開きなおし、再度 choco を実行して結果を確認する。

Chocolatey のアンインストール

これも公式サイトにドキュメントがある。

docs.chocolatey.org

一撃でいいなら、インストール先の C:\ProgramData\chocolatey\ をフォルダごと削除すればいいようだ。上記のページでは、サブフォルダの binlib についてはバックアップをとっておいた方がいいかも、ともある。

加えて、環境変数 ChocolateyInstallChocolateyToolsLocationChocolateyLastPathUpdate の 3 つを削除する。また、環境変数 PATH から Chocolatey のパスを外す。

パッケージのインストール

パッケージは公式サイトで検索できる。

chocolatey.org

Chocolatey は管理者権限を必要とするので、管理者権限付きの PowerShell で操作する。

以下は、Go 言語のツールセットをインストールする例。

PS C:\WINDOWS\system32> choco install golang
Chocolatey v0.10.15
Installing the following packages:
golang
By installing you accept licenses for the packages.
Progress: Downloading golang 1.16.2... 100%

golang v1.16.2 [Approved]
golang package files install completed. Performing other installation steps.
The package golang 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

Downloading golang 64 bit
  from 'https://golang.org/dl/go1.16.2.windows-amd64.msi'
Progress: 100% - Completed download of C:\Users\sardine\AppData\Local\Temp\chocolatey\golang\1.16.2\go1.16.2.windows-amd64.msi (118.6 MB).
Download of go1.16.2.windows-amd64.msi (118.6 MB) completed.
Hashes match.
Installing golang...
golang has been installed.
  golang may be able to 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 golang was successful.
  Software installed as 'msi', install location is likely default.

Chocolatey installed 1/1 packages.
 See the log for details (C:\ProgramData\chocolatey\logs\chocolatey.log).
PS C:\WINDOWS\system32>

インストール完了後、PowerShell (一般ユーザーでもよい) を開きなおすと使えるようになる。

> go version
go version go1.16.2 windows/amd64
>