Ubuntu:面向开发者的一步步搭建指南
本文引导初学者准备一个干净的 Ubuntu 系统以进行开发工作。逐步执行命令,按需复制粘贴。必要时解释操作原因。
1. 更新系统并安装基础工具
Section titled “1. 更新系统并安装基础工具”打开终端并执行:
sudo apt updatesudo apt upgrade -ysudo apt install -y build-essential curl wget git ca-certificates gnupg lsb-releasebuild-essential含 GCC、make与必要头文件。gnupg与lsb-release用于添加第三方仓库时的签名与版本识别。
2. 创建开发用户与目录结构
Section titled “2. 创建开发用户与目录结构”如果使用安装时创建的用户,可跳过创建用户。否则创建常规用户(不要用 root 做日常开发):
sudo adduser devusersudo usermod -aG sudo devusersu - devusermkdir -p ~/projects ~/bin将工程放在 ~/projects,个人脚本放 ~/bin(如需加入 PATH 后面配置)。
3. 配置 Git
Section titled “3. 配置 Git”设置全局用户名、邮箱与默认编辑器:
git config --global user.name "Your Name"git config --global user.email you@example.comgit config --global core.editor "nvim"git config --global credential.helper cache4. 安装编辑器(Neovim)与语言工具链
Section titled “4. 安装编辑器(Neovim)与语言工具链”安装 Neovim 与 Python 支持:
sudo apt install -y neovim python3-pippip3 install --user pynvim对于 Node、Python 等工具链,建议使用官方或社区推荐的版本管理器(NodeSource、pyenv、rbenv 等)。
5. 安装内核头与 DKMS(用于构建模块)
Section titled “5. 安装内核头与 DKMS(用于构建模块)”安装与当前内核匹配的头文件:
sudo apt install -y linux-headers-$(uname -r) dkms若运行自定义内核,请从仓库中安装对应版本的 linux-headers-<version>。
6. 图形驱动(NVIDIA)— 安全步骤说明
Section titled “6. 图形驱动(NVIDIA)— 安全步骤说明”Ubuntu 提供了 ubuntu-drivers 来自动安装经测试的闭源驱动:
sudo ubuntu-drivers devicessudo ubuntu-drivers autoinstall安装后重启。若启用了 Secure Boot,重启时系统会提示注册 MOK(Machine-Owner Key)——按提示完成密钥注册以允许未签名模块加载。
如需手动安装指定驱动:
sudo apt install -y nvidia-driver-535sudo reboot验证驱动:
nvidia-smi若失败,检查 dmesg 与 journalctl -u systemd-modules-load 的错误信息以诊断 DKMS 或模块加载问题。
7. Mesa / Intel / AMD(开源驱动)
Section titled “7. Mesa / Intel / AMD(开源驱动)”Intel 与 AMD GPU 通常可用 Mesa。可通过 mesa-utils 进行基本诊断:
sudo apt install -y mesa-utilsglxinfo | grep "OpenGL renderer"如果需要更近版本的 Mesa,可考虑受支持的 PPA,但仅在确有必要时使用。
8. Wi‑Fi 与蓝牙固件
Section titled “8. Wi‑Fi 与蓝牙固件”检查接口:
ip link若设备存在但不可用,查看固件日志:
dmesg | grep -i firmware安装常见固件示例(Intel iwlwifi):
sudo apt install -y linux-firmware-iwlwifisudo modprobe -r iwlwifi && sudo modprobe iwlwifiBroadcom 常见驱动:
sudo apt updatesudo apt install -y bcmwl-kernel-sourcesudo modprobe -r b43 bcma wl && sudo modprobe wl9. 容器(Docker)
Section titled “9. 容器(Docker)”通过官方仓库安装 Docker:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/nullsudo apt updatesudo apt install -y docker-ce docker-ce-cli containerd.iosudo usermod -aG docker $USERnewgrp dockerdocker run --rm hello-world重新登录以使用户组更改生效。
10. Secure Boot 与内核模块
Section titled “10. Secure Boot 与内核模块”若启用 Secure Boot,未签名模块可能被阻止加载。第三方驱动的 DKMS 包通常会触发 MOK 注册提示。若遇到未签名模块错误,可选择在固件中禁用 Secure Boot 或签名模块(高级操作)。
11. 防火墙与 SSH
Section titled “11. 防火墙与 SSH”启用 ufw 并允许 SSH(如需远程访问):
sudo apt install -y ufwsudo ufw allow OpenSSHsudo ufw enable
sudo apt install -y openssh-serversudo systemctl enable --now ssh12. 编译与调试示例
Section titled “12. 编译与调试示例”创建 ~/projects/hello.c:
#include <stdio.h>int main(){ printf("hello\n"); return 0; }构建并运行:
gcc -g -O0 -o hello ~/projects/hello.c./hellogdb ./hello13. 常用排错命令
Section titled “13. 常用排错命令”dmesg— 内核消息journalctl -b— 启动日志sudo lshw -C network— 网络硬件列表lspci -k— 设备与驱动对应lsmod | grep <module>— 查看已加载模块
下一步我可以基于此为你逐步展开:
- 针对 Ubuntu 24.04 的 NVIDIA 驱动逐行安装与故障排查(中英对照);
- 或先做 Wi‑Fi 固件与 Broadcom / Intel 示例的逐步教程。你想先看哪一个?