Skip to content

Instantly share code, notes, and snippets.

@llccing
Created April 11, 2024 00:17
Show Gist options
  • Save llccing/8ccd948f4453fcdfc64af6b0d4921374 to your computer and use it in GitHub Desktop.
Save llccing/8ccd948f4453fcdfc64af6b0d4921374 to your computer and use it in GitHub Desktop.
用于在 Ubuntu 上初始化开发环境,包含了zip, git, java, nodejs, sdkman, pm2, docker, docker-compose 这些常用的工具,以及初始化 ssh key Ed25519
i#!/bin/bash
# 更新系统软件包列表
sudo apt update
# 安装必需的软件包
for pkg in curl wget git vim; do
if ! command -v $pkg &> /dev/null; then
echo "Installing $pkg..."
sudo apt install -y $pkg
else
echo "$pkg is already installed."
fi
done
# 安装 zip 和 unzip
if ! command -v zip &> /dev/null; then
echo "Installing zip..."
sudo apt install -y zip
else
echo "zip is already installed."
fi
if ! command -v unzip &> /dev/null; then
echo "Installing unzip..."
sudo apt install -y unzip
else
echo "unzip is already installed."
fi
# 安装 Docker
if ! command -v docker &> /dev/null; then
echo "Installing Docker..."
sudo apt install -y apt-transport-https ca-certificates software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io
# 添加当前用户到docker组,以避免使用docker时权限问题
if ! groups $USER | grep -q "\bdocker\b"; then
sudo usermod -aG docker $USER
echo "User $USER added to docker group. Please log out and log back in to apply the changes."
else
echo "User $USER is already a member of the docker group."
fi
else
echo "Docker is already installed."
fi
# 安装 Docker Compose
if ! command -v docker-compose &> /dev/null; then
echo "Installing Docker Compose..."
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
else
echo "Docker Compose is already installed."
fi
# 安装或更新 sdkman
if ! command -v sdk &> /dev/null; then
echo "Installing sdkman..."
curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"
else
echo "sdkman is already installed."
fi
# 安装 Java version 21(如果可用)
if sdk list java | grep -q " 21.0."; then
echo "Java 21 is already installed."
else
echo "Installing Java 21..."
sdk install java 21.0.2-amzn
fi
# 安装 Node.js version 21(如果可用)和 pm2
if ! command -v nvm &> /dev/null; then
echo "Installing nvm..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
fi
source "$HOME/.nvm/nvm.sh"
if nvm list | grep -q "v21."; then
echo "Node.js 21 is already installed."
else
echo "Installing Node.js 21..."
nvm install 21
fi
if ! command -v pm2 &> /dev/null; then
echo "Installing pm2..."
npm install -g pm2
else
echo "pm2 is already installed."
fi
# 检查并生成SSH Ed25519密钥对
if [ ! -f ~/.ssh/id_ed25519 ]; then
echo "Generating SSH Ed25519 key pair..."
ssh-keygen -t ed25519 -a 100 -N "" -f ~/.ssh/id_ed25519
echo "SSH Ed25519 key pair generated."
else
echo "SSH Ed25519 key pair already exists."
fi
echo "所有指定的软件已安装完成。"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment