How to Install Kubectl on Ubuntu 22.04? (Step-by-Step Kubectl Installation Guide)

Published on September 10, 2024, Updated on December 17, 2024

Kubectl is a command-line interface (CLI) tool designed to manage Kubernetes clusters efficiently. It helps manage and control various Kubernetes resources. With Kubectl, users can deploy applications, inspect and modify cluster resources, scale deployments, and troubleshoot issues that may arise. Kubectl acts as a powerful bridge between users and their Kubernetes clusters, simplifying the process of issuing commands and retrieving information.

If you don’t know how to install Kubectl on Ubuntu 22.04, this Kubetcl installation guide will provide step-by-step instructions. By following this guide, you'll be able to successfully install Kubectl on your Ubuntu 22.04 system, enabling you to interact with Kubernetes clusters and execute commands effectively.

KUBECTL INSTALLATION ON UBUNTU 22.04

You can install Kubectl on Ubuntu 22.04 using three different methods that we will explain in this guide:

METHOD 01: INSTALL KUBECTL ON UBUNTU USING APT PACKAGE MANAGER

One method to install Kubectl on Ubuntu 22.04 is using the apt package manager. This method allows you to easily update and manage the Kubectl packages along with all their dependencies.

STEP 1: ADDING KUBERNETES REPOSITORY'S GPG KEY

First, download Kubectl’s GPG key for the Kubernetes repository and add it to your system. Execute the following command in your terminal:

$ curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg

This command downloads the GPG key, converts it to a binary format, and saves it as a keyring file. The keyring file is important for verifying the authenticity of packages from the Kubernetes repository.

STEP 2: ADDING THE KUBERNETES REPOSITORY TO SYSTEM SOURCES

Create a repository entry for Kubernetes packages to let apt fetch and install them. Execute the following command in your terminal:

$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

STEP 3: UPDATING SYSTEM PACKAGES

After adding the repository, update the system's package list using the following command:

$ sudo apt update

STEP 4: KUBECTL INSTALLATION USING APT

Finally, install Kubectl on Ubuntu 22.04 using the apt package manager with the following command:

$ sudo apt install kubectl

This command installs Kubectl along with its dependencies, making it ready for use on your Ubuntu 22.04 system.

STEP 5: VERIFY INSTALLATION

Confirm the successful installation of Kubectl by running the following command:

$ kubectl version --output=yaml

This command will display details about the installed version of Kubectl in YAML format, confirming that it's properly installed and available for use.

METHOD 02: INSTALL KUBECTL BINARY USING CURL 

This method will download Kubectl’s binary manually from the official Kubernetes repository using the curl command. It's a suitable option if you have specific preferences for the Kubectl version you want to use, or if your system lacks traditional package managers.

STEP 1: DOWNLOAD KUBECTL BINARY

To start Kubectl installation on Ubuntu, first, you will download Kubectl’s binary from the official Kubernetes release repository. Open your terminal and run the following command:

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

The above command will download Kubectl's latest stable binary on your system. If you want a specific Kubectl version, you can adjust the URL accordingly.

STEP 2: SET EXECUTION PERMISSIONS FOR THE KUBECTL BINARY

Once the binary is downloaded, make it executable by using the following command:

$ chmod +x kubectl

STEP 3: MOVE THE KUBECTL BINARY TO A DIRECTORY IN THE SYSTEM'S PATH

To make Kubectl accessible from any location in the terminal, move the binary to a directory listed in the system's PATH environment variable. Execute the following command:

$ sudo mv kubectl /usr/local/bin

The above command moves the Kubectl binary to the /usr/local/bin directory, which is commonly included in the PATH.

METHOD 03: INSTALL KUBECTL ON UBUNTU USING SNAP PACKAGE MANAGER

An alternative method for Kubectl installation on Ubuntu 22.04 is using the Snap package manager. This is quite a simple and easy process to install Kubectl on your Ubuntu system. Perform the following two steps to install Kubectl on Ubuntu 22.04 using Snap:

STEP 1: UPDATE SYSTEM PACKAGES

First, ensure your system packages are up to date by running the following command:

$ sudo apt update

STEP 2: INSTALL KUBECTL WITH SNAP

Use the Snap package manager for Kubectl installation on the Ubuntu 22.04 system by executing this command:

$ sudo snap install kubectl --classic

CONCLUSION

In this article, we demonstrated how to install Kubectl on the Ubuntu 22.04 Linux distribution. We also showed you how you can download Kubectl’s binary using the Curl command and install it on your system. If you are using a Linux VPS server, using the above steps, you can easily install Kubectl on your server. To explore Kubernetes (Kubectl commands) more, you can visit its official documentation.