Building PowerShell on macOS: A Comprehensive Guide

Building PowerShell on macOS is a straightforward process, but there are a few important steps to keep in mind. This guide will walk you through the entire process, from setting up your development environment to building and running PowerShell on your macOS machine.

Prerequisites

Before you begin, you’ll need to have a few things installed on your macOS system:

  1. Homebrew or MacPorts, which are package managers for macOS that will help you install the necessary dependencies.
  2. The .NET Core SDK, which you can install using the Start-PSBootstrap function (more on that later).

Setting Up the Development Environment

  1. Install Homebrew or MacPorts: If you haven’t already, install either Homebrew or MacPorts. These package managers will help you install the necessary dependencies for building PowerShell.

  2. Install .NET Core SDK: From a PowerShell session, run the Import-Module ./build.psm1 command, followed by Start-PSBootstrap. This function will use Homebrew or MacPorts to install OpenSSL and GNU WGet, uninstall any prior versions of the .NET CLI, and download and install the .NET Core SDK to ~/.dotnet.

  3. Add .NET Core SDK to your PATH: If you want to use the dotnet command outside of the Start-PSBuild function, add ~/.dotnet to your PATH environment variable.

Building PowerShell

  1. Start a PowerShell session: Run the pwsh command to start a new PowerShell session.

  2. Build PowerShell: From the PowerShell session, use the Start-PSBuild function from the build.psm1 module to build PowerShell.

  3. Find the built PowerShell binary: After the build completes, you’ll find the PowerShell binary at ./src/powershell-unix/bin/Debug/net6.0/osx-x64/publish/pwsh.

Addressing the “Too Many Open Files” Issue

One common issue you may encounter when building PowerShell on macOS is the “Too many open files” error. This is due to a bug in NuGet, and you can fix it by running ulimit -n 2048 in your session. To make this change permanent, add the command to your shell’s profile.

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

Conclusion

Building PowerShell on macOS is a straightforward process, but it’s important to follow the right steps and be aware of potential issues like the “Too many open files” error. By following this guide, you should be able to set up your development environment, build PowerShell, and address any common problems you encounter along the way.

For more information, please refer to the original instructions.