在 Alpine Linux 上安装 PowerShell

GitHub 版本页面上提供有所有可用包。 安装包以后,从终端运行 pwsh。 若已安装预览版,请运行 pwsh-preview。 安装之前,请查看以下支持的版本列表。

注意

PowerShell 7.4 是会删除旧版 PowerShell 7 的就地升级。 PowerShell 的预览版可以与其他版本的 PowerShell 并行安装。 如果需要与以前的版本并行运行 PowerShell 7.4,请使用二进制存档方法重新安装以前的版本。

安装步骤

Alpine 上的安装基于从版本页下载的 tar.gz 包。 包的 URL 取决于要安装的 PowerShell 版本。

  • PowerShell 7.4.5 – https://github.com/PowerShell/PowerShell/releases/download/v7.4.5/powershell-7.4.5-linux-musl-x64.tar.gz
  • PowerShell 7.2.23 – https://github.com/PowerShell/PowerShell/releases/download/v7.2.23/powershell-7.2.23-linux-alpine-x64.tar.gz
  • PowerShell 7.5-preview.3 - https://github.com/PowerShell/PowerShell/releases/download/v7.5.0-preview.3/powershell-7.5.0-preview.3-linux-musl-x64.tar.gz

然后在终端中执行以下 shell 命令,以安装 PowerShell 7.4:

# install the requirements
sudo apk add --no-cache \
    ca-certificates \
    less \
    ncurses-terminfo-base \
    krb5-libs \
    libgcc \
    libintl \
    libssl1.1 \
    libstdc++ \
    tzdata \
    userspace-rcu \
    zlib \
    icu-libs \
    curl

sudo apk -X https://dl-cdn.alpinelinux.org/alpine/edge/main add --no-cache \
    lttng-ust

# Download the powershell '.tar.gz' archive
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.4.5/powershell-7.4.5-linux-musl-x64.tar.gz -o /tmp/powershell.tar.gz

# Create the target folder where powershell will be placed
sudo mkdir -p /opt/microsoft/powershell/7

# Expand powershell to the target folder
sudo tar zxf /tmp/powershell.tar.gz -C /opt/microsoft/powershell/7

# Set execute permissions
sudo chmod +x /opt/microsoft/powershell/7/pwsh

# Create the symbolic link that points to pwsh
sudo ln -s /opt/microsoft/powershell/7/pwsh /usr/bin/pwsh

# Start PowerShell
pwsh

卸载 PowerShell

sudo rm -rf /usr/bin/pwsh /opt/microsoft/powershell

PowerShell 路径

  • $PSHOME/opt/microsoft/powershell/7/
  • 配置文件脚本存储在以下位置:
    • AllUsersAllHosts - $PSHOME/profile.ps1
    • AllUsersCurrentHost - $PSHOME/Microsoft.PowerShell_profile.ps1
    • CurrentUserAllHosts - ~/.config/powershell/profile.ps1
    • CurrentUserCurrentHost - ~/.config/powershell/Microsoft.PowerShell_profile.ps1
  • 模块存储在以下位置:
    • 用户模块 - ~/.local/share/powershell/Modules
    • 共享模块 - /usr/local/share/powershell/Modules
    • 默认模块 - $PSHOME/Modules
  • PSReadline 历史记录会记录到 ~/.local/share/powershell/PSReadLine/ConsoleHost_history.txt

配置文件采用 PowerShell 的按主机配置,所以默认主机特定配置文件位于相同位置下的 Microsoft.PowerShell_profile.ps1 中。

PowerShell 采用 Linux 上的 XDG 基目录规范

支持的版本

Microsoft 在 PowerShell 到达支持终止日期Alpine 版本到达生命周期终点之前仍然支持 PowerShell。

Microsoft 工件注册表中提供了包含 x64 版 PowerShell 7.2、PowerShell 7.4 和 PowerShell 7.5-preview 的 Docker 映像,适用于以下 Alpine 版本:

  • Alpine 3.17 - OS 支持于 2024-11-22 结束

PowerShell 的 Docker 映像不适用于 Alpine 3.18 和 3.19。

重要

Docker 映像基于操作系统 (OS) 分发服务器提供的正式 OS 映像生成。 这些映像可能没有最新的安全更新。 Microsoft 建议将 OS 包更新到最新版本,以确保应用最新的安全更新。

安装支持

Microsoft 支持本文档中的安装方法。 其他第三方源可能会提供其他安装方法。 尽管这些工具和方法可能有效,但 Microsoft 无法支持这些方法。