wsl2 ubuntu20.04安装gcc
时间: 2025-07-05 11:05:05 浏览: 19
### 安装和配置 GCC 编译器
在 WSL2 的 Ubuntu 20.04 系统中安装和配置 GCC 编译器是一个相对简单的过程。以下是详细的说明:
#### 更新系统包管理器
为了确保系统的软件包是最新的,在执行任何安装操作之前,建议先运行以下命令来更新本地的 APT 软件包索引:
```bash
sudo apt update && sudo apt upgrade -y
```
#### 安装 build-essential 工具集
`build-essential` 是一个元软件包,它包含了开发环境中常用的工具集合,其中包括 `gcc` 和 `g++` 编译器以及 `make` 构建工具等必要组件[^2]。可以通过以下命令安装该工具集:
```bash
sudo apt install build-essential -y
```
此命令将会自动拉取并安装 `gcc`、`g++` 及其他依赖项。
#### 验证 GCC 是否成功安装
安装完成后,可通过以下命令验证 GCC 是否已正确安装及其版本号:
```bash
gcc --version
```
如果显示了 GCC 版本信息,则表明安装成功[^4]。
#### 更改默认源以提高下载速度(可选)
对于网络条件较差的情况,可以考虑更改 Ubuntu 默认的软件源为国内镜像站点,从而加快软件包的下载速度。具体方法如下:
1. 打开 `/etc/apt/sources.list` 文件进行编辑:
```bash
sudo nano /etc/apt/sources.list
```
2. 将其中的内容替换为国内某大学或机构提供的镜像地址列表之一,例如清华大学开源软件镜像站提供的 URL 地址[^3]:
```
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ focal-security main restricted universe multiverse
```
3. 保存修改后的文件,并重新刷新APT缓存:
```bash
sudo apt update
```
通过以上步骤,可以在 WSL2 上顺利设置好 GCC 编译环境用于 C/C++ 开发工作流。
阅读全文
相关推荐


















