apt-get does not exist on this EC2 instance
bash: apt-get: command not foundShort answer
apt-get belongs to Debian and Ubuntu. Amazon Linux 2023 uses dnf, and Amazon Linux 2 uses yum, so the command is simply not installed. Read /etc/os-release to see which AMI you launched, then swap apt-get for dnf or yum. Or launch an Ubuntu AMI if the guide you follow needs apt.
Fix it fast
1Find out which cause you have
Ask the instance which distribution it runs. The NAME and ID lines decide which package tool applies.
cat /etc/os-release2Match what you see, then jump to its fix
What does the error look like?
bash: apt-get: command not foundsudo: apt-get: command not foundsudo: apt: command not found
Cause 1 of 2
The AMI is Amazon Linux 2023, which uses dnf
1Check
AL2023's default package manager is DNF. The yum command still exists but points to dnf.
grep -E '^(NAME|VERSION_ID)=' /etc/os-release
command -v dnf2Fix
Replace apt-get with dnf. Update is called upgrade; package names can differ from Ubuntu's.
sudo dnf upgrade -y
sudo dnf install -y packagename
sudo dnf search packagename3Verify
The package manager lists the package as installed.
dnf list installed packagenameConfirmed by docs.aws.amazon.com
Cause 2 of 2
The AMI is Amazon Linux 2, which uses yum
1Check
AL2 instances manage software with the yum package manager.
grep -E '^(NAME|VERSION_ID)=' /etc/os-release2Fix
Use yum for install, search, remove and update.
sudo yum update
sudo yum install packagename
sudo yum search packagename3Verify
yum reports the package under installed packages.
yum list installed packagenameConfirmed by docs.aws.amazon.com
How do I stop it coming back?
- Check /etc/os-release before copying install commands from a guide.
- Pick the AMI that matches the guide: Ubuntu for apt, Amazon Linux for dnf.
- Know that package names differ between distributions; search with dnf search before assuming.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Related fixes
Servers
apt-get is not found while building a Docker image
/bin/sh: apt-get: not found
apt-get belongs to Debian and Ubuntu.
2 causes, each with a fixServers
bash warns that it cannot change the locale after you log in to a server
bash: warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8)
The shell was told to use a locale that is not built on the server.
3 causes, each with a fixServers
Syntax error: redirection unexpected when a Bash script runs under sh
script.sh: 12: Syntax error: redirection unexpected
The operators <<< (here-string) and &> are Bash features.
3 causes, each with a fixServers
ping is missing inside a Docker container
sh: 1: ping: not found
Base images such as ubuntu are minimal, so ping is simply not installed.
2 causes, each with a fixServers
Running ps inside a container says command not found
bash: ps: command not found
Slim Debian and Ubuntu images do not ship the procps package, and ps comes from procps.
2 causes, each with a fixServers
GitHub Actions step fails writing to GITHUB_OUTPUT
Error: Invalid format '...'
The runner reads GITHUB_OUTPUT line by line, and every entry must be name=value or a name<<DELIMITER block.
3 causes, each with a fix