kubectx
Ubuntu Linux
installation guide
Ubuntu 20.04
Linux commands

How can I install kubectx on Ubuntu Linux 20.04?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

kubectx is a small command-line utility that makes switching Kubernetes contexts much faster than using raw kubectl config commands every time. On Ubuntu 20.04, the most reliable installation path is a direct clone plus a symlink into a directory on your PATH. That approach works even when package availability varies between machines.

What kubectx and kubens Do

kubectx switches Kubernetes contexts, while kubens switches namespaces. They are often installed together because they solve the same daily workflow problem.

Without them, switching looks like this:

bash
kubectl config use-context my-cluster
kubectl config set-context --current --namespace=payments

With the helpers installed, you can do:

bash
kubectx my-cluster
kubens payments

The benefit is speed and fewer typing mistakes, especially when you work with multiple clusters.

Install From the Repository on Ubuntu 20.04

Make sure you have Git available:

bash
sudo apt update
sudo apt install -y git

Then clone the project and create symlinks into a local bin directory:

bash
1git clone https://github.com/ahmetb/kubectx.git "$HOME/.kubectx"
2mkdir -p "$HOME/.local/bin"
3
4ln -sf "$HOME/.kubectx/kubectx" "$HOME/.local/bin/kubectx"
5ln -sf "$HOME/.kubectx/kubens" "$HOME/.local/bin/kubens"

Add that directory to your shell path if needed:

bash
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.zshrc
source ~/.zshrc

Now verify the commands exist:

bash
kubectx
kubens

If they print context or namespace information instead of "command not found", the install is working.

Using the Tools After Installation

List contexts:

bash
kubectx

Switch to another context:

bash
kubectx staging-cluster

List namespaces in the current context:

bash
kubens

Switch namespace:

bash
kubens kube-system

These commands are thin wrappers around kubeconfig operations, so they use the same underlying configuration file as kubectl.

Optional Shell Completion

Tab completion is worth enabling because the whole point of kubectx is fast interactive use.

If your shell setup supports completion scripts, you can source the provided files from the cloned repository. For zsh, a lightweight approach is to add their completion directory to your shell startup setup or use your existing completion framework if you already have one.

If you skip completion, the tool still works fine. It is only a usability improvement, not a requirement.

Why a Manual Install Is Often Better

Ubuntu package sources can vary, and some machines may not have a fresh package for kubectx. Cloning the repository keeps the install method simple and explicit:

  • easy to inspect
  • easy to update with git pull
  • easy to remove by deleting the directory and symlinks

For command-line utilities of this size, that is often more maintainable than hunting for a distro-specific package.

Updating or Removing It Later

To update:

bash
git -C "$HOME/.kubectx" pull

To remove:

bash
rm -f "$HOME/.local/bin/kubectx" "$HOME/.local/bin/kubens"
rm -rf "$HOME/.kubectx"

That is another advantage of keeping the install local to your home directory.

Common Pitfalls

  • Cloning the repository but forgetting to symlink the executables into a directory on PATH.
  • Adding ~/.local/bin to the shell config but not reloading the shell.
  • Assuming kubectx is broken when the real problem is an empty or misconfigured kubeconfig file.
  • Installing only kubectx and then expecting kubens to exist too.
  • Using root-owned install paths unnecessarily for a user-level CLI tool.

Summary

  • On Ubuntu 20.04, a Git clone plus local symlink is a reliable way to install kubectx.
  • Install kubectx and kubens together for the best workflow.
  • Make sure the symlink directory is on your PATH.
  • Verify the tool with a simple kubectx command after installation.
  • Keeping the install in your home directory makes updates and removal straightforward.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions