Building PowerShell on macOS can be a straightforward process, but there are a few important considerations to keep in mind. This comprehensive guide will walk you through the necessary steps to set up your development environment and successfully build PowerShell on your macOS machine.

Environment Setup

To get started, you’ll need to install either Homebrew or MacPorts - the two most popular package managers for macOS. These tools will help you install the required dependencies for building PowerShell.

Once you have Homebrew or MacPorts set up, follow these steps:

  1. Download and Install PowerShell: Start by downloading and installing a self-hosted copy of PowerShell on your macOS machine. You can do this by following the same instructions as the Linux build guide.

  2. Install Dependencies: From the PowerShell prompt, run the Import-Module ./build.psm1 command, and then use the Start-PSBootstrap function. This will:

    • Use brew or port to install OpenSSL and GNU WGet
    • Uninstall any prior versions of the .NET CLI
    • Download and install the .NET Core SDK to ~/.dotnet
  3. Add .NET to Your PATH: If you want to use the dotnet command outside of the Start-PSBuild function, be sure to add ~/.dotnet to your system’s PATH environment variable.

Addressing the ‘Too Many Open Files’ Error

One common issue that can arise when building PowerShell on macOS is the ‘Too many open files’ error. This is due to a bug in NuGet, which can cause the dotnet restore command to fail.

To work around this, you can run the following command in your session:

ulimit -n 2048

This will increase the maximum number of open files to 2048. To make this change permanent, you can add the ulimit -n 2048 command to your shell’s profile (e.g., ~/.bashrc or ~/.zshrc).

Unfortunately, we cannot do this automatically in the build module due to issue #847 in the PowerShell repository.

Building PowerShell Using the Module

Once you have your environment set up, you can start a PowerShell session by running pwsh, and then use the Start-PSBuild function from the build module to begin the build process.

After the build completes, you’ll find the resulting PowerShell executable at ./src/powershell-unix/bin/Debug/net6.0/osx-x64/publish/pwsh.

Building PowerShell on macOS requires a bit of setup, but with the right tools and guidance, you can get a successful build up and running in no time. Remember to address the ‘Too Many Open Files’ error and you’ll be on your way to contributing to the PowerShell project on macOS.