This tutorial guides students through creating a remote development environment on an Ubuntu 24.04 server. The students will use their laptops to write and edit code on the remote server via Visual Studio Code (VS Code) and GitHub Copilot. They will also set up an SSH connection, configure the server with essential tools, and develop a C++ program that reads Alice in Wonderland, processes it, and outputs a sorted list of unique words with counts.
1. Setting Up the Environment
1.1. Prepare the Remote Server
- Update the Server:
sudo apt update && sudo apt upgrade -y - Install Basic Tools:
sudo apt install -y build-essential git curl
1.2. Configure SSH Access
On Your Laptop:
- Generate an SSH Key:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"- Save the key in the default location (
~/.ssh/id_rsa). - Press Enter for no passphrase (or set one if desired).
- Save the key in the default location (
- Copy the Public Key to the Server:
ssh-copy-id username@server_ip - Test the Connection:
ssh username@server_ipYou should be logged into the server without a password.
1.3. Install VS Code and Remote Extensions
On Your Laptop:
- Install VS Code:
- Download and install VS Code from https://code.visualstudio.com/.
- Install the Remote-SSH Extension:
- Open VS Code.
- Go to the Extensions view (
Ctrl+Shift+XorCmd+Shift+Xon macOS). - Search for
Remote - SSHand install it.
- Configure the Remote Connection:
- Press
Ctrl+Shift+P(Cmd+Shift+Pon macOS) and typeRemote-SSH: Add New SSH Host. - Enter the connection string:
username@server_ip - Select the SSH configuration file location (e.g.,
~/.ssh/config).
- Press
- Connect to the Remote Server:
- From the Remote Explorer view in VS Code, connect to the server.
- Once connected, VS Code will open a remote workspace.
1.4. Install GitHub Copilot
- Install the GitHub Copilot Extension:
- In VS Code, search for “GitHub Copilot” in the Extensions view and install it.
- Sign In to GitHub:
- Follow the prompts to authenticate GitHub Copilot.
- Test Copilot:
- Open a new file and type a comment like
// Function to add two numbers. Observe how Copilot suggests code snippets.
- Open a new file and type a comment like
2. Setting Up the Development Environment
2.1. Install Build Essentials
On the remote server, ensure the C++ build tools are installed:
sudo apt install -y build-essential
2.2. Create a Project Directory
- Navigate to the Workspace:
mkdir -p ~/projects/alice && cd ~/projects/alice - Initialize a Git Repository:
git init
3. Write the C++ Program with Copilot
3.1. Fetch Alice in Wonderland
Download the text file from Project Gutenberg:
curl -o alice.txt https://www.gutenberg.org/files/11/11-0.txt
3.2. Create the Main Program
- Open Project Folder on server: From VS Code open a connection to the server, then click on “Open Folder” and pick “projects” then “alice” then hit “ok”.
- Create a File: In VS Code, create a file named
main.cpp. (control click on explorer, then pick “new file…”) - Use Copilot to Assist with Writing the Code: Add JUST the code from below and let GitHub Copilot suggest implementations:
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <algorithm>
using namespace std;
// Function to read words from a file and count their occurrences
void processFile(const std::string& filename, std::map<std::string, int>& wordCounts);
// Function to print the sorted words and their counts
void printSortedWords(const std::map<std::string, int>& wordCounts);
int main() {
std::map<std::string, int> wordCounts;
// Process the file
processFile("alice.txt", wordCounts);
// Print sorted words and counts
printSortedWords(wordCounts);
return 0;
}
- Generate Functions Using Copilot: For each function comment, use Copilot to generate the implementation. For example:
void processFile(const std::string& filename, std::map<std::string, int>& wordCounts) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening file " << filename << std::endl;
return;
}
std::string word;
while (file >> word) {
// Remove punctuation
word.erase(std::remove_if(word.begin(), word.end(), ::ispunct), word.end());
// Convert to lowercase
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
// Increment the word count
wordCounts[word]++;
}
}
void printSortedWords(const std::map<std::string, int>& wordCounts) {
// Create a set of pairs to sort the words by count
std::set<std::pair<int, std::string>> sortedWords;
for (const auto& pair : wordCounts) {
sortedWords.insert(std::make_pair(pair.second, pair.first));
}
// Print the sorted words
for (const auto& pair : sortedWords) {
std::cout << pair.second << ": " << pair.first << std::endl;
}
}
3.3. Create a Makefile
- Create the Makefile: Create a file named
Makefileand use Copilot to help write it: (Make sure to use g++)
# Makefile for Alice in Wonderland word count program
CXX = g++
CXXFLAGS = -std=c++17 -Wall
all: alice
alice: main.o
$(CXX) $(CXXFLAGS) -o alice main.o
main.o: main.cpp
$(CXX) $(CXXFLAGS) -c main.cpp
clean:
rm -f alice *.o
- Build the Program: Run the following command to compile the program:
make - Run the Program: Execute the compiled binary:
./alice
4. Explore the Output
The program will output a sorted list of unique words from Alice in Wonderland along with their counts.
5. Debugging and Enhancements
- Using Copilot for Enhancements:
- Ask Copilot to add comments or refactor code.
- Use Copilot to implement additional features, such as ignoring common stop words.
- Testing:
- Modify the text file and test with smaller datasets.
This tutorial demonstrates a complete remote development workflow using VS Code, SSH, GitHub Copilot, and C++ on an Ubuntu server.
Additional Steps to Save the Project to GitHub with SSH Key Setup
This section will guide students on how to configure their SSH key for GitHub, set up a repository, and push the project to GitHub.
1. Set Up an SSH Key for GitHub
- Check for an Existing SSH Key: On your remote server, check if an SSH key already exists:
ls ~/.ssh/id_rsa.pubIf a key exists, skip to Step 3. Otherwise, continue to Step 2.
- Generate a New SSH Key: Create a new SSH key for your GitHub account:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"- Save the key in the default location:
~/.ssh/id_rsa. - Press Enter for no passphrase (or set one if desired).
- Save the key in the default location:
- Add the SSH Key to Your GitHub Account:
- Copy the SSH public key to your clipboard:
cat ~/.ssh/id_rsa.pubCopy the output starting withssh-rsa. - Log in to GitHub and go to Settings > SSH and GPG keys:
- Click New SSH Key:
- Title: Enter a name like “Remote Server Key”.
- Key: Paste the copied SSH key.
- Click Add SSH Key.
- Copy the SSH public key to your clipboard:
- Test the Connection: Back on your remote server, run:
ssh -T git@github.com
You should see a message like:Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
2. Create a GitHub Repository
- Log in to GitHub: Visit https://github.com/ and log in.
- Create a New Repository:
- Click New or visit https://github.com/new.
- Enter a repository name, e.g.,
alice-word-count. - (Optional) Add a description.
- Choose Public or Private.
- Do not initialize the repository with a README.
- Click Create repository.
3. Add the GitHub Repository to Your Project
- Navigate to the Project Directory on the remote server:
cd ~/projects/alice - Initialize Git (if not done earlier):
git init - Add the GitHub Repository as a Remote: Replace
<username>and<repository>with your GitHub username and repository name:git remote add origin git@github.com:<username>/alice-word-count.git
4. Stage and Commit the Project
- Stage All Files: Add all project files to the staging area: (make clean removes .o and executable)
make cleangit add . - Commit the Files: Write a commit message describing the work:
git commit -m "Initial commit: Add Alice word count project"
5. Push the Project to GitHub
- Push the Code to the Repository:
git branch -M maingit push -u origin main - Authenticate with SSH: If your SSH key is correctly configured, the push will proceed without asking for your password.
6. Verify the Code on GitHub
- Visit Your Repository: Open your browser and go to
https://github.com/<username>/alice-word-count. - Check Files: Ensure all project files, including
main.cpp,alice.txt, andMakefile, are uploaded.
7. Add a README File
- Create a README File: Add a
README.mdfile to describe the project:echo "# Alice Word Count Project" > README.md echo "This project reads Alice in Wonderland, counts unique words, and displays them sorted." >> README.mdgitadd README.mdgit commit -m "Add README file"git push - Customize the README: Open the
README.mdfile in VS Code and describe the project in detail, including how to build and run it.
8. Collaborate on GitHub
- Share the Repository: Share the GitHub link with collaborators or instructors.
- Pull Changes: To sync updates from the repository:
git pull origin main - Push Updates: When you make further changes, commit and push them using:
git add . git commit -m "Description of changes" git push
By following these steps, your project will be securely saved on GitHub, ready for collaboration and future updates. Let me know if you need further assistance!
