Building PowerShell on Linux can be a daunting task, but with the right guidance, it’s a rewarding process that can deepen your understanding of the platform. This comprehensive guide will take you through the necessary steps to set up your environment, install the required toolchain, and build PowerShell from the ground up.

Setting Up Your Environment

The first step is to ensure your environment is properly configured for building PowerShell on Linux. This guide assumes you are using Ubuntu 16.04 LTS, as that is the distribution the PowerShell team uses. However, the build module should work on a best-effort basis for other distributions as well.

Git Setup

Before you can begin, you’ll need to set up Git correctly. Refer to the Working with the PowerShell Repository, PowerShell README, and Contributing Guidelines for detailed instructions. This guide assumes you have recursively cloned the PowerShell repository and navigated into it.

Toolchain Setup

PowerShell relies on the .NET Command-Line Interface (dotnet) to build the managed components. Installing the toolchain is a straightforward process:

  1. Run the Start-PSBootstrap command in a PowerShell session. This will:
    • Add the LLVM package feed
    • Install the necessary dependencies via apt-get
    • Uninstall any prior versions of the .NET CLI
    • Download and install the .NET Core SDK 2.0.0 to ~/.dotnet

If you want to use dotnet outside of Start-PSBuild, be sure to add ~/.dotnet to your PATH environment variable.

Building PowerShell Using the Provided Module

The PowerShell team maintains a PowerShell module with the Start-PSBuild function to handle the build process. Since this is PowerShell code, it requires a self-hosted copy of PowerShell on Linux.

  1. Import the build.psm1 module:

    Import-Module ./build.psm1
    
  2. Run the Start-PSBuild command:

    Start-PSBuild
    

Congratulations! If everything went according to plan, you should now have a fully-built version of PowerShell. The Start-PSBuild script will output the location of the executable, which you can run using the command:

./src/powershell-unix/bin/Debug/net6.0/linux-x64/publish/pwsh

This will launch the PowerShell Core instance you just built. You can also run the cross-platform Pester tests with Start-PSPester and the xUnit tests with Start-PSxUnit.

Building PowerShell on Linux may seem daunting at first, but by following this step-by-step guide, you’ll be able to set up your environment, install the necessary toolchain, and successfully build PowerShell from the ground up. Happy coding!

[Source: https://raw.githubusercontent.com/PowerShell/PowerShell/master/docs/building/linux.md]