Install and Set Up kubectl on Linux

Before you begin

You must use a kubectl version that is within one minor version difference of your cluster. For example, a v1.28 client can communicate with v1.27, v1.28, and v1.29 control planes. Using the latest compatible version of kubectl helps avoid unforeseen issues.

Install kubectl on Linux

The following methods exist for installing kubectl on Linux:

Install kubectl binary with curl on Linux

  1. Download the latest release with the command:

    1. curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    1. curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl"

    Note:

    To download a specific version, replace the $(curl -L -s https://dl.k8s.io/release/stable.txt) portion of the command with the specific version.

    For example, to download version 1.28.0 on Linux x86-64, type:

    1. curl -LO https://dl.k8s.io/release/v1.28.0/bin/linux/amd64/kubectl

    And for Linux ARM64, type:

    1. curl -LO https://dl.k8s.io/release/v1.28.0/bin/linux/arm64/kubectl
  2. Validate the binary (optional)

    Download the kubectl checksum file:

    1. curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl.sha256"
    1. curl -LO "https://dl.k8s.io/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/arm64/kubectl.sha256"

    Validate the kubectl binary against the checksum file:

    1. echo "$(cat kubectl.sha256) kubectl" | sha256sum --check

    If valid, the output is:

    1. kubectl: OK

    If the check fails, sha256 exits with nonzero status and prints output similar to:

    1. kubectl: FAILED
    2. sha256sum: WARNING: 1 computed checksum did NOT match

    Note: Download the same version of the binary and checksum.

  3. Install kubectl

    1. sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

    Note:

    If you do not have root access on the target system, you can still install kubectl to the ~/.local/bin directory:

    1. chmod +x kubectl
    2. mkdir -p ~/.local/bin
    3. mv ./kubectl ~/.local/bin/kubectl
    4. # and then append (or prepend) ~/.local/bin to $PATH
  4. Test to ensure the version you installed is up-to-date:

    1. kubectl version --client

    Or use this for detailed view of version:

    1. kubectl version --client --output=yaml

Install using native package management

  1. Update the apt package index and install packages needed to use the Kubernetes apt repository:

    1. sudo apt-get update
    2. # apt-transport-https may be a dummy package; if so, you can skip that package
    3. sudo apt-get install -y apt-transport-https ca-certificates curl
  2. 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:

    1. curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
  3. Add the appropriate Kubernetes apt repository. If you want to use Kubernetes version different than v1.28, replace v1.28 with the desired minor version in the command below:

    1. # This overwrites any existing configuration in /etc/apt/sources.list.d/kubernetes.list
    2. echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

Note: To upgrade kubectl to another minor release, you’ll need to bump the version in /etc/apt/sources.list.d/kubernetes.list before running apt-get update and apt-get upgrade. This procedure is described in more detail in Changing The Kubernetes Package Repository.

  1. Update apt package index, then install kubectl:

    1. sudo apt-get update
    2. sudo apt-get install -y kubectl

Note: In releases older than Debian 12 and Ubuntu 22.04, /etc/apt/keyrings does not exist by default, and can be created using sudo mkdir -m 755 /etc/apt/keyrings

  1. Add the Kubernetes yum repository. If you want to use Kubernetes version different than v1.28, replace v1.28 with the desired minor version in the command below.
  1. # This overwrites any existing configuration in /etc/yum.repos.d/kubernetes.repo
  2. cat <<EOF | sudo tee /etc/yum.repos.d/kubernetes.repo
  3. [kubernetes]
  4. name=Kubernetes
  5. baseurl=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/
  6. enabled=1
  7. gpgcheck=1
  8. gpgkey=https://pkgs.k8s.io/core:/stable:/v1.28/rpm/repodata/repomd.xml.key
  9. EOF

Note: To upgrade kubectl to another minor release, you’ll need to bump the version in /etc/yum.repos.d/kubernetes.repo before running yum update. This procedure is described in more detail in Changing The Kubernetes Package Repository.

  1. Install kubectl using yum:
  1. sudo yum install -y kubectl