在 Alpine Linux 上安装 PowerShell

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

较新版本的 PowerShell 7 取代了现有 PowerShell 7 版本。 PowerShell 的预览版可以与其他版本的 PowerShell 并行安装。 较新的预览版本将替换现有的以前的预览版本。 如果需要与以前的版本并行运行 PowerShell 7.5,请使用 二进制存档 方法重新安装以前的版本。

注意

本文中的安装命令适用于 PowerShell 的最新稳定版本。 若要安装其他版本的 PowerShell,请调整命令以匹配所需的版本。 以下链接将引导你到 GitHub 上每个版本的最新发布页面。

在“发布”页的“资产”部分中可以找到每个包的下载链接。 由于资产部分可能处于折叠状态,因此可能需要单击展开它。

安装步骤

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

  • PowerShell 7.4 - https://github.com/PowerShell/PowerShell/releases/download/v7.4.13/powershell-7.4.13-linux-musl-x64.tar.gz
  • PowerShell 7.5 - https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/powershell-7.5.4-linux-musl-x64.tar.gz
  • PowerShell 7.6-preview - https://github.com/PowerShell/PowerShell/releases/download/v7.6.0-preview.5/powershell-7.6.0-preview.5-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 \
    libssl3 \
    libstdc++ \
    tzdata \
    userspace-rcu \
    zlib \
    icu-libs \
    curl

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

# Download the powershell '.tar.gz' archive
curl -L https://github.com/PowerShell/PowerShell/releases/download/v7.5.4/powershell-7.5.4-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
    • 所有用户当前主机 - $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。

.NET SDK 的 Docker 映像包含最新版本的 PowerShell。 这些映像可从 Microsoft工件注册表获取。

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

这些映像用于测试目的。 如果你需要用于生产工作负载的 Docker 映像,则应该构建和维护自己的映像。

安装支持

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