活动公告

系统通知
05-18 21:22
系统通知
通知:本站资源由网友上传分享,如有违规等问题请到版务模块进行投诉,资源失效请在帖子内回复要求补档,会尽快处理!
10-23 09:31

手把手教你搭建个性化Gentoo Linux开发环境提升编程效率详解系统配置软件安装及环境优化技巧让开发更高效顺畅

SunJu_FaceMall

3万

主题

2860

科技点

3万

积分

白金月票

碾压王

积分
32872

塔罗立华奏

<font color=白金月票" /> 发表于 2025-9-30 17:40:01 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
引言

Gentoo Linux是一个高度灵活、可定制的发行版,以其源代码为基础的软件包管理系统和极致的定制能力而闻名。对于开发者而言,Gentoo提供了一个理想的平台,可以根据个人需求精确控制每个组件,从而打造一个高效、稳定的开发环境。本文将详细介绍如何从零开始搭建一个个性化的Gentoo Linux开发环境,包括系统安装、配置、软件安装以及环境优化,帮助您提升编程效率,让开发过程更加高效顺畅。

准备工作

在开始安装Gentoo Linux之前,我们需要做好充分的准备工作,确保安装过程顺利进行。

硬件要求

Gentoo Linux对硬件的要求相对灵活,但为了获得良好的开发体验,建议以下配置:

• CPU:64位处理器(x86_64或ARM64)
• 内存:至少4GB RAM,推荐8GB或更多
• 存储空间:至少20GB可用空间,推荐SSD以获得更好的性能
• 网络连接:稳定的互联网连接,用于下载源代码和软件包

下载安装介质

首先,我们需要从Gentoo官方网站下载最新的安装介质:
  1. # 访问Gentoo官网下载页面
  2. https://www.gentoo.org/downloads/
  3. # 选择适合您架构的安装介质(如amd64)
  4. # 下载minimal安装CD或stage3 tarball
复制代码

创建可启动安装介质

下载完成后,我们需要创建一个可启动的USB安装介质:
  1. # 在Linux系统上使用dd命令创建可启动USB
  2. # 假设USB设备为/dev/sdb,请根据实际情况替换
  3. sudo dd if=gentoo-install-amd64-minimal.iso of=/dev/sdb bs=4M status=progress
  4. sudo sync
复制代码

系统安装

启动安装环境

1. 将创建好的USB安装介质插入目标计算机,并设置BIOS/UEFI从USB启动。
2. 启动后,您将看到Gentoo的启动菜单。选择默认选项进入安装环境。
3. 登录后,首先配置网络:
  1. # 检查网络接口
  2. ip a
  3. # 如果使用DHCP,通常网络会自动配置
  4. # 如果需要手动配置静态IP
  5. ip addr add 192.168.1.100/24 dev eth0
  6. ip route add default via 192.168.1.1
  7. echo "nameserver 8.8.8.8" > /etc/resolv.conf
复制代码

磁盘分区

Gentoo安装需要手动分区,这里我们以UEFI系统为例,使用GPT分区表:
  1. # 启动 parted 进行分区
  2. parted /dev/sda
  3. # 创建GPT分区表
  4. mklabel gpt
  5. # 创建EFI系统分区(ESP)
  6. mkpart ESP fat32 1MiB 512MiB
  7. set 1 boot on
  8. # 创建swap分区(根据内存大小调整)
  9. mkpart swap linux-swap 512MiB 8GiB
  10. # 创建根分区
  11. mkpart root ext4 8GiB 100%
  12. # 退出parted
  13. quit
  14. # 格式化分区
  15. mkfs.fat -F 32 /dev/sda1
  16. mkswap /dev/sda2
  17. mkfs.ext4 /dev/sda3
  18. # 启用swap
  19. swapon /dev/sda2
  20. # 挂载分区
  21. mount /dev/sda3 /mnt/gentoo
  22. mkdir /mnt/gentoo/boot
  23. mount /dev/sda1 /mnt/gentoo/boot
复制代码

安装Gentoo基础系统
  1. # 确保系统时间准确
  2. ntpd -qg
  3. # 下载stage3 tarball
  4. cd /mnt/gentoo
  5. links https://www.gentoo.org/downloads/mirrors/
  6. # 选择一个镜像,进入releases/amd64/autobuilds/current-stage3-amd64/目录
  7. # 下载最新的stage3 tarball
  8. # 或者使用wget直接下载(替换URL为实际的stage3 tarball链接)
  9. wget http://distfiles.gentoo.org/releases/amd64/autobuilds/current-stage3-amd64/stage3-amd64-*.tar.xz
  10. # 解压stage3 tarball
  11. tar xpvf stage3-*.tar.xz --xattrs-include='*.*' --numeric-owner
  12. # 配置编译选项
  13. nano -w /mnt/gentoo/etc/portage/make.conf
复制代码

在make.conf中添加以下内容,根据您的CPU和需求进行调整:
  1. # 通用编译选项
  2. COMMON_FLAGS="-O2 -pipe -march=native"
  3. CFLAGS="${COMMON_FLAGS}"
  4. CXXFLAGS="${COMMON_FLAGS}"
  5. FCFLAGS="${COMMON_FLAGS}"
  6. FFLAGS="${COMMON_FLAGS}"
  7. # 设置并行编译任务数(通常是CPU核心数+1)
  8. MAKEOPTS="-j5"
  9. # 设置GENTOO_MIRRORS和USE标志
  10. GENTOO_MIRRORS="https://mirrors.tuna.tsinghua.edu.cn/gentoo"
  11. USE="X gtk gnome kde -systemd -pulseaudio alsa bluetooth ffmpeg"
复制代码

配置Portage
  1. # 复制Portage配置文件
  2. mkdir --parents /mnt/gentoo/etc/portage/repos.conf
  3. cp /mnt/gentoo/usr/share/portage/config/repos.conf /mnt/gentoo/etc/portage/repos.conf/gentoo.conf
  4. # 复制DNS信息
  5. cp --dereference /etc/resolv.conf /mnt/gentoo/etc/
  6. # 挂载必要的文件系统
  7. mount --types proc /proc /mnt/gentoo/proc
  8. mount --rbind /sys /mnt/gentoo/sys
  9. mount --make-rslave /mnt/gentoo/sys
  10. mount --rbind /dev /mnt/gentoo/dev
  11. mount --make-rslave /mnt/gentoo/dev
  12. mount --bind /run /mnt/gentoo/run
  13. mount --make-slave /mnt/gentoo/run
复制代码

进入新系统
  1. # 进入chroot环境
  2. chroot /mnt/gentoo /bin/bash
  3. source /etc/profile
  4. export PS1="(chroot) ${PS1}"
  5. # 挂载boot分区
  6. mount /dev/sda1 /boot
复制代码

安装基础系统
  1. # 更新Portage树
  2. emerge --sync --quiet
  3. # 选择profile
  4. eselect profile list
  5. eselect profile set default/linux/amd64/17.1/desktop/plasma/systemd (或根据您的需求选择)
  6. # 更新@world集合
  7. emerge --update --deep --newuse @world
  8. # 设置时区
  9. echo "Asia/Shanghai" > /etc/timezone
  10. emerge --config sys-libs/timezone-data
  11. # 设置locale
  12. nano -w /etc/locale.gen
  13. # 取消注释 en_US.UTF-8 UTF-8 和 zh_CN.UTF-8 UTF-8
  14. locale-gen
  15. eselect locale set en_US.utf8
  16. env-update && source /etc/profile && export PS1="(chroot) ${PS1}"
  17. # 安装固件
  18. emerge sys-kernel/linux-firmware
复制代码

安装和配置内核
  1. # 安装内核源码
  2. emerge sys-kernel/gentoo-sources
  3. # 配置内核
  4. cd /usr/src/linux
  5. make menuconfig
  6. # 或者使用genkernel自动配置
  7. emerge sys-kernel/genkernel
  8. genkernel all
复制代码

