'sudo' is not recognized as an internal or external command

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# 'sudo' is not recognized as an internal or external command

The error "'sudo' is not recognized as an internal or external command, operable program or batch file" occurs because sudo is a Unix command that is not available on Windows.

To solve the error, run the command without the sudo prefix or run CMD as an administrator before issuing the command.

sudo is not recognized as internal or external command

sudo is a Unix-specific command that allows you to elevate your permissions.

The command is not available on Windows, but you can try to rerun the command without the sudo prefix.

cmd
# 👇️ your command without sudo dir

If the command you're trying to run requires you to have administrative privileges, then start CMD as an administrator.

To open CMD as an administrator:

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Issue your command.
cmd
dir

administrative command prompt

Alternatively, you can open PowerShell as an administrator.

To run PowerShell as an administrator:

  1. Click on the Search bar and type "PowerShell".
  2. Right-click on the "PowerShell" application and click "Run as administrator".

run powershell as administrator

PowerShell
dir

administrator powershell

You can also run Git Bash as an administrator if you have git installed.

To run Git Bash as an administrator:

  1. Click on the Search bar and type "Git Bash".
  2. Right-click on the "Git Bash" application and click "Run as administrator".

run git bash as administrator

GitBash
dir

administrative git bash

An alternative approach is to install the Windows Subsystem for Linux.

The Windows Subsystem for Linux (WSL) allows you to install a Linux distribution (e.g. Ubuntu) and use Linux applications, utilities and the Bash shell directly on Windows.

If you start a Bash shell, you can use the sudo command.

# Installing Windows subsystem for Linux

To install Windows subsystem for Linux (WSL):

  1. Click on the Search bar and type CMD.

  2. Right-click on the Command Prompt application and click "Run as administrator".

run cmd as administrator

  1. Issue the wsl --install -d Ubuntu command.
cmd
wsl --install -d Ubuntu

wsl install ubuntu

  1. You can use the wsl --list --online command if you want to list all valid distributions that can be installed.
cmd
wsl --list --online # 👇️ the syntax for the installation command wsl --install -d DISTRO_NAME

wsl list online

  1. If you get an error that the wsl command is not recognized, use the older lxrun command instead.
cmd
lxrun /install
  1. You will be prompted to enter a new UNIX username and Password. Make sure to remember your credentials.

create default unix account

  1. Wait for the installation to complete.

wait for installation to complete

  1. Close your CMD and PowerShell sessions and reopen your shell as an administrator.

  2. Now you can simply type bash in CMD to start a bash shell.

cmd
bash sudo apt update ls

start bash shell

Make sure to use the bash command to enter a bash session before issuing Unix commands.
  1. You can use the sudo prefix if you have to issue a command as an administrator.
cmd
sudo apt update && sudo apt upgrade

sudo for admin permissions

  1. You can click on the Search bar and type "Ubuntu" to start your integrated Linux environment.

windows start ubuntu

  1. If the error persists, you have to restart your PC.

Alternatively, you can install your Linux distribution of choice using Microsoft Store.

# Install a Linux distribution using Microsoft Store

To install a Linux distribution using Microsoft Store:

  1. Click on the https://aka.ms/wslstore link.

  2. In the Microsoft Store window, use the Search bar to look for your Linux distribution of choice. For example, Ubuntu 22.04.1 LTS.

search for linux distribution

  1. Click on your preferred Linux distribution and use the "Get" button to install it.

click get button

  1. Wait for the installation to complete.

wait for linux installation to complete

  1. Once the installation is complete, click on the "Open" button.

click open button

  1. A console window will launch and you will be prompted for a username and a password. Make sure to remember your credentials.

enter your credentials

After the process is complete, you can click on the Search bar and type "Ubuntu" to start your integrated Linux environment.

windows start ubuntu

Now you can simply type bash in CMD to start a bash shell.

cmd
bash sudo apt update ls

start bash shell

Make sure to use the bash command to enter a bash session before issuing Unix commands.

You can use the sudo prefix if you have to issue a command as an administrator.

cmd
sudo apt update && sudo apt upgrade

sudo for admin permissions

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.