Three ways to install kubectl on Ubuntu 24.04 and 22.04: the apt repository (recommended), a direct download, or snap. Pick one. All three end with the same kubectl binary.
Method 1: APT Repository (Recommended)
# Add Kubernetes APT repository
sudo apt-get update && sudo apt-get install -y apt-transport-https ca-certificates curl gnupg
curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.31/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.31/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
# Install kubectl
sudo apt-get update
sudo apt-get install -y kubectlpkgs.k8s.io has one repository per Kubernetes minor version. Change v1.31 in both URLs to the minor version your cluster runs. On releases older than Ubuntu 22.04, /etc/apt/keyrings does not exist yet, so create it first with sudo mkdir -p -m 755 /etc/apt/keyrings.
Method 2: Direct Download
# Download the latest stable release (use arm64 instead of amd64 on ARM machines)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# Check the binary against its published checksum: expect "kubectl: OK"
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
echo "$(cat kubectl.sha256) kubectl" | sha256sum --check
# Install and clean up
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
rm kubectl kubectl.sha256Method 3: Snap
sudo snap install kubectl --classicVerify
kubectl version --client
kubectl cluster-info # If connected to a clusterMatch kubectl to Your Cluster
kubectl is supported within one minor version of your cluster’s API server. A v1.31 kubectl works with v1.30, v1.31 and v1.32 control planes. kubectl version prints both the client and the server version, so check it once you are connected.
Enable Shell Completion
# Bash (needs the bash-completion package)
sudo apt-get install -y bash-completion
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
source ~/.bashrc
# Zsh
echo 'source <(kubectl completion zsh)' >> ~/.zshrcUpgrade kubectl
# APT: latest patch release of your current minor version
sudo apt-get update && sudo apt-get install --only-upgrade kubectl
# Snap
sudo snap refresh kubectlTo move APT to a new minor version, change the version in /etc/apt/sources.list.d/kubernetes.list, then run the upgrade again. For a direct download, repeat Method 2.
Troubleshooting
The connection to the server localhost:8080 was refused: kubectl is installed but has no kubeconfig. Copy your cluster’s config to~/.kube/config, or point theKUBECONFIGvariable at it.E: Unable to locate package kubectl: the Kubernetes apt repository is missing, or you did not runsudo apt-get updateafter adding it.kubectl: command not foundafter a direct download: check that/usr/local/binis on yourPATHwithwhich kubectl.
Uninstall
sudo apt-get remove -y kubectl # APT
sudo snap remove kubectl # Snap
sudo rm /usr/local/bin/kubectl # Direct download