在手动配置内核时,确保启用以下关键选项:
  1. Processor type and features  --->
  2.     [*] Symmetric multi-processing support
  3.     [*] Enable LPAE for physical memory extensions
  4. Device Drivers  --->
  5.     Generic Driver Options  --->
  6.         [*] Maintain a devtmpfs filesystem to mount at /dev
  7.     [*] Fusion MPT device support  --->
  8.         <*>   Fusion MPT ScsiHost drivers for SPI
  9.         <*>   Fusion MPT ScsiHost drivers for FC
  10.         <*>   Fusion MPT ScsiHost drivers for SAS
  11. File systems  --->
  12.     <*> The Extended 4 (ext4) filesystem
  13.     <*> Reiserfs support
  14.     <*> JFS filesystem support
  15.     <*> XFS filesystem support
  16.     <*> Btrfs filesystem support
  17. Networking support  --->
  18.     Networking options  --->
  19.         <*> Packet socket
  20.         <*> Unix domain sockets
  21.         [*] TCP/IP networking
  22.         <*>   IP: multicasting
  23.         <*>   IP: advanced router
  24.         <*>   IP: policy routing
  25.         <*>   IP: verbose route monitoring
  26.         <*>   IP: TCP syncookie support
复制代码

安装系统工具
  1. # 安装必要的系统工具
  2. emerge sys-apps/pcmciautils
  3. emerge net-misc/dhcpcd
  4. emerge sys-apps/usbutils
  5. # 安装日志工具
  6. emerge app-admin/sysklogd
  7. rc-update add sysklogd default
  8. # 安装cron工具
  9. emerge sys-process/cronie
  10. rc-update add cronie default
  11. # 安装文件系统工具
  12. emerge sys-fs/e2fsprogs
  13. emerge sys-fs/xfsprogs
  14. emerge sys-fs/btrfs-progs
复制代码

配置引导程序
  1. # 安装GRUB2引导程序
  2. emerge sys-boot/grub:2
  3. # 配置GRUB2
  4. grub-install --target=x86_64-efi --efi-directory=/boot --bootloader-id=gentoo
  5. # 生成GRUB配置
  6. grub-mkconfig -o /boot/grub/grub.cfg
复制代码

完成安装
  1. # 设置root密码
  2. passwd
  3. # 退出chroot环境
  4. exit
  5. # 卸载文件系统
  6. umount -l /mnt/gentoo/dev{/shm,/pts,}
  7. umount -R /mnt/gentoo
  8. # 重启系统
  9. reboot
复制代码

基础配置

系统更新与维护
  1. # 更新系统
  2. emerge --sync
  3. emerge --update --deep --newuse @world
  4. # 清理不需要的依赖
  5. emerge --depclean
  6. # 重建已损坏的包
  7. revdep-rebuild
复制代码

用户和权限管理
  1. # 创建普通用户
  2. useradd -m -G users,wheel,audio,video,usb,cdrom,portage -s /bin/bash developer
  3. passwd developer
  4. # 配置sudo
  5. emerge app-admin/sudo
  6. visudo
  7. # 取消注释 %wheel ALL=(ALL) ALL
复制代码

网络配置
  1. # 配置有线网络
  2. emerge net-misc/netifrc
  3. echo 'config_eth0="dhcp"' > /etc/conf.d/net
  4. cd /etc/init.d
  5. ln -s net.lo net.eth0
  6. rc-update add net.eth0 default
  7. # 配置无线网络(如果需要)
  8. emerge net-wireless/iw net-wireless/wpa_supplicant
  9. echo 'config_wlan0="dhcp"' > /etc/conf.d/net
  10. cd /etc/init.d
  11. ln -s net.lo net.wlan0
  12. rc-update add net.wlan0 default
复制代码

配置Xorg和显示服务器
  1. # 安装Xorg
  2. emerge xorg-server
  3. # 安装显卡驱动
  4. # NVIDIA显卡
  5. emerge x11-drivers/nvidia-drivers
  6. # AMD显卡
  7. emerge x11-drivers/xf86-video-amdgpu
  8. # Intel显卡
  9. emerge x11-drivers/xf86-video-intel
  10. # 配置Xorg
  11. Xorg -configure
  12. mv /root/xorg.conf.new /etc/X11/xorg.conf
复制代码

开发环境搭建

基础开发工具
  1. # 安装基础开发工具
  2. emerge sys-devel/gcc sys-devel/binutils sys-devel/make sys-devel/automake sys-devel/autoconf sys-devel/bison sys-devel/flex
  3. # 安装版本控制系统
  4. emerge dev-vcs/git dev-vcs/subversion dev-vcs/mercurial
  5. # 安装构建工具
  6. emerge dev-util/cmake dev-util/ninja dev-util/meson
复制代码

Python开发环境
  1. # 安装Python
  2. emerge dev-lang/python:3.9 dev-lang/python:3.10
  3. # 安装虚拟环境工具
  4. emerge dev-python/virtualenv dev-python/pip
  5. # 安装常用Python包
  6. pip install numpy scipy matplotlib pandas jupyter
复制代码

Java开发环境
  1. # 安装OpenJDK
  2. emerge virtual/jdk
  3. # 或者安装Oracle JDK(需要先接受许可协议)
  4. echo 'dev-java/oracle-jdk-bin oracle-jdk-bin' > /etc/portage/package.license/oracle-jdk
  5. emerge dev-java/oracle-jdk-bin
  6. # 安装Maven和Gradle
  7. emerge dev-java/maven dev-java/gradle-bin
复制代码

C/C++开发环境
  1. # 安装额外的编译器和调试工具
  2. emerge sys-devel/clang sys-devel/gdb sys-devel/lld
  3. # 安装代码分析工具
  4. emerge dev-util/cppcheck dev-util/valgrind
  5. # 安装构建系统
  6. emerge dev-build/ninja dev-build/meson
复制代码

Web开发环境
  1. # 安装Node.js和npm
  2. emerge net-libs/nodejs
  3. # 安装常用前端工具
  4. npm install -g typescript webpack babel-cli
  5. # 安装后端环境
  6. # PHP
  7. emerge dev-lang/php
  8. # Ruby
  9. emerge dev-lang/ruby
  10. # Go
  11. emerge dev-lang/go
  12. # Rust
  13. emerge dev-lang/rust
复制代码

数据库环境
  1. # 安装MySQL/MariaDB
  2. emerge dev-db/mariadb
  3. # 初始化数据库
  4. emerge --config dev-db/mariadb
  5. rc-update add mysql default
  6. rc-service mysql start
  7. # 安装PostgreSQL
  8. emerge dev-db/postgresql
  9. # 安装MongoDB
  10. emerge dev-db/mongodb
  11. # 安装Redis
  12. emerge dev-db/redis
复制代码

IDE和编辑器
  1. # 安装Visual Studio Code
  2. emerge app-editors/vscode
  3. # 安装JetBrains IDEs
  4. emerge dev-util/jetbrains-toolbox
  5. # 安装Vim/Neovim
  6. emerge app-editors/vim app-editors/neovim
  7. # 安装Emacs
  8. emerge app-editors/emacs
复制代码

环境优化

系统性能优化
  1. # 配置系统启动服务
  2. rc-update del bootmisc boot
  3. rc-update del consolefont boot
  4. rc-update del keymaps boot
  5. # 优化内核参数
  6. echo "vm.swappiness=10" >> /etc/sysctl.conf
  7. echo "vm.vfs_cache_pressure=50" >> /etc/sysctl.conf
  8. sysctl -p
  9. # 安装并配置systemd-cron(如果使用systemd)
  10. emerge sys-apps/systemd-cron
复制代码

文件系统优化
  1. # 为SSD优化
  2. echo "noop" > /sys/block/sda/queue/scheduler
  3. echo "1" > /sys/block/sda/queue/iosched/fq_low_latency
  4. # 添加到/etc/rc.local使其永久生效
  5. echo 'echo "noop" > /sys/block/sda/queue/scheduler' >> /etc/rc.local
  6. echo 'echo "1" > /sys/block/sda/queue/iosched/fq_low_latency' >> /etc/rc.local
  7. chmod +x /etc/rc.local
  8. # 配置fstrim定时任务(针对SSD)
  9. emerge sys-apps/util-linux
  10. systemctl enable fstrim.timer
复制代码

内存管理优化
  1. # 安装并配置zram
  2. emerge sys-block/zram-init
  3. echo 'ALGO="lz4"' >> /etc/conf.d/zram-init
  4. echo 'SIZE="512"' >> /etc/conf.d/zram-init
  5. rc-update add zram-init default
复制代码

网络优化
  1. # 优化网络设置
  2. echo "net.core.rmem_max = 16777216" >> /etc/sysctl.conf
  3. echo "net.core.wmem_max = 16777216" >> /etc/sysctl.conf
  4. echo "net.ipv4.tcp_rmem = 4096 87380 16777216" >> /etc/sysctl.conf
  5. echo "net.ipv4.tcp_wmem = 4096 65536 16777216" >> /etc/sysctl.conf
  6. echo "net.core.netdev_max_backlog = 5000" >> /etc/sysctl.conf
  7. echo "net.ipv4.tcp_congestion_control = bbr" >> /etc/sysctl.conf
  8. sysctl -p
复制代码

开发环境个性化配置
  1. # 编辑~/.bashrc
  2. cat << EOF > ~/.bashrc
  3. # ~/.bashrc: executed by bash(1) for non-login shells.
  4. # If not running interactively, don't do anything
  5. case \$- in
  6.     *i*) ;;
  7.       *) return;;
  8. esac
  9. # don't put duplicate lines or lines starting with space in the history.
  10. HISTCONTROL=ignoreboth
  11. # append to the history file, don't overwrite it
  12. shopt -s histappend
  13. # for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
  14. HISTSIZE=1000
  15. HISTFILESIZE=2000
  16. # check the window size after each command and, if necessary,
  17. # update the values of LINES and COLUMNS.
  18. shopt -s checkwinsize
  19. # set a fancy prompt (non-color, unless we know we "want" color)
  20. case "\$TERM" in
  21.     xterm-color|*-256color) color_prompt=yes;;
  22. esac
  23. force_color_prompt=yes
  24. if [ -n "\$force_color_prompt" ]; then
  25.     if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
  26.         color_prompt=yes
  27.     else
  28.         color_prompt=
  29.     fi
  30. fi
  31. if [ "\$color_prompt" = yes ]; then
  32.     PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
  33. else
  34.     PS1='\u@\h:\w\$ '
  35. fi
  36. unset color_prompt force_color_prompt
  37. # enable color support of ls and also add handy aliases
  38. if [ -x /usr/bin/dircolors ]; then
  39.     test -r ~/.dircolors && eval "\$(dircolors -b ~/.dircolors)" || eval "\$(dircolors -b)"
  40.     alias ls='ls --color=auto'
  41.     alias grep='grep --color=auto'
  42.     alias fgrep='fgrep --color=auto'
  43.     alias egrep='egrep --color=auto'
  44. fi
  45. # colored GCC warnings and errors
  46. export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
  47. # some more ls aliases
  48. alias ll='ls -alF'
  49. alias la='ls -A'
  50. alias l='ls -CF'
  51. # Add an "alert" alias for long running commands.
  52. alias alert='notify-send --urgency=low -i "\$([ \$? = 0 ] && echo terminal || echo error)" "\$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert\$//'\'')"'
  53. # enable programmable completion features
  54. if ! shopt -oq posix; then
  55.   if [ -f /usr/share/bash-completion/bash_completion ]; then
  56.     . /usr/share/bash-completion/bash_completion
  57.   elif [ -f /etc/bash_completion ]; then
  58.     . /etc/bash_completion
  59.   fi
  60. fi
  61. # Custom aliases and functions
  62. alias update='sudo emerge --sync && sudo emerge --update --deep --newuse @world'
  63. alias clean='sudo emerge --depclean && sudo revdep-rebuild'
  64. alias ..='cd ..'
  65. alias ...='cd ../..'
  66. alias ....='cd ../../..'
  67. alias grep='grep --color=auto'
  68. alias egrep='egrep --color=auto'
  69. alias fgrep='fgrep --color=auto'
  70. # Extract files based on extension
  71. extract() {
  72.     if [ -f \$1 ] ; then
  73.         case \$1 in
  74.             *.tar.bz2)   tar xjf \$1     ;;
  75.             *.tar.gz)    tar xzf \$1     ;;
  76.             *.bz2)       bunzip2 \$1     ;;
  77.             *.rar)       unrar e \$1     ;;
  78.             *.gz)        gunzip \$1      ;;
  79.             *.tar)       tar xf \$1      ;;
  80.             *.tbz2)      tar xjf \$1     ;;
  81.             *.tgz)       tar xzf \$1     ;;
  82.             *.zip)       unzip \$1       ;;
  83.             *.Z)         uncompress \$1  ;;
  84.             *.7z)        7z x \$1        ;;
  85.             *)     echo "'\$1' cannot be extracted via extract()" ;;
  86.              esac
  87.          else
  88.              echo "'\$1' is not a valid file"
  89.      fi
  90. }
  91. # Create a directory and cd into it
  92. mkcd() {
  93.     mkdir -p "\$1" && cd "\$1"
  94. }
  95. EOF
