Install kubectl on Ubuntu 24.04 with bash autocomplete

Using PPA is not the best solution, but it is easy and fast. Official documentation here

Update packages

sudo apt-get update

Download the public signing key for the Kubernetes package repositories. The same signing key is used for all repositories so you can disregard the version in the URL

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.35/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Allow unprivileged APT programs to read this keyring

sudo chmod 644 /etc/apt/keyrings/kubernetes-apt-keyring.gpg

Add the appropriate Kubernetes apt repository. If you want to use Kubernetes version different than v1.35, replace v1.35 with the desired minor version in the command below.This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.35/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Helps tools such as command-not-found to work correctly

sudo chmod 644 /etc/apt/sources.list.d/kubernetes.list

Install

sudo apt-get update
sudo apt-get install -y kubectl

Enable bash completion, cause I want autocomplete with TAB when searching pods, deployments, nodes, etc.

sudo apt install -y bash-completion
kubectl completion bash | sudo tee /etc/bash_completion.d/kubectl > /dev/null

Connect to your cluster

mkdir ~/.kube
## copy your config in  ~/.kube

Use different configs with adding this in ~/.bashrc

export KUBECONFIG="${KUBECONFIG}:${HOME}/.kube/civo-civo-ftw-kubeconfig"
alias use-civo='export KUBECONFIG=~/.kube/civo-civo-ftw-kubeconfig'
alias use-proxmox='export KUBECONFIG=~/.kube/proxmox-config'

Reload shell or log out and log in again

source ~/.bashrc

Leave a Reply

Your email address will not be published. Required fields are marked *