# Installing and Configuring Terraform for Your Environment

As you embark on your journey into Infrastructure as Code (IaC) with Terraform, the first crucial step is setting up Terraform on your local machine. In this guide, we'll walk through the installation process and initial configuration, ensuring you're ready to start defining and managing your infrastructure as code.

"terraform" installation done just by **dropping and playing** the terraform binary or executable

## **Step 1: Downloading Terraform**

Terraform is distributed as a standalone binary, making installation straightforward. Follow these steps to download Terraform:

1. Visit the official Terraform website: [Terraform Downloads](https://www.terraform.io/downloads.html).
    
2. Identify the appropriate version for your operating system (Windows, macOS, or Linux).
    
3. Download the binary and save it to a directory on your machine.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699980254735/bf55c353-6407-4f5e-893e-404e1e30aa05.png align="center")

## **Step 2: Installing Terraform**

### **Windows:**

1. Extract the downloaded zip file.
    
2. Move the`terraform.exe` binary to a directory included in your system's PATH.
    
3. Open a command prompt and verify the installation by typing:
    

```xml
terraform -version
```

### **macOS:**

1. Extract the downloaded archive.
    
2. Move the`terraform` binary to a directory in your PATH (e.g., `/usr/local/bin/`).
    
3. Open a terminal and verify the installation.
    

```xml
terraform -version
```

### **AWS Linux:**

1. Extract the downloaded archive.
    
2. Move the`terraform` binary to a directory in your PATH (e.g., `/usr/bin/terraform`).
    
3. Open a terminal and verify the installation.
    

```xml
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700726439863/e8477931-853e-493c-b85f-be0be8ed7538.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700726485559/119829cf-a345-40db-8136-97dd965319c2.png align="center")

```xml
terraform -version
```

If the installation was successful, you should see Terraform's version information, indicating that it's ready for use.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699980352670/b94ce0b9-4670-49af-8cd9-08da2b8da0d1.png align="center")

## **Step 3: Configuring Terraform**

Before you start using Terraform, it's essential to configure it with your preferred settings and credentials. Follow these steps to configure Terraform:

1. **Create a working directory.**
    
    Organize your Terraform projects by creating a dedicated directory for each. Navigate to your desired workspace.
    
    ```xml
    mkdir my_terraform_project
    cd my_terraform_project
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699980428825/d5fa8f2e-063c-41ee-848c-6a02f4efc37f.png align="center")
    
2. **Initialize Terraform:**
    
    Run the following command to initialize Terraform in your project directory:
    
    ```xml
    terraform init
    ```
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1699980517620/1fad3867-2ce0-4934-8e4c-083c15276fc0.png align="center")
    
    This command downloads the necessary provider plugins and sets up your Terraform environment.
    
3. **Configure provider credentials:**
    
    If you're working with a cloud provider (e.g., AWS, Azure), you'll need to configure your provider credentials. Use the command "`aws configure`" for configuring AWS credentials:
    
    ```xml
    aws_access_key = "your_access_key"
    aws_secret_key = "your_secret_key"
    ```
    
    Make sure to replace `"your_access_key"` and `"your_secret_key"` with your actual credentials.
    
4. **(Optional) Configure additional settings:**
    
    Explore advanced configurations in the `terraform` block in your [`main.tf`](http://main.tf) file. For example, set a preferred backend for storing Terraform state remotely.
    
    ```xml
    main.tf
    
    terraform {
      backend "s3" {
        bucket = "my-terraform-state-bucket"
        key    = "terraform.tfstate"
        region = "us-west-2"
      }
    }
    ```
    
    Modify these settings according to your project requirements.
    

## Running Multiple versions of terraform on same machine

By default, we run a single Terraform version on our machine. But we can make use of multiple versions of Terraform on the same machine. By copying various terraform versions inside, `/usr/bin/terraform*` it will look like this:

`/usr/bin/terraform_122 #rename terraform 1.2.2 binary`

`/usr/bin/terraform_157 #rename terraform 1.5.7 binary`

Configure default version by creating symbolic link as:

`/usr/bin/terraform -> /usr/bin/terraform_122`

If I want to use version 1.5.7 for my work, I can explicitly use the corresponding version as follows:

`terraform_157 --version`

`terraform_157 init`

`terraform_157 plan`

If I want to use version 1.2.2 for my work, I can explicitly use the corresponding version as follows:

`terraform_122 --version`

As we can see, Terraform is symlinked to`/usr/bin/terraform_122`, the default version is set to 1.2.2 on your system, and it can be used as usual, i.e.

`terraform --version`

## **Conclusion:**

Congratulations! You've successfully installed and configured Terraform for your environment. With Terraform initialized and your credentials set up, you're ready to start defining infrastructure as code. In the next blog post, we'll explore the basics of creating your first Terraform configuration and provisioning resources. Stay tuned for an exciting journey into the world of infrastructure as code with Terraform!
