Ubuntu系统完整配置教程
目录
- 配置镜像源
- 安装网络服务
- 虚拟机中安装CUDA
- Python开发环境配置
- Java开发环境配置
1. 配置镜像源
1.1 备份原始源文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.backup
1.2 编辑源文件
sudo nano /etc/apt/sources.list
1.3 各大镜像源配置
中科大镜像源(推荐)
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.ustc.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
华为镜像源
deb https://mirrors.huaweicloud.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.huaweicloud.com/ubuntu/ jammy-security main restricted universe multiverse
阿里云镜像源
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
山东大学镜像源
deb https://mirrors.sdu.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.sdu.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.sdu.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.sdu.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
清华大学镜像源
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
1.4 更新软件包列表
sudo apt update
sudo apt upgrade -y
2. 安装网络服务
2.1 安装SSH服务(包含SFTP和SCP)
# 安装OpenSSH服务器
sudo apt install openssh-server -y# 启动SSH服务
sudo systemctl start ssh# 设置开机自启
sudo systemctl enable ssh# 检查SSH服务状态
sudo systemctl status ssh# 配置SSH(可选)
sudo nano /etc/ssh/sshd_config
常用SSH配置选项:
Port 22 # SSH端口
PermitRootLogin no # 禁止root登录
PasswordAuthentication yes # 允许密码认证
PubkeyAuthentication yes # 允许公钥认证
配置修改后重启SSH服务:
sudo systemctl restart ssh
2.2 测试SFTP和SCP
# SFTP连接测试
sftp username@localhost# SCP文件传输测试
scp test.txt username@localhost:/home/username/
2.3 安装Telnet服务
# 安装xinetd和telnetd
sudo apt install xinetd telnetd -y# 创建telnet配置文件
sudo nano /etc/xinetd.d/telnet
在文件中添加以下内容:
service telnet
{disable = noflags = REUSEsocket_type = streamwait = nouser = rootserver = /usr/sbin/in.telnetdlog_on_failure += USERID
}
启动和配置服务:
# 重启xinetd服务
sudo systemctl restart xinetd
sudo systemctl enable xinetd# 检查服务状态
sudo systemctl status xinetd# 检查telnet端口
sudo netstat -tlnp | grep :23# 测试连接
telnet localhost
注意: Telnet不安全,建议仅在内网测试环境使用。
3. 虚拟机中安装CUDA
3.1 虚拟机GPU限制说明
VMware虚拟机限制:
- VMware Workstation不支持GPU直通到虚拟机
- 只有VMware vSphere/ESXi企业版支持GPU直通
- 对于学习和开发,建议使用CPU版本
# 检查是否有NVIDIA GPU(虚拟机中通常为空)
lspci | grep -i nvidia# 如果没有输出,说明虚拟机中没有GPU
3.1.1 方案一:安装CPU版本(推荐用于虚拟机)
跳过CUDA安装,直接安装CPU版本的深度学习框架:
3.2 安装NVIDIA驱动
# 添加NVIDIA PPA
sudo add-apt-repository ppa:graphics-drivers/ppa
sudo apt update# 检查可用驱动
ubuntu-drivers devices# 自动安装推荐驱动
sudo ubuntu-drivers autoinstall# 或手动安装特定版本
sudo apt install nvidia-driver-535
3.3 安装CUDA Toolkit
# 下载CUDA(以12.2为例)
wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu2204/x86_64/cuda-keyring_1.1-1_all.deb
sudo dpkg -i cuda-keyring_1.1-1_all.deb
sudo apt-get update# 安装CUDA
sudo apt-get install cuda-12-2# 配置环境变量
echo 'export PATH=/usr/local/cuda-12.2/bin:$PATH' >> ~/.bashrc
echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.2/lib64:$LD_LIBRARY_PATH' >> ~/.bashrc
source ~/.bashrc# 验证安装
nvcc --version
nvidia-smi
3.4 安装cuDNN
# 从NVIDIA官网下载cuDNN(需要注册账号)
# 下载后解压并安装
tar -xzvf cudnn-*.tgz
sudo cp cuda/include/cudnn*.h /usr/local/cuda/include
sudo cp cuda/lib64/libcudnn* /usr/local/cuda/lib64
sudo chmod a+r /usr/local/cuda/include/cudnn*.h /usr/local/cuda/lib64/libcudnn*
4. Python开发环境配置
4.1 安装Python和pip
# 安装Python 3.10(Ubuntu 22.04默认)
sudo apt install python3 python3-pip python3-dev -y# 升级pip
pip3 install --upgrade pip# 安装虚拟环境工具
sudo apt install python3-venv -y
4.2 创建Python虚拟环境
# 创建项目目录
mkdir ~/python_projects
cd ~/python_projects# 创建虚拟环境
python3 -m venv ml_env# 激活虚拟环境
source ml_env/bin/activate
4.3 安装TensorFlow
CPU版本(虚拟机推荐)
# 在虚拟环境中安装CPU版本
pip install tensorflow-cpu# 验证TensorFlow安装
python -c "import tensorflow as tf; print(tf.__version__)"
GPU版本(物理机)
# 需要先安装CUDA和cuDNN
pip install tensorflow[and-cuda]# 验证TensorFlow GPU支持
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
4.4 安装PyTorch
CPU版本(虚拟机推荐)
# 安装PyTorch CPU版本
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu# 验证PyTorch安装
python -c "import torch; print(torch.__version__)"
GPU版本(物理机)
# 安装PyTorch GPU版本(根据CUDA版本选择)
# CUDA 12.1
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121# 验证PyTorch GPU支持
python -c "import torch; print(torch.cuda.is_available())"
4.5 安装常用数据科学库
pip install numpy pandas matplotlib seaborn jupyter scikit-learn opencv-python
4.6 配置Jupyter Notebook
# 生成配置文件
jupyter notebook --generate-config# 设置密码
jupyter notebook password# 启动Jupyter
jupyter notebook --ip=0.0.0.0 --port=8888
5. Java开发环境配置
5.1 安装Java JDK
# 安装OpenJDK 17(LTS版本)
sudo apt install openjdk-17-jdk openjdk-17-doc openjdk-17-source -y# 验证安装
java -version
javac -version# 配置JAVA_HOME
echo 'export JAVA_HOME=/usr/lib/jvm/java-17-openjdk-amd64' >> ~/.bashrc
echo 'export PATH=$JAVA_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
5.2 安装Maven
# 安装Maven
sudo apt install maven -y# 验证安装
mvn -version# 配置Maven镜像(阿里云)
mkdir -p ~/.m2
cat > ~/.m2/settings.xml << EOF
<settings><mirrors><mirror><id>aliyun</id><mirrorOf>central</mirrorOf><name>Aliyun Maven</name><url>https://maven.aliyun.com/repository/public</url></mirror></mirrors>
</settings>
EOF
5.3 安装Gradle
# 下载并安装Gradle
wget https://services.gradle.org/distributions/gradle-8.5-bin.zip
sudo unzip -d /opt/gradle gradle-8.5-bin.zip# 配置环境变量
echo 'export GRADLE_HOME=/opt/gradle/gradle-8.5' >> ~/.bashrc
echo 'export PATH=$GRADLE_HOME/bin:$PATH' >> ~/.bashrc
source ~/.bashrc# 验证安装
gradle -v
5.4 安装开发工具
IntelliJ IDEA(推荐)
# 添加JetBrains仓库
sudo snap install intellij-idea-community --classic# 或下载专业版
# wget https://download.jetbrains.com/idea/ideaIU-2023.3.2.tar.gz
# sudo tar -xzf ideaIU-*.tar.gz -C /opt/
# sudo ln -s /opt/idea-*/bin/idea.sh /usr/local/bin/idea
Visual Studio Code
# 安装VS Code
sudo snap install code --classic# 安装Java扩展包
code --install-extension vscjava.vscode-java-pack
5.5 安装数据库
MySQL
# 安装MySQL
sudo apt install mysql-server -y# 安全配置
sudo mysql_secure_installation# 启动服务
sudo systemctl start mysql
sudo systemctl enable mysql# 创建开发用户
sudo mysql
CREATE USER 'developer'@'localhost' IDENTIFIED BY 'your_password';
GRANT ALL PRIVILEGES ON *.* TO 'developer'@'localhost';
FLUSH PRIVILEGES;
EXIT;
PostgreSQL
# 安装PostgreSQL
sudo apt install postgresql postgresql-contrib -y# 启动服务
sudo systemctl start postgresql
sudo systemctl enable postgresql# 创建开发用户
sudo -u postgres createuser --interactive
5.6 前端开发环境
Node.js和npm
# 安装Node.js 18.x
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
sudo apt install nodejs -y# 验证安装
node -v
npm -v# 安装yarn(可选)
npm install -g yarn# 安装常用工具
npm install -g @vue/cli create-react-app @angular/cli
5.7 容器化工具
Docker
# 安装Docker
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "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/null
sudo apt update
sudo apt install docker-ce docker-ce-cli containerd.io -y# 添加用户到docker组
sudo usermod -aG docker $USER
newgrp docker# 验证安装
docker --version
docker run hello-world
常见问题解决
Q1: 虚拟机中CUDA安装失败
VMware虚拟机限制:
- VMware Workstation不支持GPU直通
- 建议使用CPU版本的TensorFlow和PyTorch
- 如需GPU加速,考虑:
- 使用WSL2(Windows)
- 双系统安装
- 使用云服务(Colab、AWS等)
性能优化建议:
# 增加虚拟机CPU核心数
# 分配更多内存(建议16GB+)
# 使用SSD存储
# 启用虚拟化加速
Q2: TensorFlow无法识别GPU
# 检查CUDA和cuDNN版本兼容性
# TensorFlow 2.15需要CUDA 12.2和cuDNN 8.9
python -c "import tensorflow as tf; print(tf.sysconfig.get_build_info())"
Q3: SSH连接被拒绝
# 检查防火墙
sudo ufw allow 22/tcp
sudo ufw reload# 检查SSH服务
sudo systemctl status ssh
Q4: Maven下载依赖慢
- 确保已配置阿里云镜像
- 检查~/.m2/settings.xml文件
性能优化建议
-
虚拟机配置
- 分配至少8GB内存
- 4个CPU核心
- 50GB以上磁盘空间
- 启用虚拟化技术(VT-x/AMD-V)
-
开发环境优化
- 使用SSD存储
- 配置swap分区
- 定期清理缓存
-
网络优化
- 使用最近的镜像源
- 配置代理(如需要)
备份建议
定期备份以下内容:
- /etc/apt/sources.list
- ~/.bashrc
- ~/.m2/settings.xml
- Python虚拟环境
- 项目代码
# 创建备份脚本
mkdir ~/backups
tar -czf ~/backups/config_backup_$(date +%Y%m%d).tar.gz ~/.bashrc ~/.m2 ~/python_projects
结语
本教程涵盖了Ubuntu系统从基础配置到开发环境搭建的完整流程。根据实际需求,您可以选择性地安装和配置相关组件。建议在虚拟机中先测试所有配置,确认无误后再在生产环境中部署。
如有问题,请查阅官方文档或社区支持。