Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

apt-get does not exist on this EC2 instance

Errorbash: apt-get: command not found

Short 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.

  • Applies to Amazon Linux 2023, Amazon Linux 2
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-release
  2. 2Match what you see, then jump to its fix

    1. 1NAME="Amazon Linux" and VERSION_ID="2023" in /etc/os-releaseThe AMI is Amazon Linux 2023, which uses dnfFix
    2. 2NAME="Amazon Linux" and VERSION_ID="2"The AMI is Amazon Linux 2, which uses yumFix

What does the error look like?

  • bash: apt-get: command not found
  • sudo: apt-get: command not found
  • sudo: 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 dnf

2Fix

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 packagename

3Verify

The package manager lists the package as installed.

dnf list installed packagename

Confirmed by docs.aws.amazon.com

Did this fix it?

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-release

2Fix

Use yum for install, search, remove and update.

sudo yum update
sudo yum install packagename
sudo yum search packagename

3Verify

yum reports the package under installed packages.

yum list installed packagename

Confirmed by docs.aws.amazon.com

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.