复制代码
  1. # 安装Vim插件管理器vim-plug
  2. curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
  3.     https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  4. # 创建.vimrc配置文件
  5. cat << EOF > ~/.vimrc
  6. " Enable syntax highlighting
  7. syntax on
  8. " Set line numbers
  9. set number
  10. " Enable mouse support
  11. set mouse=a
  12. " Set tab width to 4 spaces
  13. set tabstop=4
  14. set shiftwidth=4
  15. set expandtab
  16. " Enable file type detection
  17. filetype on
  18. filetype plugin on
  19. filetype indent on
  20. " Set encoding to UTF-8
  21. set encoding=utf-8
  22. " Enable search highlighting
  23. set hlsearch
  24. set incsearch
  25. " Enable smart case in search
  26. set smartcase
  27. " Show matching brackets
  28. set showmatch
  29. " Enable line wrapping
  30. set wrap
  31. " Set leader key
  32. let mapleader = ","
  33. " Split navigation
  34. nnoremap <C-J> <C-W><C-J>
  35. nnoremap <C-K> <C-W><C-K>
  36. nnoremap <C-L> <C-W><C-L>
  37. nnoremap <C-H> <C-W><C-H>
  38. " Clear search highlighting
  39. nnoremap <leader>/ :nohlsearch<CR>
  40. " Toggle line numbers
  41. nnoremap <leader>n :set number!<CR>
  42. " Toggle wrap
  43. nnoremap <leader>w :set wrap!<CR>
  44. " Plugins will be installed below
  45. call plug#begin('~/.vim/plugged')
  46. " Status line
  47. Plug 'vim-airline/vim-airline'
  48. Plug 'vim-airline/vim-airline-themes'
  49. " File explorer
  50. Plug 'preservim/nerdtree', { 'on': 'NERDTreeToggle' }
  51. " Fuzzy finder
  52. Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
  53. Plug 'junegunn/fzf.vim'
  54. " Code completion
  55. Plug 'neoclide/coc.nvim', {'branch': 'release'}
  56. " Syntax highlighting
  57. Plug 'sheerun/vim-polyglot'
  58. " Git integration
  59. Plug 'tpope/vim-fugitive'
  60. Plug 'airblade/vim-gitgutter'
  61. " Themes
  62. Plug 'morhetz/gruvbox'
  63. " Auto pairs
  64. Plug 'jiangmiao/auto-pairs'
  65. " Comment code
  66. Plug 'tpope/vim-commentary'
  67. " Surround
  68. Plug 'tpope/vim-surround'
  69. " Indent guides
  70. Plug 'Yggdroot/indentLine'
  71. call plug#end()
  72. " Set theme
  73. colorscheme gruvbox
  74. set background=dark
  75. " NERDTree configuration
  76. nnoremap <leader>e :NERDTreeToggle<CR>
  77. nnoremap <leader>ef :NERDTreeFind<CR>
  78. let NERDTreeShowHidden=1
  79. let NERDTreeQuitOnOpen=1
  80. let NERDTreeAutoDeleteBuffer=1
  81. " Airline configuration
  82. let g:airline#extensions#tabline#enabled=1
  83. let g:airline_powerline_fonts=1
  84. " FZF configuration
  85. nnoremap <leader>f :Files<CR>
  86. nnoremap <leader>b :Buffers<CR>
  87. nnoremap <leader>g :Rg<CR>
  88. " CoC configuration
  89. let g:coc_global_extensions = [
  90.     \ 'coc-json',
  91.     \ 'coc-tsserver',
  92.     \ 'coc-html',
  93.     \ 'coc-css',
  94.     \ 'coc-python',
  95.     \ 'coc-java',
  96.     \ 'coc-clangd',
  97.     \ 'coc-go',
  98.     \ 'coc-rust-analyzer'
  99.     \ ]
  100. " Use tab for trigger completion with characters ahead and navigate
  101. inoremap <silent><expr> <TAB>
  102.       \ pumvisible() ? "\<C-n>" :
  103.       \ <SID>check_back_space() ? "\<TAB>" :
  104.       \ coc#refresh()
  105. inoremap <expr><S-TAB> pumvisible() ? "\<C-p>" : "\<C-h>"
  106. function! s:check_back_space() abort
  107.   let col = col('.') - 1
  108.   return !col || getline('.')[col - 1]  =~# '\s'
  109. endfunction
  110. " Use <c-space> to trigger completion
  111. inoremap <silent><expr> <c-space> coc#refresh()
  112. " Use <cr> to confirm completion
  113. inoremap <expr> <cr> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
  114. " Use `[g` and `]g` to navigate diagnostics
  115. nmap <silent> [g <Plug>(coc-diagnostic-prev)
  116. nmap <silent> ]g <Plug>(coc-diagnostic-next)
  117. " GoTo code navigation
  118. nmap <silent> gd <Plug>(coc-definition)
  119. nmap <silent> gy <Plug>(coc-type-definition)
  120. nmap <silent> gi <Plug>(coc-implementation)
  121. nmap <silent> gr <Plug>(coc-references)
  122. " Use K to show documentation in preview window
  123. nnoremap <silent> K :call <SID>show_documentation()<CR>
  124. function! s:show_documentation()
  125.   if (index(['vim','help'], &filetype) >= 0)
  126.     execute 'h '.expand('<cword>')
  127.   else
  128.     call CocAction('doHover')
  129.   endif
  130. endfunction
  131. " Highlight the symbol and its references when holding the cursor
  132. autocmd CursorHold * silent call CocActionAsync('highlight')
  133. " Symbol renaming
  134. nmap <leader>rn <Plug>(coc-rename)
  135. " Formatting selected code
  136. xmap <leader>f  <Plug>(coc-format-selected)
  137. nmap <leader>f  <Plug>(coc-format-selected)
  138. " Applying code actions to the selected code block
  139. xmap <leader>a  <Plug>(coc-codeaction-selected)
  140. nmap <leader>a  <Plug>(coc-codeaction-selected)
  141. " Remap keys for applying code actions at the cursor position
  142. nmap <leader>ac  <Plug>(coc-codeaction-cursor)
  143. " Remap keys for apply code actions affect whole buffer
  144. nmap <leader>as  <Plug>(coc-codeaction-source)
  145. " Apply the most preferred quickfix action to fix diagnostic on the current line
  146. nmap <leader>qf  <Plug>(coc-fix-current)
  147. " Map function and class text objects
  148. xmap if <Plug>(coc-funcobj-i)
  149. omap if <Plug>(coc-funcobj-i)
  150. xmap af <Plug>(coc-funcobj-a)
  151. omap af <Plug>(coc-funcobj-a)
  152. xmap ic <Plug>(coc-classobj-i)
  153. omap ic <Plug>(coc-classobj-i)
  154. xmap ac <Plug>(coc-classobj-a)
  155. omap ac <Plug>(coc-classobj-a)
  156. EOF
