所谓工欲善其事必先利其器,搭环境往往是开发过程中卡出很多初学者的拦路虎。
对于很多老鸟来说,很多东西都已经习惯成自然,也就没有刻意和初学者说。但对于很多初学者,却是受益良多。
这个系列,先从操作系统开始,记录一些在开发中常用的工具和小技巧
.NET 和 iOS 开发请绕路,这里主要针对的Linux。目前很多开发都需要一台Linux环境来进行编译,调试等等。然而公司发给大家的一般都是win的电脑。
考虑到各种安全策略的限制,还没发直接安装Linux系统,若干年前,只能通过Virtualbox等软件来安装虚拟机,来曲线救国。
后来有了Microsoft 的Hyper-V 等技术,方便了虚拟机的安装,但终究是要在不同的系统之间来回切换的,还是不是很方便。那么有什么方法可以在windows 下操作linux呢?
当然有了,不得不提当前Linux最优秀的发行版本,Windows 的 Subsystem Linux(WSL)!!!
曾几何时,WSL 还只能支持命令行操作,在WSL中使用GUI 应用,还需要另外配置RDP。现在,已经进化到自带RDP协议Run Linux GUI apps on the Windows Subsystem for Linux,只需要一个命令,就可以在WSL中使用IDEA敲代码, 然你感受丝般顺滑(真的,不信的去试下在WSL打开IDEA和Win 下打开IDEA的速度)。
支持各类Shell 命令(再也不用在Git Bash 里敲命令了,太爽了)。
安装WSL 也极其简单, 参照这里Install Linux on Windows with WSL
(base) PS C:\Users\Administrator> wsl --list --online
The following is a list of valid distributions that can be installed.
Install using 'wsl.exe --install '.
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
openSUSE-Leap-15.5 openSUSE Leap 15.5
SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
openSUSE-Tumbleweed openSUSE Tumbleweed
wsl --install -d Ubuntu-22.04
wsl --set-version Ubuntu-22.04 2
然后就是进入系统,设置用户名,密码这些,和正常安装ubuntu一样。
sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
sudo cat > /etc/apt/sources.list << EOF
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src 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-src 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-src 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
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
EOF
sudo apt-get update -y
sudo apt-get upgrade -y
sudo apt-get install vim -y # nano用不惯,替换为vim
sudo apt-get remove nano -y
sudo apt-get install curl -y
sudo apt-get install git -y
sudo apt-get install iputils-ping net-tools -y
sudo apt-get install sshpass -y
sudo apt-get install zip unzip -y
sudo apt-get install git -y
sudo apt-get install openssh-server -y
sodu apt-get install jq -y
# install zsh
sudo apt install zsh -y
chsh -s $(which zsh)
## verify
zsh --version
# install oh-my-zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
## 国内
# sh -c "$(wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh -O -)"
# set oh my zsh theme
##vi .zshrc
##ZSH_THEME="afowler"
## 更多主题参见 https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
## plugins 自动补全和加亮
git clone https://github.com/zsh-users/zsh-autosuggestions ${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
## 更多插件,查看 https://github.com/ohmyzsh/ohmyzsh/wiki/Plugins
## plugin 配置
#vi .zshrc
#plugins=(git
#zsh-autosuggestions
#zsh-syntax-highlighting
#)
SDKMAN 是一个 SDK 管理工具, 可以管理多个JDK版本。 除了JDK外,还支持Scala, Groovy 等SDKs
curl -s "https://get.sdkman.io" | bash
sdk install java 8.0.382-amzn
sdk install java 11.0.20-amzn
sdk install java 17.0.8-amzn
sdk install gradle 8.2.1
sdk install maven 3.9.4
sdk install scala 3.3.0
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash
nvm install latest
npm install --global yarn
#(可选)换源
nvm npm_mirror https://npmmirror.com/mirrors/npm/
nvm node_mirror https://npmmirror.com/mirrors/node/
sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6 -y
wget https://repo.anaconda.com/archive/Anaconda3-2022.05-Linux-x86_64.sh # 从这 https://repo.anaconda.com/archive/替换为你需要的版本
sh Anaconda3-2022.05-Linux-x86_64.sh # 一路yes
# (可选)换源
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/menpo
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/simpleitk
conda config --remove-key channels # 建议删除defaut的默认源(推荐) 或者 vi ~/.condarc
conda config --set show_channel_urls yes
conda info
# 创建python 环境
conda create -n mypython python=3.10.3
# 一键安装docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
sudo systemctl start docker
docker version
# 设置Docker 自启动
systemctl daemon-reload
systemctl restart docker.service
systemctl enable docker.service
# 关闭docker 服务自启动
systemctl disable docker.service
# 添加docker 用户组
sudo groupadd docker
sudo gpasswd -a $USER docker
newgrp docker
sudo systemctl restart docker
docker ps
wsl 已经继承了RDP 协议,可以直接在wsl里面起这些有UI的应用
# 查看有哪些可用的
sudo snap search chrome
# 安装
sudo snap install --classic code
sudo snap install firefox
sudo snap install pycharm-community
sudo snap install intellij-idea-community
# 启动的话,只需要 snap run firefox
支持中文
# 安装中文语言包
sudo apt install language-pack-zh-hans
# 设置中文为默认语言 选择en_US.UTF-8和zh_CN.UTF-8, 选择zh_CN.UTF-8为默认语言
sudo dpkg-reconfigure locales
# REF: https://blog.csdn.net/weixin_43589764/article/details/124327175
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
针对自己的需求,可以把常用的写进一个脚本里,这样换环境的话,就能一键setup了。
这是我常用的setup_in_one_script_ubuntu…sh
因为用了zsh, 有些调整需要改下.zshrc 才生效。
还有snap 启动应用这些,可以写一些alias,更加方便
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="$HOME/.oh-my-zsh"
ZSH_THEME="robbyrussell"
plugins=(git
zsh-autosuggestions
zsh-syntax-highlighting
)
source $ZSH/oh-my-zsh.sh
# conda
export PATH=/home/miao18/anaconda3/bin:$PATH
# >>> conda initialize >>>
# !! Contents within this block are managed by 'conda init' !!
__conda_setup="$('/home/miao18/anaconda3/bin/conda' 'shell.bash' 'hook' 2> /dev/null)"
if [ $? -eq 0 ]; then
eval "$__conda_setup"
else
if [ -f "/home/miao18/anaconda3/etc/profile.d/conda.sh" ]; then
. "/home/miao18/anaconda3/etc/profile.d/conda.sh"
else
export PATH="/home/miao18/anaconda3/bin:$PATH"
fi
fi
unset __conda_setup
# <<< conda initialize <<<
conda activate spider
# nvm
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
# alias
alias pycharm="snap run pycharm-community"
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="$HOME/.sdkman"
[[ -s "$HOME/.sdkman/bin/sdkman-init.sh" ]] && source "$HOME/.sdkman/bin/sdkman-init.sh"