Home > Backend Development > Golang > Why Does \'sudo go run main.go\' Fail with \'exec: go: executable file not found in $PATH\' on Ubuntu 16.04?

Why Does \'sudo go run main.go\' Fail with \'exec: go: executable file not found in $PATH\' on Ubuntu 16.04?

Susan Sarandon
Release: 2024-11-17 06:54:03
Original
802 people have browsed it

Why Does

Troubleshooting "sudo go run main.go" Error

When attempting to execute "sudo go run main.go" to capture network packets using "gopacket" on Ubuntu 16.04, you may encounter the error message "exec: go: executable file not found in $PATH." This error arises because the necessary environment variables are not set for the root user.

Solution:

Instead of using "sudo go run ...," follow these steps:

1. Compile the Binary without sudo:

  • Use "go build" to compile the main.go file into an executable binary. For example:

    go build
    Copy after login

2. Run the Binary with sudo:

  • Execute the compiled binary with sudo. For instance:

    sudo ./mycapt
    Copy after login

Alternatively:

  • Use "go install" to install the binary into your system's $GOPATH/bin directory.

    go install
    Copy after login
  • Then run the binary with sudo from the $GOPATH/bin directory:

    sudo $GOPATH/bin/mycapt
    Copy after login

By compiling and running the binary separately, you ensure that the necessary environment variables are available to the root user.

The above is the detailed content of Why Does \'sudo go run main.go\' Fail with \'exec: go: executable file not found in $PATH\' on Ubuntu 16.04?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template