×

@abiramen

Projects Posts
13 April 2020 (Updated 29 January 2021)
Setting up WSL and GitHub (archive)
Start coding locally with the Windows Subsystem for Linux, and learn some basic Git to store copies of your work.
Tags:
keyboard_arrow_down
This post is no longer up to date. I generally now advise most people use WSL 2. I may or may not write an updated version of this post in the future.

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!

Coding locally on your Windows 10 computer with WSL

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.

Installing WSL1

You need to be on at least Windows 10 version 1803 to use WSL. You can check your Windows version by opening Search and entering winver. If you need to update Windows, go here.
  1. Open PowerShell. Type in PowerShell into Windows Search, right click on the result and click Run as administrator. Select Yes if a prompt shows up.
  2. Run the following command:
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
    If you are prompted to restart your computer, do so.
  3. Open the Microsoft Store. You can again do this using Windows Search.
  4. Search for Ubuntu and install it. Ubuntu currently has the best support for WSL.
  5. Once installed, open Ubuntu. You can do this by opening Search and typing in bash. Once you do this, a terminal should open!
  6. You'll be guided through setting up an account. Make sure you remember what password you use.
  7. We can now install a C compiler. We'll be using gcc. You can use it just like dcc, but you'll get less helpful error messages. To install, run the following command:
    sudo apt-get install build-essential
    Enter your password when prompted.
  8. Run the following command to make sure everything is up to date.
    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.

Installing WSL2

You need to be on at least Windows 10 version 1903 to use WSL2. If you aren't, you should probably use WSL1.
  1. Follow Step 1 and 2 from the above section.
  2. Run dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart in the same PowerShell window.
  3. Download and run this installer from Microsoft.
  4. Run wsl --set-default-version 2 in PowerShell.
  5. Follow Step 3 onwards from the above section.

Why is WSL2 seemingly eating my RAM?

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:

Installing dcc

Your mileage may vary, but you can install dcc locally! Run the following commands:

  1. 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
    curl is a utility in Linux to download files from the internet. You may also see wget sometimes, which is very similar.
  2. sudo apt install /tmp/dcc_2.6.4_all.deb
    This installs the package we just downloaded.

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.

Very optional but very cool: Installing Windows Terminal

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!

Windows Terminal
You need to be on at least Windows 10 version 1909 for this to work. You can check your Windows version by opening Search and entering winver. If you're need to update Windows, go here.
  1. Open the Microsoft Store.
  2. Search for Windows Terminal and install it. Once installed, you can open it by entering wt or searching Windows Terminal in Search.
  3. If you want, you can adjust settings. If you know how to read a JSON file, use Ctrl+, to access settings. You can even find themes here.

Setting up Git and GitHub

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.

Signing up for GitHub

Every developer has a GitHub account. GitHub provides servers for you to host your code, using Git.

  1. Sign up for GitHub here. This shouldn't take more than a minute
  2. Get the GitHub Education Pack! GitHub Education is really handy. Students can get GitHub Pro, as well as heaps of other useful tools for free. Visit education.github.com to sign up!
  3. Follow someone on GitHub for the first time! You can score bonus points by following me here :)
  4. Create your first repository here. Think of a repository (or repo for short) as somewhere you store all your code for a project. Perhaps call your repo hello-world.

Note: If you're ever using GitHub for coursework, make sure that you make your repo private to prevent plagiarism.

Installing Git

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.

For Ubuntu/Debian (including WSL):

Run sudo apt-get install git from your terminal.

For Windows and macOS:

Download Git from here, and follow the instructions to install.

Setting up Git on each machine

  1. Telling Git your name. Run git config --global user.name "yourusername" - I usually just use my GitHub username here.
  2. Telling Git your email. Run 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'.

Making use of our new repo

Now, we want to make use of our repo, and push code to it.

  1. Create a folder called HelloWorld somewhere on your computer.
  2. Open a terminal and use cd until you're in your new folder.
  3. Initialise a local repository using the command git init. This means you've set up a repository within your own computer, which tracks changes.
  4. Make a hello_world.c file in the folder. Here's some code for you to steal.
    #include <stdio.h>
    int main(void) {
        printf("Hello world!");
        return 0;
    }
    
  5. Run 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.
  6. Run 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!).
  7. Now, we want to sync our local repository with our remote repository on GitHub. The first time we do this, we have to tell git where our remote repository is. We do this with git remote add origin https://github.com/your-username/your-repo-name.git
  8. We can finally push the change to our remote repository. Use 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.

Using git to work across multiple computers

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.

Git

Using SSH keypairs to push to GitHub

Part 1: Generating a keypair

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.

Part 2: Adding your public key to GitHub

  1. Open the GitHub Key Settings page. You can find it here. Press Add SSH key.
  2. Give the key a name to describe what computer it is from. This is so that you don't get mixed up if you add more computers.
  3. Paste the public key you copied earlier. Press Add when you are done.
GitHub

Part 3: Updating your local repositories

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.

SSH key for a repo