OpenTofu: installing OpenTofu on Debian/Ubuntu

Terraform has now been open-source and forked with the OpenTofu project.  The ‘tofu’ binary is a drop-in replacement for terraform, and this article will show you how to install on Debian/Ubuntu.

After installation, we will then use the Debian/Ubuntu Alternatives concept to supersede existing calls to ‘terraform’ to instead invoke ‘tofu’.

Setup OpenTofu apt repository

As described in the official docs, first add the OpenTofu GPG key

sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://get.opentofu.org/opentofu.gpg | sudo tee /etc/apt/keyrings/opentofu.gpg >/dev/null
curl -fsSL https://packages.opentofu.org/opentofu/tofu/gpgkey | sudo gpg --no-tty --batch --dearmor -o /etc/apt/keyrings/opentofu-repo.gpg >/dev/null
sudo chmod a+r /etc/apt/keyrings/opentofu.gpg /etc/apt/keyrings/opentofu-repo.gpg

Then add OpenTofu to the apt source list, referencing the gpg key just downloaded.

echo \
"deb [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main
deb-src [signed-by=/etc/apt/keyrings/opentofu.gpg,/etc/apt/keyrings/opentofu-repo.gpg] https://packages.opentofu.org/opentofu/tofu/any/ any main" | \
sudo tee /etc/apt/sources.list.d/opentofu.list > /dev/null
sudo chmod a+r /etc/apt/sources.list.d/opentofu.list

Install OpenTofu

Now you can install the ‘tofu’ apt package, just like any other apt package.

sudo apt-get update
sudo apt-get install -y tofu

Validate installation

$ which tofu
/usr/bin/tofu

$ tofu --version

Create Debian/Ubuntu alternative to supersede terraform binary

In order to route any invocation of ‘terraform’ to use ‘tofu’ instead, we can use Debian/Ubuntu Alternatives.

# are any alternatives for 'terraform' already defined?
sudo update-alternatives --query terraform

# if regular file exists at alternative location, then delete it
[ -L /usr/local/bin/terraform ] || { echo "deleting regular file to make room for alternative";sudo rm /usr/local/bin/terraform; }

# set high priority 80 to invoking tofu
sudo update-alternatives --install /usr/local/bin/terraform terraform /usr/bin/tofu 80

# now call to 'terraform' should invoke opentofu 'tofu'
$ terraform --version
OpenTofu v1.8.1
on linux_amd64
+ provider registry.opentofu.org/hashicorp/local v2.5.1

 

REFERENCES

OpenTofu home

Setup OpenTofu apt repository for Ubuntu