Install PowerShell on Ubuntu 22.04 or 20.04
PowerShell isn’t just for Windows; you can run it natively on Ubuntu 22.04 and 20.04. This guide shows the exact commands, why each step matters, and what to watch out for if things go sideways.
Installing the Microsoft Repository
1. Update your package list –
sudo apt update
If you skip this, apt will complain that it can’t find the new repo packages.
2. Install prerequisites –
sudo apt install -y wget apt-transport-https software-properties-common
These tools let your machine fetch the Microsoft signing key over HTTPS and add a new repository source.
3. Download Microsoft’s GPG key –
wget https://packages.microsoft.com/config/ubuntu/$(lsb_release -rs)/prod.list -O /etc/apt/sources.list.d/microsoft-prod.list
This is the one line that tells apt where PowerShell lives. Don’t miss it; otherwise, you’ll get a “no such file or directory” error.
4. Refresh package lists again –
sudo apt update
5. Install PowerShell –
sudo apt install -y powershell
Now you have the pwsh binary in /usr/bin. No more fiddling with Snap or Flatpak.
Verifying the Installation
pwsh -NoLogo -Command "$PSVersionTable.PSVersion"
The output should be something like 7.3.x. If you get a “command not found” error, double‑check that /usr/bin is in your $PATH.
Common Pitfalls (and how to avoid them)
- Missing the GPG key –
If you see “The following signatures couldn't be verified because the public key is missing,” run apt-key adv --keyserver keyserver.ubuntu.com --recv-keys with the correct ID from Microsoft’s docs.
- Broken network during install –
I’ve seen this happen after a bad driver update that broke the kernel module for Wi‑Fi, causing wget to stall. A simple retry usually fixes it.
- Conflicting PowerShell packages –
If you previously installed PowerShell via Snap or a third‑party repo, remove those first: sudo snap remove powershell or sudo apt purge powershell.
Using PowerShell with Ubuntu Packages
Once PowerShell is up, you can call native package managers from it:
# List all installed packages Get-ChildItem /var/lib/dpkg/status | Select-String "^Package:" -NotMatch # Install a new Ubuntu package from within PowerShell sudo apt install -y git
It’s handy when you’re scripting cross‑platform setups and want to keep the same syntax for Windows, macOS, and Linux.
A Quick Script Example
pwsh -NoLogo -Command @' # Update system sudo apt update; sudo apt upgrade -y # Install common dev tools sudo apt install -y git curl build-essential # Show PowerShell version Write-Host "PowerShell version:" ($PSVersionTable.PSVersion) '@
Run it once, and you’ve got a fresh machine ready for whatever project comes next.
That’s all there is to it. Drop the commands into your terminal, hit enter, and enjoy scripting on Ubuntu like a pro.