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.
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:
With the helpers installed, you can do:
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:
Then clone the project and create symlinks into a local bin directory:
Add that directory to your shell path if needed:
Now verify the commands exist:
If they print context or namespace information instead of "command not found", the install is working.
Using the Tools After Installation
List contexts:
Switch to another context:
List namespaces in the current context:
Switch namespace:
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:
To remove:
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/binto the shell config but not reloading the shell. - Assuming
kubectxis broken when the real problem is an empty or misconfigured kubeconfig file. - Installing only
kubectxand then expectingkubensto 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
kubectxandkubenstogether for the best workflow. - Make sure the symlink directory is on your
PATH. - Verify the tool with a simple
kubectxcommand after installation. - Keeping the install in your home directory makes updates and removal straightforward.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.