Skip to content

Instantly share code, notes, and snippets.

@daopk
Last active September 26, 2023 04:02
Show Gist options
  • Save daopk/59fb3db6fea5dbaae9542a0e4eeed4c8 to your computer and use it in GitHub Desktop.
Save daopk/59fb3db6fea5dbaae9542a0e4eeed4c8 to your computer and use it in GitHub Desktop.
My script for quickly setting up my development environment in WSL2 Ubuntu
#!/bin/bash
# Update and upgrade packages
sudo apt-get update
sudo apt-get upgrade -y
# Install Homebrew
NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.profile
eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"
# Install zsh using Homebrew
brew install zsh git
# Config git
git config --global credential.helper store
# Append Homebrew to zsh profile
echo 'eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)"' >> ~/.zprofile
# Install Oh My Zsh
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" "" --unattended
# Install zsh-autosuggestions plugin
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
# Add the plugin to the list of plugins in the .zshrc file
sed -i 's/plugins=(git)/plugins=(git zsh-autosuggestions kubectl)/g' ~/.zshrc
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
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
# Install the latest LTS version of Node.js using nvm
nvm install --lts
# Set the LTS version of Node.js as the default
nvm alias default 'lts/*'
# install pnpm
curl -fsSL https://get.pnpm.io/install.sh | sh -
# Set WINDOWS_USERNAME variable to the current Windows host username using PowerShell
WINDOWS_USERNAME=$(powershell.exe '$env:UserName' | tr -d '\r')
# Check if .ssh folder exists in Windows host
if [ -d "/mnt/c/Users/$WINDOWS_USERNAME/.ssh" ]; then
# Prompt user to copy Windows .ssh folder to Ubuntu WSL2 .ssh folder
read -p "Do you want to copy your Windows .ssh folder to your Ubuntu WSL2 .ssh folder? (y/n)" choice
case "$choice" in
y|Y )
# Copy .ssh folder from Windows to Ubuntu WSL2 and set permissions
echo "Copying .ssh folder from Windows to Ubuntu WSL2..."
cp -r "/mnt/c/Users/$WINDOWS_USERNAME/.ssh" ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
;;
n|N )
echo "Skipping .ssh folder copy."
;;
* )
echo "Invalid option. Skipping .ssh folder copy.";;
esac
else
echo "The .ssh folder does not exist in the Windows host."
fi
#!/bin/bash
# Append the nvm setup to ~/.zshrc file
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm' >> ~/.zshrc
echo '[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion' >> ~/.zshrc
echo '' >> ~/.zshrc
echo '# place this after nvm initialization!' >> ~/.zshrc
echo 'autoload -U add-zsh-hook' >> ~/.zshrc
echo '' >> ~/.zshrc
echo 'load-nvmrc() {' >> ~/.zshrc
echo ' local nvmrc_path' >> ~/.zshrc
echo ' nvmrc_path="$(nvm_find_nvmrc)"' >> ~/.zshrc
echo '' >> ~/.zshrc
echo ' if [ -n "$nvmrc_path" ]; then' >> ~/.zshrc
echo ' local nvmrc_node_version' >> ~/.zshrc
echo ' nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")' >> ~/.zshrc
echo '' >> ~/.zshrc
echo ' if [ "$nvmrc_node_version" = "N/A" ]; then' >> ~/.zshrc
echo ' nvm install' >> ~/.zshrc
echo ' elif [ "$nvmrc_node_version" != "$(nvm version)" ]; then' >> ~/.zshrc
echo ' nvm use' >> ~/.zshrc
echo ' fi' >> ~/.zshrc
echo ' elif [ -n "$(PWD=$OLDPWD nvm_find_nvmrc)" ] && [ "$(nvm version)" != "$(nvm version default)" ]; then' >> ~/.zshrc
echo ' echo "Reverting to nvm default version"' >> ~/.zshrc
echo ' nvm use default' >> ~/.zshrc
echo ' fi' >> ~/.zshrc
echo '}' >> ~/.zshrc
echo '' >> ~/.zshrc
echo 'add-zsh-hook chpwd load-nvmrc' >> ~/.zshrc
echo 'load-nvmrc' >> ~/.zshrc
# Set zsh as default shell
command -v zsh | sudo tee -a /etc/shells
chsh -s /home/linuxbrew/.linuxbrew/bin/zsh
# Start zsh shell
exec zsh
@daopk
Copy link
Author

daopk commented Apr 15, 2023

sh -c "$(curl -fsSL https://gist.githubusercontent.com/daopk/59fb3db6fea5dbaae9542a0e4eeed4c8/raw/ubuntu_wsl2_setup.sh)"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment