简单介绍如何在Linux上使用Go二进制包安装Go。
环境说明
- 操作系统:Debian 13 Trixie (Linux)
- 架构:amd64 (x86_64)
- 安装版本:Go 1.24.2
安装步骤
1. 下载 Go 二进制包
bash
1
wget https://go.dev/dl/go1.24.2.linux-amd64.tar.gz -O /tmp/go1.24.2.tar.gz
或使用 curl:
bash
1
curl -L -o /tmp/go1.24.2.tar.gz https://go.dev/dl/go1.24.2.linux-amd64.tar.gz
2. 安装 Go
bash
12345678
# 如果之前已安装 Go,需先删除旧版本
rm -rf /usr/local/go
# 解压到 /usr/local 目录
tar -C /usr/local -xzf /tmp/go1.24.2.tar.gz
# 删除下载包
rm /tmp/go1.24.2.tar.gz
3. 添加环境变量
将 Go 添加到 PATH 编辑 ~/.bashrc 或 ~/.profile:
bash
12
echo 'export PATH=$PATH:/usr/local/go/bin' >> ~/.bashrc
source ~/.bashrc
4. 换源(可选,国内加速)
编辑 ~/.profile 添加:
bash
12
export GO111MODULE=on
export GOPROXY=https://goproxy.cn
然后执行:
bash
1
source ~/.profile
验证安装
1. 验证 Go 版本
bash
1
go version
输出:
text
1
go version go1.24.2 linux/amd64
2. 验证环境变量
bash
1
go env GOPROXY
输出:
text
1
https://goproxy.cn
3. 测试下载 Go 包
bash
1234
mkdir -p ~/test_go
cd ~/test_go
go mod init test
go get github.com/gin-gonic/gin@latest
下载成功说明代理正常工作。
4. 清理测试文件
bash
12
cd ~
rm -rf ~/test_go
常用命令
| 命令 | 说明 |
|---|---|
go version |
查看 Go 版本 |
go env |
查看 Go 环境变量 |
go mod init <module> |
初始化模块 |
go get <package> |
下载依赖包 |
卸载 Go
bash
1
rm -rf /usr/local/go
同时删除环境变量配置(~/.bashrc 或 ~/.profile 中添加的行)。