> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Compiling and installing the application

> Guide to compiling Go modules into executables and installing them so binaries can be run anywhere using go build, go install, GOPATH GOBIN and module-aware workflows.

In this lesson you'll learn how to compile a Go module into a native executable and how to install that executable so it can be run from anywhere on your system. This covers both local builds with `go build` and installing binaries with `go install` (module-aware and GOPATH workflows).

## 1) Build the binary in the module directory

From the module directory (the directory that contains your `go.mod` or package source), run:

```bash theme={null}
go build
```

This command compiles the package in the current directory and produces an executable file in the same directory.

* On Linux/macOS run the executable with `./<executable-name>`.
* On Windows run the `*.exe` file by name (for example `cryptit.exe`).

Example main.go (showing how the program might call your encrypt/decrypt packages):

```go theme={null}
package main

import (
    "fmt"

    "yourmodule/encrypt"
    "yourmodule/decrypt"
)

func main() {
    encryptedStr := encrypt.Nimbus("Kodekloud")
    fmt.Println(encryptedStr)
    fmt.Println(decrypt.Nimbus(encryptedStr))
}
```

Build and run locally:

```bash theme={null}
# build in the module directory
go build

# run the executable in the current directory (Linux/macOS)
./cryptit
```

Sample output:

```bash theme={null}
Nrghnorxg
Kodekloud
```

## 2) Running the executable from another directory

If your shell's current directory is not the one containing the executable, you must either change to that directory or provide the full path to the executable.

Example showing an attempt to run from the home directory without a path (fails), then running with a full path:

```bash theme={null}
# change to home
cd ~

# fails because ./cryptit refers to the home directory
./cryptit
# run using full path to the executable (example)
~/Desktop/kodekloud/cryptit/cryptit
# Nrghnorxg
# Kodekloud
```

## 3) Install the binary so it is runnable anywhere

Installing places the compiled executable into a directory that is typically on your `PATH`, such as `$GOPATH/bin` or a directory set by `GOBIN`. Use `go install` to compile and install.

1. Check `GOPATH` and `GOBIN`:

```bash theme={null}
go env GOPATH
go env GOBIN
```

2. If `GOBIN` is not set and you want to use `GOPATH/bin`, add it to your shell `PATH` (example for macOS/Linux):

```bash theme={null}
# show GOPATH
go env GOPATH
# add GOPATH/bin to PATH for the current shell session
export PATH=$PATH:/Users/priyanka/go/bin
```

3. From the module directory, install the package:

```bash theme={null}
# while still in the module directory
go install
```

`go install` compiles the package and places the resulting binary in `GOBIN` (if set) or `GOPATH/bin`.

Verify the binary is in the bin directory and run it from anywhere:

```bash theme={null}
# list the bin directory
ls ~/go/bin
# run the installed binary from home (no path needed)
cd ~
cryptit
# Nrghnorxg
# Kodekloud
```

## Quick reference: common commands

| Task | Command / Example |
| - | - |
| Build current directory | `go build` |
| Run local executable (Linux/macOS) | `./<executable-name>` |
| Install into `GOBIN` / `GOPATH/bin` | `go install` |
| Show GOPATH | `go env GOPATH` |
| Show GOBIN | `go env GOBIN` |
| Add GOPATH/bin to PATH (example) | `export PATH=$PATH:/Users/priyanka/go/bin` |

## 4) Module-aware installs and versioned installs

<Callout icon="lightbulb" color="#1CB2FE">
  If your project uses Go modules (has a `go.mod` file), `go install` without a version will compile and install the package in the current directory into `GOBIN` or `GOPATH/bin`. For installing a remote command at a specific version (or the latest), use the versioned form:

  `go install example.com/cmd@latest`

  Adjust your workflow based on your Go version and whether you rely on `GOPATH` or module-aware installs. See the official Go documentation on `go install` for more details: [https://golang.org/cmd/go/#hdr-Description\_of\_go\_install](https://golang.org/cmd/go/#hdr-Description_of_go_install).
</Callout>

## 5) Troubleshooting tips

* If `cryptit` is not recognized after installation, confirm the install directory is on your `PATH` (`echo $PATH`).
* If `go install` reports module or dependency errors, run `go mod tidy` in the module directory to ensure required dependencies are present.
* Use `which cryptit` or `command -v cryptit` to locate the installed binary.

That's it — you can now:

* build a local binary with `go build`,
* install it with `go install`,
* add the install directory to your `PATH`, and
* run the executable from any directory.

## Links and references

* [Go `go build` documentation](https://golang.org/cmd/go/#hdr-Compile_packages_and_dependencies)
* [Go `go install` documentation](https://golang.org/cmd/go/#hdr-Description_of_go_install)
* [Go modules overview](https://blog.golang.org/using-go-modules)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/advanced-golang/module/6ce7d245-515e-428d-9672-915e49a8ebd6/lesson/ab3c3730-e7c8-4cb6-90d6-ffefac74a7f1" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.