Complete Tutorial: Setting Up a Remote Development Environment for C++ on Ubuntu 24.04

Alternate version in Python

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

  1. Update the Server:
    sudo apt update && sudo apt upgrade -y
  2. Install Basic Tools:
    sudo apt install -y build-essential git curl

1.2. Configure SSH Access

On Your Laptop:

  1. 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).
  2. Copy the Public Key to the Server:
    ssh-copy-id username@server_ip
  3. Test the Connection:
    ssh username@server_ip You should be logged into the server without a password.

1.3. Install VS Code and Remote Extensions

On Your Laptop:

  1. Install VS Code:
  2. Install the Remote-SSH Extension:
    • Open VS Code.
    • Go to the Extensions view (Ctrl+Shift+X or Cmd+Shift+X on macOS).
    • Search for Remote - SSH and install it.
  3. Configure the Remote Connection:
    • Press Ctrl+Shift+P (Cmd+Shift+P on macOS) and type Remote-SSH: Add New SSH Host.
    • Enter the connection string:
      username@server_ip
    • Select the SSH configuration file location (e.g., ~/.ssh/config).
  4. 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

  1. Install the GitHub Copilot Extension:
    • In VS Code, search for “GitHub Copilot” in the Extensions view and install it.
  2. Sign In to GitHub:
    • Follow the prompts to authenticate GitHub Copilot.
  3. Test Copilot:
    • Open a new file and type a comment like // Function to add two numbers. Observe how Copilot suggests code snippets.

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

  1. Navigate to the Workspace:
    mkdir -p ~/projects/alice && cd ~/projects/alice
  2. 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

  1. 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”.
  2. Create a File: In VS Code, create a file named main.cpp. (control click on explorer, then pick “new file…”)
  3. 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;
}
  1. 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

  1. Create the Makefile: Create a file named Makefile and 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
  1. Build the Program: Run the following command to compile the program:
    make
  2. 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

  1. Check for an Existing SSH Key: On your remote server, check if an SSH key already exists:
    ls ~/.ssh/id_rsa.pub If a key exists, skip to Step 3. Otherwise, continue to Step 2.

  1. 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).

  1. Add the SSH Key to Your GitHub Account:
    1. Copy the SSH public key to your clipboard:
      cat ~/.ssh/id_rsa.pub Copy the output starting with ssh-rsa.
    2. Log in to GitHub and go to Settings > SSH and GPG keys:
    3. Click New SSH Key:
      • Title: Enter a name like “Remote Server Key”.
      • Key: Paste the copied SSH key.
      • Click Add SSH Key.
  2. 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

  1. Log in to GitHub: Visit https://github.com/ and log in.
  2. 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

  1. Navigate to the Project Directory on the remote server:
    cd ~/projects/alice
  2. Initialize Git (if not done earlier):
    git init
  3. 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

  1. Stage All Files: Add all project files to the staging area: (make clean removes .o and executable)
    make clean
    git add .
  2. 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

  1. Push the Code to the Repository:
    git branch -M main
    git push -u origin main
  2. 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

  1. Visit Your Repository: Open your browser and go to https://github.com/<username>/alice-word-count.
  2. Check Files: Ensure all project files, including main.cpp, alice.txt, and Makefile, are uploaded.

7. Add a README File

  1. Create a README File: Add a README.md file 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.md
    git add README.md
    git commit -m "Add README file"
    git push
  2. Customize the README: Open the README.md file in VS Code and describe the project in detail, including how to build and run it.

8. Collaborate on GitHub

  1. Share the Repository: Share the GitHub link with collaborators or instructors.
  2. Pull Changes: To sync updates from the repository:
    git pull origin main
  3. 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!

Scroll to Top