个人开发环境配置

工欲善其事必先利其器, 一个良好的开发环境可以大大提高编程效率。本篇文章将会分享安装相应工具来帮助我们提高效率~

单机环境

WSL

Linux系统提供了更友好方便的开发环境,如果你现在正在使用Windows系统,可以尝试使用Windows Subsystem Linux(WSL)开发。

安装

  1. 打开PowerShell安装Ubuntu-22.04

wsl --help
wsl --install Ubuntu-22.04

如果你觉得PowerShell不好用,可以尝试安装Windows Terminal

  1. 替换apt源:

sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
sudo vim /etc/apt/sources.list
  • 添加以下软件仓库

# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
# 在较新的操作系统版本的软件包的新版本
deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
# 预发布软件源,不建议启用
# deb http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src http://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse

Free software

Non-free software

Supported

Main

Restricted

Unsupported

Universe

Multiverse

  1. 更新并安装基础软件

# 更新软件列表
sudo apt-get update
# 更新软件包
sudo apt-get upgrade
sudo apt-get install build-essential

Trouble Shooting

  1. wsl默认安装在C盘中,当wsl中数据越来越多时,会导致C盘容量不足,这个时候我们可以将wsl迁移到其他磁盘中

wsl --list -v
wsl --terminate Ubuntu-22.04
wsl --export Ubuntu-22.04 "D:\wsl_export\ubuntu-ex.tar"
wsl --unregister Ubuntu-22.04
wsl --import Ubuntu-22.04 "D:\wsl_import\ubuntu" "D:\wsl_export\ubuntu-ex.tar"
  1. wsl中使用windows代理, 创建文件%USERPROFILE%\.wslconfig,详见配置说明

[experimental]
autoMemoryReclaim=gradual  # gradual  | dropcache | disabled
networkingMode=mirrored
dnsTunneling=true
firewall=true
autoProxy=true

Oh My Zsh

Linux默认的bash shell虽然在功能已经比较完善了,但如果你想更加个性化一点,可以尝试使用下Oh My Zsh~

安装zsh

  1. 安装zsh

sudo apt install zsh
  1. 安装oh my zsh

sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  1. 安装插件: 在~/.zshrc文件中更新配置

plugins=(
    git
    # 解压: x file
    extract
    # 目录跳转:z dir
    z
    # 自动补全
    zsh-autosuggestions
    # 高亮命令
    zsh-syntax-highlighting
)
  • 自动补全

git clone https://github.com/zsh-users/zsh-autosuggestions.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
  • 高亮命令

git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting

若github连接不上,可使用gitee镜像:https://gitee.com/mirrors/{project}

Trouble Shooting

  1. 无root权限时安装zsh

    • 源码编译安装

wget -O zsh.tar.xz https://sourceforge.net/projects/zsh/files/latest/download
mkdir zsh && unxz zsh.tar.xz && tar -xvf zsh.tar -C zsh --strip-components 1
cd zsh

./configure --prefix=$HOME
make
make install
  • 若出现依赖错误,依然可以源码安装相应依赖,比如configure: error: "No terminal handling library was found on your system. This is probably a library called curses or ncurses. You may need to install a package called 'curses-devel' or 'ncurses-devel' on your system"

wget https://ftp.gnu.org/pub/gnu/ncurses/ncurses-6.1.tar.gz
tar xvfz ncurses-6.1.tar.gz
cd ncurses-6.1
./configure --prefix=$HOME --with-shared
make
make install
# 安装zsh时需要找到相应动态库
export CPPFLAGS="-I$HOME/include" LDFLAGS="-L$HOME/lib"

Homebrew

Ubuntu原生的包管理软件apt不是特别好用,有很多最新的软件包都找不到,这个时候可以安装Homebrew获取最新软件包,尤其在MacOS平台上~

安装

  1. 脚本自动安装

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  1. 配置镜像

echo 'export HOMEBREW_API_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles/api' >> ~/.zshrc
echo 'export HOMEBREW_BOTTLE_DOMAIN=https://mirrors.aliyun.com/homebrew/homebrew-bottles' >> ~/.zshrc
echo 'export HOMEBREW_BREW_GIT_REMOTE=https://mirrors.aliyun.com/homebrew/brew.git' >> ~/.zshrc
echo 'export HOMEBREW_CORE_GIT_REMOTE=https://mirrors.aliyun.com/homebrew/homebrew-core.git' >> ~/.zshrc
source ~/.zshrc

brew update

Trouble Shooting

  1. 安装maven时会自动安装openjdk依赖

brew install --ignore-dependencies maven

Python

Ubuntu系统默认安装了Python3,但实际情况下需要许多虚拟环境,这里我们可以利用conda进行统一管理~

