If you're a UNSW student, there's a good chance that you're here from my CSE setup guide for vscode. This is going to be a short guide to setting up WSL, which provides you with a Linux environment within Windows. To go through the WSL section, you'll need to be on Windows 10. If you're here just to learn some basic Git, you can click here to skip all the Windows stuff, free of charge!
Windows 10 now supports running Linux distributions from within it. We can set this up so that we can easily
use a lot of the Linux commands and scripting which is taught in UNSW CSE degrees. You can even use your new Linux
setup to go on and learn other languages over breaks, such as Rust.
Before we proceed, there's two versions of WSL that you can use - WSL1 and WSL2. For most people, especially those
of you that are relatively new to programming, WSL1 is the best option. WSL2 is still a bit rough around the edges,
and you can always upgrade later. WSL2 also doesn't play very nicely with Type 2 Hypervisors such as VirtualBox and vmware, since it uses Hyper-V - this will restrict you to using Hyper-V for any virtual machines that you decide to make. If you're someone that uses Docker or really
needs native Linux GUIs, click here for instructions on how to install WSL2.
winver
.
If you need to update Windows, go
here.
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-LinuxIf you are prompted to restart your computer, do so.
bash
. Once you do this, a terminal should open!
sudo apt-get install build-essentialEnter your password when prompted.
sudo apt update && sudo apt upgrade
You should now be able to use the text based Linux commands you learn during the course, replacing dcc with
gcc. (Note: If you've learnt about dcc --leak-check
, it won't work with gcc.)
If you use VSCode, you can use code [filename]
to open a file. If you use Vim, Vim will work as
normal. However, gedit won't work. Google 'wsl x server' if you're really desperate to use it.
If you want a prettier terminal with more features to use with WSL, click here to jump to those instructions. If you want to install dcc, click here.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
in the same PowerShell window.
wsl --set-default-version 2
in PowerShell.
If you have WSL2 running for a while, you might see a process called Vmmem in Task Manager eating up all your RAM. You can be relieved to know that this isn't the result of malware, but WSL2 using memory for cache. However, this isn't an issue whatsoever - if another process on your computer needs more RAM, WSL2 will reduce its cache usage - in fact, Windows itself uses any remaining unused RAM as cache to speed up your computer, because unused RAM is wasted RAM.
If this RAM usage still somewhat irrationally annoys you, you can do one of the following:
sudo sh -c \"echo 3 >'/proc/sys/vm/drop_caches' && swapoff -a && swapon -a && printf '\n%s\n' 'Cached RAM and Swap Cleared'\"
%userprofile%
in File Explorer (by entering it into the address bar) and creating a file called .wslconfig and entering
the following:
[wsl2] memory=4096MBThis restricts WSL2 to 4GB of RAM. If you're running Docker containers from WSL2, I encourage you to let WSL2 use at least half your system memory - I have mine at 12GB, or 60%. If you don't know what Docker is, you can just ignore this.
Your mileage may vary, but you can install dcc locally! Run the following commands:
curl -L https://github.com/COMP1511UNSW/dcc/releases/download/2.6.4/dcc_2.6.4_all.deb -o
/tmp/dcc_2.6.4_all.deb
sudo apt install /tmp/dcc_2.6.4_all.deb
Now, try compiling and running a program. If this doesn't work, run the command
sudo bash -c "echo 0 > /proc/sys/kernel/yama/ptrace_scope;echo 1 > /proc/sys/vm/overcommit_memory"
,
but only do this if you really need to.
Note that you should still follow the steps to install build-essential from earlier because it has a lot of other useful tools.
This part is very very optional, but installs Windows Terminal, which allows you to access cmd, powershell and bash in one place, open multiple terminals (even side-by-side), and most importantly, use themes!
winver
.
If you're need to update Windows, go here.
wt
or searching Windows Terminal in Search.Git is a really useful tool which allows you backup your code, revert to older versions, keep track of changes, update and pull your code from multiple devices, and provides several tools to allow you to work collaboratively. This is called version control.
Every developer has a GitHub account. GitHub provides servers for you to host your code, using Git.
Note: If you're ever using GitHub for coursework, make sure that you make your repo private to prevent plagiarism.
If you're working on VNC, or with the CSE machines over SSH, Git is already installed on these machines! Otherwise, you'll need to install Git.
Run sudo apt-get install git
from your terminal.
Download Git from here, and follow the instructions to install.
git config --global user.name "yourusername"
-
I
usually just use my GitHub username here.git config --global user.email
"name@example.com"
- I usually use the private GitHub email ending with
@users.noreply.github.com,
which can be found here under 'Keep email addresses
private'.Now, we want to make use of our repo, and push code to it.
cd
until you're in your new folder.git init
. This
means you've set up a repository within your own computer, which tracks changes.#include <stdio.h> int main(void) { printf("Hello world!"); return 0; }
git add hello_world.c
. This tells git that you want changes to the
file to be
included in the next update. If you ever have multiple files that you want to update, use
git add --all
.git commit -m "Initialise repo"
. This keeps track of the update,
noting what changes you've made, and stores it on the local copy of the repository. Your commit
message should always describe the changes you've made (in present tense!).git remote add origin https://github.com/your-username/your-repo-name.git
git push
.You should now see your hello_world.c file at https://github.com/your-username/your-repo-name!
You should
always commit and push your code whenever you've
made a notable change, so that you can revert your code if you ever have problems later on. Note that
git will
ask for your GitHub password each time you push. We can avoid this by setting up an SSH
keypair, which you can scroll down to read about.
We can make a copy of our hello-world repository on another computer using
git clone https://github.com/your-username/your-repo-name.git
. This sets up a new local
repository with the contents
from the remote repository. Now, we can work on our code on this new computer, and use git add, commit and
push
to update our GitHub repository just like before. Now, when we return to our
original computer, we run
git pull
to fetch updates from the remote repository. as soon as we start. This is very
important to do
- if you don't, and realise too late, you'll have to deal with resolving what's called a merge conflict.
There is much, much more to Git. If you want to learn more about Git, and you're a UNSW student, take COMP1531, which teaches you all about Git, especially in the context of teamwork. You can also learn from the slides for this CSESoc workshop.
You'll first need to generate a keypair. If you haven't done this, check out the guide in my other post. If you have, you can use the same keypair.
If you already have any local repositories, you need to update them to use the SSH Git path. To do this,
cd
to the directory, and enter
git remote set-url origin git@github.com:your-username/your-repo-name
.
If you clone or set
up any new repositories, you will also have
to use the SSH address, i.e. git clone git@github.com:your-username/your-repo-name
. If you're
ever unsure as to the SSH path, you can check
on GitHub.