Building PowerShell on Linux: A Comprehensive Guide

If you’re a developer looking to build PowerShell on Linux, you’ve come to the right place. This guide will walk you through the process step-by-step, ensuring you have a smooth and successful experience.

Setting Up the Environment

The first step is to set up your development environment. This guide assumes you’re using Ubuntu 16.04 LTS, as that’s the distribution the PowerShell team primarily uses. However, the build module should work on a best-effort basis for other Linux distributions as well.

Git Setup

Before you can start building PowerShell, you’ll need to ensure your Git setup is correct. Refer to the Working with the PowerShell Repository, README, and Contributing Guidelines for more information.

This guide assumes you have recursively cloned the PowerShell repository and navigated into it.

Toolchain Setup

To build the managed components of PowerShell, we use the .NET Command-Line Interface (dotnet). Setting up the toolchain is straightforward if you have a self-hosted copy of PowerShell on Linux.

First, you’ll need to download and install the PowerShell package. You can do this by following the instructions in the Alternate ways to install PowerShell on Linux guide.

Alternatively, you can use the ./tools/install-powershell.sh script provided in the PowerShell repository to install the package.

Once you have PowerShell installed, you can import the build module and bootstrap the dependencies:

Import-Module ./build.psm1
Start-PSBootstrap

The Start-PSBootstrap function performs the following tasks:

  1. Adds the LLVM package feed.
  2. Installs the dependencies required for the .NET CLI toolchain and the PowerShell build process via apt-get.
  3. Uninstalls any prior versions of the .NET CLI.
  4. Downloads and installs 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 Module

Now that you have your environment set up, you can use the PowerShell module maintained in the repository to build PowerShell. Since this is PowerShell code, it requires self-hosting, which you should have set up in the previous step.

Import-Module ./build.psm1
Start-PSBuild

Congratulations! If everything went well, you should now have a successfully built version of PowerShell. The Start-PSBuild script will output the location of the executable, which you can then run:

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

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 with this comprehensive guide, you should be able to set up your development environment and successfully build the PowerShell binaries. If you encounter any issues, be sure to refer to the provided resources and the PowerShell repository’s documentation for further assistance.

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