Skip to content
DBDeependra Bhatta~/notes
Infrastructure as Code#opentofu · #linux · #terraform

OpenTofu: Why It Exists, Installation and Migrating from Terraform

Why OpenTofu was forked from Terraform, how to install it on Ubuntu, and how to migrate an existing Terraform project with tofu init, including the lock file warning.

· updated · 5 min read
ON THIS PAGE

OpenTofu is an open-source fork of Terraform, run by the community under the Linux Foundation. It uses the same HCL language, providers and state format, so existing Terraform projects run on it, usually without code changes. This guide explains why the fork happened, installs OpenTofu on Ubuntu, and migrates one of the AWS projects from part 1 of this series.

Why OpenTofu exists

Terraform was open source under the Mozilla Public License (MPL 2.0) from 2014, and a large ecosystem of providers, modules and tools grew around it. In August 2023, HashiCorp moved Terraform to the Business Source License (BUSL). The BUSL is source-available, not open source: it restricts use in products that compete with HashiCorp, and the wording left many companies unsure whether their own use was allowed.

The community responded with the OpenTF manifesto. It asked HashiCorp to return Terraform to an open-source license, and said the community would fork it otherwise. HashiCorp did not change course, so the last MPL version was forked. The fork was named OpenTofu and placed under the Linux Foundation, so no single company controls it.

The manifesto's promises for the project:

  • Truly open source, under a well-known license that will not change suddenly. OpenTofu stays on MPL 2.0.
  • Community-driven, with contributions accepted on their merit.
  • Impartial, with features prioritized for users, not for one vendor.
  • Layered and modular, so other tools can build on it.
  • Backward-compatible, so existing Terraform code keeps working.

Since the fork, OpenTofu has also added features of its own, such as client-side state encryption. The workflow is the same as Terraform: write, tofu plan, tofu apply. The CLI command is tofu instead of terraform.

Prerequisites

  • An Ubuntu or Debian machine with sudo access (the screenshots use an Ubuntu Vagrant VM).
  • An existing Terraform project to migrate, such as the EC2 project from part 1.

Install OpenTofu

Option 1: the installer script

The standalone installation guide uses an installer script. Copy the commands from the official page, because they may change:

terminal
$ curl --proto '=https' --tlsv1.2 -fsSL https://get.opentofu.org/install-opentofu.sh -o install-opentofu.sh
$ chmod +x install-opentofu.sh
$ less install-opentofu.sh
$ ./install-opentofu.sh --install-method standalone
$ rm -f install-opentofu.sh

The less step is deliberate: always read a downloaded script before running it.

Option 2: the APT repository on Debian or Ubuntu

The Debian/Ubuntu guide adds the OpenTofu package repository, so apt upgrade keeps tofu up to date.

Install the tools needed to add a repository:

terminal
$ sudo apt-get update
$ sudo apt-get install -y apt-transport-https ca-certificates curl gnupg

Download the signing keys into /etc/apt/keyrings:

terminal
$ 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

Add the repository, limited to those keys with signed-by:

SHBash
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:

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

Check the installation

terminal
$ tofu --version

tofu --version showing OpenTofu v1.10.5 in the Vagrant VM, and terraform --version showing v1.13.1 on the host

In my lab, the Ubuntu VM ran OpenTofu 1.10.5 and the host ran Terraform 1.13.1. The two tools can coexist on one machine because their commands have different names.

Migrate a Terraform project to OpenTofu

The migration guide describes a process that is safe and reversible:

  1. Back up the state and the code. For a local backend, copy terraform.tfstate and terraform.tfstate.backup. For a remote backend, follow that backend's backup method (for S3, bucket versioning helps).
  2. Install OpenTofu and check it with tofu --version.
  3. Initialize with tofu init.
  4. Verify with tofu plan. It should report no changes, or only changes you expected.
  5. Apply once with tofu apply, which may update the state file format.
  6. Test with a small change, for example a new tag, then tofu plan and tofu apply.

If the migration fails, restore the backups and run terraform init and terraform plan to return to Terraform.

I ran the migration on a copy of my first EC2 project (example1-backup) so the original stayed untouched:

terminal
$ cd ~/shared_folder/terraform-examples/example1-backup
$ tofu init

tofu init in a copy of a Terraform project, with a warning that lock file entries were rewritten to registry.opentofu.org

The lock file warning

tofu init printed Warning: Dependency lock file entries automatically updated. This warning is expected during a migration:

  • OpenTofu downloads providers from its own registry, registry.opentofu.org, so it rewrote the lock file entry from registry.terraform.io/hashicorp/aws to registry.opentofu.org/hashicorp/aws.
  • The provider version (6.11.0) was kept. The hashes were not, because the OpenTofu registry's provider builds are not byte-for-byte identical to HashiCorp's.

Review the updated .terraform.lock.hcl and commit it once you decide to stay on OpenTofu. After that, everything from part 1, such as variables, backends, workspaces and modules, works the same with tofu in place of terraform.

Key takeaways

  • OpenTofu is the MPL-licensed fork of Terraform, created after HashiCorp moved Terraform to the BUSL in August 2023.
  • It is a drop-in replacement: same HCL, same providers, same state; the command is tofu.
  • Install it with the official installer script or the APT repository, and check with tofu --version.
  • Migrate on a backup: tofu init, confirm tofu plan shows no changes, then test with a small change.
  • The lock file warning during tofu init is expected; versions stay the same, the registry and hashes change.