安装

  1. 脚本自动安装

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
sh Miniconda3-latest-Linux-x86_64.sh
  1. 设置pip镜像源

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple
pip config set install.trusted-host mirrors.aliyun.com
  1. 虚拟环境管理

conda create --name tmp python=3.11 -y
conda remove -n tmp --all

Java

JDK有很多的实现,这里我们以OpenJDK为例安装,如果需要Oracle JDK的话,直接去Oracle官网下载相应压缩包,将其解压到相应目录并设置好环境变量即可~

安装

  1. apt安装

sudo apt install openjdk-17-jdk
sudo apt install maven
  1. 配置镜像: ~/.m2/settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <mirrors>
        <mirror>
            <id>aliyunmaven</id>
            <mirrorOf>*</mirrorOf>
            <name>阿里云公共仓库</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </mirror>
    </mirrors>
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <repositories>
                <repository>
                    <id>spring</id>
                    <url>https://maven.aliyun.com/repository/spring</url>
                    <releases>
                        <enabled>true</enabled>
                    </releases>
                    <snapshots>
                        <enabled>true</enabled>
                    </snapshots>
                </repository>
            </repositories>
        </profile>
    </profiles>
</settings>

VSCode

VSCode繁荣的插件系统,可以让我们只需一个IDE就能完成全部工作,避免在不同工具之间切换的麻烦~

安装

直接去官网下载相应软件包安装即可

插件

  1. 远程开发

    1. 配置本地ssh

      ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
      cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
      chmod 0600 ~/.ssh/authorized_keys
    2. 连接远程机器

      # 可将本地公钥复制到远程主机的authorized_keys中,避免重复输入密码连接
      ssh ${user}@${ip} -A
    3. 配置默认shell

      {
          "terminal.integrated.profiles.linux":{
              "zsh": {
                  "path": "${ZSH_PATH}"
              }
          },
          "terminal.integrated.defaultProfile.linux": "zsh",
          "terminal.integrated.shell.linux": "${ZSH_PATH}",
          "terminal.integrated.automationProfile.linux": {
              "path": "${ZSH_PATH}"
          }
      }
  1. Python

  2. Extension Pack for Java

    1. java配置

      {
          "java.jdt.ls.java.home": "{JAVA_LS_PATH}",
          "java.configuration.runtimes": [
              {
                  "name": "JavaSE-1.8",
                  "path": "${JAVA_8_PATH}"
              },
              {
                  "name": "JavaSE-11",
                  "path": "${JAVA_11_PATH}"
              },
              {
                  "name": "JavaSE-17",
                  "path": "${JAVA_17_PATH}"
              },
          ]
      }
      
  1. 其他

    1. Better Comments

    2. Code Spell Checker

    3. Git Graph

    4. SonarLint

容器环境

Docker

从上面单机的配置可以看出来,开发环境其实时很耗时耗力的,幸好Docker为我们封装好了现成的容器环境,可以开箱即用~

安装

  1. 脚本自动安装

curl -fsSL get.docker.com -o get-docker.sh
sudo sh get-docker.sh --mirror Aliyun
  1. 建立docker用户组

sudo groupadd docker
sudo usermod -aG docker $USER
# 重新登陆生效
logout
  1. 设置镜像加速: 在文件/etc/docker/daemon.json添加

{
  "registry-mirrors": [
    "https://hub-mirror.c.163.com",
    "https://mirror.baidubce.com"
  ]
}

阿里云镜像地址获取

  1. 启动docker

# 自启动
# sudo systemctl enable docker
sudo systemctl start docker
docker run --rm hello-world
  1. 安装docker-compose

curl -SL https://github.com/docker/compose/releases/download/v2.20.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose version

k8s

Docker帮我们封装好了应用,而k8s帮我们封装好了集群。

安装

  1. 脚本自动安装

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube
  1. 启动集群

# 使用镜像加速
minikube start --image-mirror-country=cn --kubernetes-version=1.23.0
# 安装kubectl
minikube kubectl -- get po -A
alias kubectl="minikube kubectl --"
  1. 部署应用

kubectl create deployment hello-minikube --image=kicbase/echo-server:1.0
kubectl expose deployment hello-minikube --type=NodePort --port=8080
minikube service hello-minikube
  1. 集群管理

kubectl delete service hello-minikube
kubectl delete deployment hello-minikube
minikube stop

总结

因为基本上都是国外的软件,直接下载的话速度会很慢甚至访问不了,这就要求我们学会配置镜像,或者学会科学上网。。


个人开发环境配置
https://syntomic.cn/archives/personal-development-environment-configuration
作者
syntomic
发布于
2023年03月05日
更新于
2024年08月27日
许可协议