This content originally appeared on DEV Community and was authored by Nathan Ferguson
So I’m still getting back into things when it comes to CLI, and more importantly, terraform. I’m gonna break down the steps I was doing to get this working.
Previously I installed Terraform on my machine using Homebrew:
# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Install Terraform
brew install terraform
# Verify installation
terraform version
This worked fine, however later when I went to use the terraform
command, it gave me the dreaded:
command not found: terraform
.
Checking further, it seems that this is only stored in $PATH temporarily. I discovered this is what’s happening when you try to use a command like terraform
:
- Your shell looks at the PATH variable
- It searches each directory in PATH (from left to right)
- It looks for a file named terraform that’s executable
- Once found, it runs that program
- If not found in any PATH directory → “command not found” error
I then used the following to verify that terraform was indeed installed:
find / -name terraform 2>/dev/null
I was able to then verify it was at /opt/homebrew/bin/terraform
. The issue is that /opt/homebrew/bin isn’t in my PATH.
I needed to add this to PATH, which I did via this command:
echo 'export PATH="/opt/homebrew/bin:$PATH"' >> ~/.zshrc
I then tried to restart using
source ~/.zshrc
but I was getting another error! This time it was command not found: compdef
. After some additional research, I discovered this happens when there’s an issue with zsh completion system or when completion scripts are being loaded before zsh is fully initialized.
To fix, I ran these commands:
# Create a backup first
cp ~/.zshrc ~/.zshrc.backup
# Add the lines to the beginning
echo -e "autoload -Uz compinit\ncompinit\n$(cat ~/.zshrc)" > ~/.zshrc.tmp
mv ~/.zshrc.tmp ~/.zshrc
# Reload
source ~/.zshrc
I then was able to use source ~/.zshrc
which reloads the console.
After all this, since the terraform location it was added successfully to PATH, I was able to confirm terraform was working:
terraform version
Terraform v1.12.2
on darwin_arm64
Next steps are to start playing around with Terraform!
This content originally appeared on DEV Community and was authored by Nathan Ferguson