What the 'kubectl: command not found' Error Means
The 'kubectl: command not found' (or 'bash: kubectl: command not found') error appears in a Linux terminal when the system cannot find the kubectl executable file in the directories listed in the PATH environment variable. This means that either kubectl is not installed on your system, it is installed but its path is not added to PATH, or the path is specified incorrectly.
The error is typically accompanied by exit code 127 (in bash), which indicates that the command was not found.
Possible Causes
- kubectl is not installed. You are trying to use
kubectlwithout having installed the Kubernetes client first. - kubectl is installed but not in PATH. You may have installed
kubectlin a custom directory (e.g.,~/binor/opt/kubectl), but that directory is not included in thePATHvariable. - Incorrect path in PATH. The
PATHcontains an incorrect path to thekubectlexecutable, for example due to a typo or a change in the file's location. - Shell issues. A shell is being used that does not load configuration files (e.g.,
~/.bashrc) wherekubectlwas added toPATH. - Corrupted installation.
kubectlis installed, but the executable file is missing or damaged.
Solutions
Method 1: Install kubectl
If kubectl is not installed, you need to install it. There are several installation methods depending on your Linux distribution.
For Ubuntu/Debian:
# Update package index
sudo apt-get update
# Install kubectl
sudo apt-get install kubectl
For CentOS/RHEL:
# Install kubectl via yum
sudo yum install kubectl
For Fedora:
# Install kubectl via dnf
sudo dnf install kubectl
Alternative installation via the official Kubernetes repository:
You can install kubectl directly from the Kubernetes website, which ensures you get the latest version:
# Download the latest stable version (example for amd64)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
# Add execute permissions
chmod +x kubectl
# Move to a directory in PATH (e.g., /usr/local/bin)
sudo mv kubectl /usr/local/bin/
# Verify installation
kubectl version --client
Method 2: Configure the PATH Variable
If kubectl is already installed but the command is not found, the path to the executable might not be added to PATH.
- Find the location of
kubectl:# Search in standard directories sudo find / -name kubectl 2>/dev/null # Or if you know the approximate path ls /usr/local/bin/kubectl - Check your current
PATH:echo $PATH
Ensure the directory containingkubectl(e.g.,/usr/local/bin) appears in the output. - If the directory is not in
PATH, add it: Open the~/.bashrcfile (for bash) or~/.zshrc(for zsh) in a text editor and add the line:export PATH=$PATH:/path/to/directory
For example, ifkubectlis located in/usr/local/bin(which is typical) and that directory is not inPATH, add:export PATH=$PATH:/usr/local/bin - Apply the changes:
source ~/.bashrc # for bash # or source ~/.zshrc # for zsh
Or simply restart the terminal. - Verify that
kubectlis now available:which kubectl kubectl version --client
Method 3: Create a Symbolic Link
If kubectl is installed in a non-standard directory and you do not want to modify PATH, you can create a symbolic link in a directory that is already in PATH (e.g., /usr/local/bin):
# Assuming kubectl is located at /opt/kubectl/bin/kubectl
sudo ln -s /opt/kubectl/bin/kubectl /usr/local/bin/kubectl
# Verification
kubectl version --client
Prevention
- Install
kubectlvia official repositories or package managers to automatically add it toPATH. - Check
PATHafter installation using theecho $PATHcommand to ensure the directory containingkubectlis included. - Use standard paths for installing binaries, such as
/usr/local/binor/usr/bin, which are typically already inPATH. - If installing manually, always add the path to
PATHin your shell configuration file (e.g.,~/.bashrc).