Skip to content

Instantly share code, notes, and snippets.

@shivapoudel
Last active February 19, 2023 19:47
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shivapoudel/1de1a384116975a27d6dd5d29d612699 to your computer and use it in GitHub Desktop.
Save shivapoudel/1de1a384116975a27d6dd5d29d612699 to your computer and use it in GitHub Desktop.
WSL setup for Ubuntu environment.

Installing Git on WSL

Git appears to come as standard as part of the WSL install. You can test this by running:

$ git --version

If for some reason Git is not installed then you can simply pull it down:

sudo apt-get update
sudo apt-get install git

Setup global configuration settings

git config --global user.name "Shiva Poudel"
git config --global user.email "3774827+shivapoudel@users.noreply.github.com"
git config --global core.autocrlf false

Additionally, follow this documentation to generate new SSH key and add it to your GitHub account.

Sharing an existing configuration and SSH key between Windows and WSL

If you have an configuration and SSH key already setup on Windows you could reuse it rather than creating a new one. (Note that PuTTY keys do not work here). To do this you can just copy the key from the Windows filesystem into the WSL's filesystem. Linux has some rules about how visible the key is. So you need to make sure the access levels are strict enough to meet these requirements:

Configuration settings

cd ~
cp /mnt/c/Users/shiva/.gitconfig .
cp /mnt/c/Users/shiva/.gitignore_global .

SSH Key and Known Host

cd ~
mkdir .ssh
cd .ssh
cp /mnt/c/Users/shiva/.ssh/id_ed25519* .
# Fix **WARNING: UNPROTECTED PRIVATE KEY FILE!**
chmod 600 * # Check the permission with `stat -c "%a %n" *`

Installing Python

Python is probably the easiest to install.

sudo apt install python3
sudo apt-get -y install python3 python3-dev python3-pip python3-venv libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev

Installing Node

Node is almost as easy to install, let's use nvm for that inevitable day where we have to support mutliple versions of Node.

  1. Make sure your Linux distribution is ready to go run an update and an upgrade:
sudo apt update
sudo apt -y upgrade
  1. Install curl which allows you to transfer data and download additional dependencies:
sudo apt-get install curl
  1. After it finishes installing, download the latest nvm version:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
  1. Confirm this has worked. The output should be a version number.
source ~/.bashrc
nvm --version
  1. Set latest Node.js version:
nvm install --lts

You should now have node and npm installed. To update node just run the command again.

nvm use --lts

Installing PHP

sudo apt-get install -y zip unzip rsync curl php php-cli php-curl php-dev php-mysql php-mbstring php-xmlrpc php-xdebug

Installing Composer

curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer

Installing WP-CLI

curl -SO# https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info

Setup Aliases

# Python
echo "alias python='python3'" >> ~/.bash_aliases

# Laravel
echo "alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'" >> ~/.bash_aliases
echo "alias sail='alias dep='vendor/bin/dep'" >> ~/.bash_aliases
source ~/.bash_aliases

Aliasing python3 to python can be dicey if you install something that expects python to be Python 2. Other alias are helpful for Laravel projects.

# Make sure your Linux distribution is ready to go run an update and an upgrade:
sudo apt update
sudo apt -y upgrade
# Init .hushlogin
touch ~/.hushlogin
# Install python.
sudo apt install python3
sudo apt-get -y install python3 python3-dev python3-pip python3-venv libxml2-dev libxslt1-dev zlib1g-dev libffi-dev libssl-dev
# Remove dir background in ls -color output.
dircolors -p | sed 's/;42/;01/' > ~/.dircolors
source ~/.bashrc
# Install git
sudo apt-get update
sudo apt-get install git
cd ~
cp /mnt/c/Users/shiva/.gitconfig .
cp /mnt/c/Users/shiva/.gitignore_global .
mkdir .ssh
cd .ssh
cp /mnt/c/Users/shiva/.ssh/id_ed25519* .
# Fix **WARNING: UNPROTECTED PRIVATE KEY FILE!**
chmod 600 * # Check the permission with `stat -c "%a %n" *`
# Install curl which allows you to transfer data and download additional dependencies.
sudo apt-get install curl
# Install Node.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
source ~/.bashrc
nvm install --lts
nvm use --lts # Update node
# Install PHP.
sudo apt-get install -y zip unzip rsync curl php php-cli php-curl php-dev php-mysql php-mbstring php-xmlrpc php-xdebug
# Install Composer.
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer
# Install WP-CLI.
curl -SO# https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar
chmod +x wp-cli.phar
sudo mv wp-cli.phar /usr/local/bin/wp
wp --info
# Setup the aliases.
echo "alias python='python3'" >> ~/.bash_aliases
echo "alias sail='[ -f sail ] && bash sail || bash vendor/bin/sail'" >> ~/.bash_aliases
echo "alias sail='alias dep='vendor/bin/dep'" >> ~/.bash_aliases
@shivapoudel
Copy link
Author

shivapoudel commented May 28, 2020

Turn on Windows Features:

dism.exe /online /enable-feature /FeatureName:Containers-DisposableClientVM /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

@shivapoudel
Copy link
Author

shivapoudel commented May 28, 2020

clip.exe < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

For copying ssh key in Github on WSL2.

@shivapoudel
Copy link
Author

shivapoudel commented Jun 28, 2021

export WP_TESTS_DIR="/mnt/c/Users/shiva/wordpress-develop/tests/phpunit"

Ref: https://github.com/WordPress/wp-notify/blob/develop/docs/contributors/develop.md

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