复制代码
  1. # 配置Git
  2. git config --global user.name "Your Name"
  3. git config --global user.email "your.email@example.com"
  4. git config --global core.editor vim
  5. git config --global color.ui true
  6. git config --global push.default simple
  7. # 配置Git别名
  8. git config --global alias.st status
  9. git config --global alias.co checkout
  10. git config --global alias.br branch
  11. git config --global alias.ci commit
  12. git config --global alias.unstage 'reset HEAD --'
  13. git config --global alias.last 'log -1 HEAD'
  14. git config --global alias.visual '!gitk'
  15. # 生成SSH密钥
  16. ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
  17. eval "$(ssh-agent -s)"
  18. ssh-add ~/.ssh/id_rsa
  19. # 显示公钥,用于添加到GitHub等平台
  20. cat ~/.ssh/id_rsa.pub
复制代码

桌面环境配置
  1. # 安装KDE Plasma
  2. emerge kde-plasma/plasma-meta kde-plasma/plasma-nm kde-apps/kde-apps-meta
  3. # 启用显示管理器
  4. rc-update add sddm default
  5. rc-service sddm start
复制代码
  1. # 安装GNOME
  2. emerge gnome-base/gnome gnome-base/gnome-extra-apps
  3. # 启用显示管理器
  4. rc-update add gdm default
  5. rc-service gdm start
复制代码
  1. # 安装Xfce
  2. emerge xfce-base/xfce4-meta xfce-extra/xfce4-terminal xfce-extra/thunar-volman
  3. # 安装显示管理器
  4. emerge x11-misc/lightdm
  5. rc-update add lightdm default
  6. rc-service lightdm start
复制代码

常见问题解决

编译错误处理

在Gentoo中,由于所有软件都是从源代码编译的,可能会遇到各种编译错误。以下是一些常见问题的解决方法:
  1. # 如果遇到依赖问题,可以尝试以下命令
  2. emerge --update --deep --newuse @world
  3. emerge --depclean
  4. revdep-rebuild
  5. # 如果特定包编译失败,可以尝试重新安装
  6. emerge --oneshot --emptytree <package-name>
  7. # 查看详细的编译日志
  8. emerge --verbose --jobs=1 <package-name>
复制代码
  1. # 检查USE标志
  2. emerge --info | grep USE
  3. # 临时修改USE标志
  4. USE="-flag1 flag2" emerge <package-name>
  5. # 永久修改USE标志
  6. echo "category/package-name flag1 -flag2" >> /etc/portage/package.use/custom
复制代码
  1. # 如果遇到许可证问题,可以接受许可证
  2. echo "category/package-name license-name" >> /etc/portage/package.license/custom
复制代码

性能问题解决
  1. # 检查系统资源使用情况
  2. top
  3. htop
  4. iotop
  5. # 检查启动服务
  6. rc-status
  7. # 禁用不必要的服务
  8. rc-update del service-name default/boot/sysinit
  9. # 检查内核日志
  10. dmesg | tail
复制代码
  1. # 检查内存使用情况
  2. free -h
  3. cat /proc/meminfo
  4. # 检查swap使用情况
  5. swapon --show
  6. # 增加swap文件
  7. fallocate -l 2G /swapfile
  8. chmod 600 /swapfile
  9. mkswap /swapfile
  10. swapon /swapfile
  11. echo '/swapfile none swap sw 0 0' >> /etc/fstab
复制代码

网络问题解决
  1. # 检查网络接口
  2. ip a
  3. # 检查路由
  4. ip r
  5. # 检查DNS
  6. cat /etc/resolv.conf
  7. # 重启网络服务
  8. rc-service net.eth0 restart
复制代码
  1. # 更换Gentoo镜像
  2. echo 'GENTOO_MIRRORS="https://mirrors.tuna.tsinghua.edu.cn/gentoo"' >> /etc/portage/make.conf
  3. # 配置DISTDIR镜像
  4. echo 'GENTOO_MIRRORS="https://mirrors.tuna.tsinghua.edu.cn/gentoo"' >> /etc/portage/make.conf
  5. echo 'PORTAGE_BINHOST="https://mirrors.tuna.tsinghua.edu.cn/gentoo/packages"' >> /etc/portage/make.conf
复制代码

总结

通过本文的详细指导,您已经成功搭建了一个个性化的Gentoo Linux开发环境。从系统安装到基础配置,再到开发环境搭建和环境优化,我们涵盖了整个过程的关键步骤。Gentoo Linux的灵活性和可定制性使其成为开发者的理想选择,您可以根据自己的需求进一步调整和优化系统。

记住,Gentoo的学习曲线可能较陡,但一旦熟悉了其工作原理,您将拥有一个完全符合个人需求的高效开发环境。定期更新系统、维护软件包、优化性能,将确保您的开发环境始终保持最佳状态。

希望本文能帮助您在Gentoo Linux上实现更高效、更顺畅的开发体验。祝您编程愉快!
「七転び八起き(ななころびやおき)」
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则