Using gcloud for Virtual Machines

After this section, you will be able to achieve the following outcomes:

  • Understand the concept of Virtual Machines (VMs): Learn what a VM is and how it operates on a host operating system.
  • Set up a Google Cloud project using gcloud: Create a Google Cloud project and enable billing for it.
  • Create a Virtual Machine instance: Configure and deploy a VM on Google Cloud using the Ubuntu 22.04 LTS operating system.
  • Install and initialize the gcloud CLI: Set up the gcloud command interface on your local machine for managing cloud resources.
  • Connect to the VM using SSH: Establish a secure connection to your VM via the command line from your local machine.
  • Update and manage the VM: Perform essential updates on the VM and create snapshots for backup and recovery.

Virtual Machines

Our goal in this section is to create a virtual machine (VM) instance running a distribution of the Linux operating system. A VM is a virtualized operating system that runs on a host operating system. That host operating system may also be Linux, but it could be Windows or macOS. In short, using virtual machines means instead of installing an operating system on bare metal, we use virtual machine software to mimic the process of installing an additional OS as an app (so to speak) on an existing OS.

There are many ways to do virtualization. According to Red Hat, there is data virtualization, desktop virtualization, server virtualization, OS virtualization, (which is the technology we cover here), and network functions virtualization.

In this book, we're going to use Google Cloud to create and run our virtual machines. There are other options available that you can explore on your own.

Google Cloud / gcloud

Google Account

We need a Google account to create our virtual machines. I imagine you already have a Google account, but if not, then create one at https://www.google.com. Be sure to use your personal Google account for this project.

Google Cloud (gcloud) Project

After signing into Google:

  1. Create a Google Cloud project.
  2. Enable billing for that project.
  3. Create a VM (or virtual instance) on Google Cloud.
  4. Install the gcloud CLI software on our personal machines to connect to our remote VM;
    • alternatively, use the web interface (details below) to connect to our remote VM.

Although I include most of the instructions on this page to perform the above steps, it is imperative that you read through Google's instructions also.

Create a Project

After signing into your Google account, proceed to Step 1 at the top of the Install the gcloud CLI page to create a new project. Review the page on creating and managing projects.

When you create your project, you can name it anything, but try to name it something to do with this project. E.g., I might use the name sysadmin-418. Avoid spaces in the names of your project. Click on the Create button, and leave the organization field set to No Organization. Make sure you've selected your project, and then click on Enable for Compute Engine API.

Google Billing

Second, set up a billing account for your gcloud project. This means there is a cost associated with this product, but the machines we'll build require few resources and the cost should be minimal. In the past, I usually pay about $1 per month. Follow Step 2 to enable billing for the new project. See also the page on how to create, modify, or close your self-serve Cloud Billing account.

gcloud VM Instance

Next, log into Google Cloud Console. This should take you to the Dashboard page.

Our first goal is to create a virtual machine (VM) instance. As a reminder, a VM is a virtualized operating system. We will use software to mimic the process of installing an operating system on Google's servers.

Google Cloud offers a number of Linux-based operating systems to create VMs. We're going to use the Ubuntu operating system and specifically the Ubuntu 22.04 LTS version. We are not going to install a graphical user interface on our Ubuntu servers. Rather, we will focus on using the command line to do most of our work.

What is Ubuntu? Ubuntu is a distribution of Linux. A new version of Ubuntu is released every six months. The 22.04 signifies that this is the April 2022 version. The LTS signifies Long Term Support. LTS versions are released every two years, and Canonical LTD, the owners of Ubuntu, provide five years standard support for LTS versions. Thus, Ubuntu 22.04 is supported through June 2027.

LTS versions of Ubuntu are more stable than non-LTS versions of Ubuntu. The latter receive nine months of standard support and generally use cutting edge technology. Cutting edge technology is not always desirable for server operating systems, which often prioritize stability. Each version of Ubuntu has a code name. Ubuntu 22.04 LTS has the code name Jammy Jellyfish. You can see a list of versions, code names, release dates, and more on Ubuntu's Releases page.

You should be on the page where you create a new VM, but if not

  • If Click the three horizontal bars at the top left of the screen.
  • Hover over the Compute Engine link, and then select VM Instances.
  • In the window, select the project that you created earlier.
    • E.g., for me, I used the project name sysadmin-418.
  • Next, click on Create Instance.
  • Change the name for your instance.
    • E.g., I chose fall-2025 (no spaces)

If you are already on the Create VM page, then:

  • Change the name for your instance.
    • E.g., I chose fall-2025 (no spaces)
  • Use default Region and Zone.
  • Make sure E2 (Low cost, day-to-day computing) is selected.
  • Under the Machine type drop down box, select e2-micro (0.25-2 vCPU (1 shared core), 1 GB memory)
    • This is the lowest cost virtual machine and perfect for our needs.

Next, click on the OS and storage link in the left hand navigation section.

  • Click on the Change button.
  • Under Operating system, select Ubuntu.
  • Under Version, select Ubuntu 22.04 LTS x86/64
  • Leave Boot disk type be set to Balanced persistent disk
  • Disk size should be set to 10 GB.
  • Click on the Select button.

Next, click on the Networking link in the left hand navigation section.

  • Check the Allow HTTP Traffic button
  • Finally, click on the Create button to create your VM instance.

Install the latest gcloud CLI version

In this section, we install the gcloud CLI software to connect to our virtual machines on Google Cloud. Using the gcloud CLI is a more advanced way to connect to our VMs. If you prefer, you can connect to your VM using Google's web interface. Skip to the Connect To Our VM section if you prefer the web interface.

Using the gcloud CLI will allow us to connect to remote server using our own terminal applications. The Install the gcloud CLI page provides instructions for different operating systems.

There are installation instructions for macOS, Windows, Chromebooks, and various Linux distributions. Follow these instructions closely for the operating system that you're using.

Note that for macOS, you have to choose among three different CPU/chip architectures. If you have an older macOS machine (before November 2020 or so), it's likely that you'll select macOS 64-bit (x86_64). If you have a newer macOS machine, then it's likely you'll have to select macOS 64-bit (arm64, Apple M1 silicon). It's unlikely that any of you are using a 32-bit macOS operating system. If you're not sure which macOS system you have, then let me know and I can help you determine the appropriate platform. Alternatively, follow these instructions to find your processor information:

  • click on the Apple menu
  • choose About This Mac
  • locate the Processor or Chip information

After you have downloaded the gcloud CLI for your particular OS and CPU architecture, you will need to open a command prompt/terminal on your machines to complete the instructions that describe how to install the gcloud CLI. macOS uses the Terminal app, which can be located using Spotlight. Windows users can use the Command Prompt or Powershell.

Windows Users

Windows users will download a regular .exe file, and launch the installer in the regular Windows way. Please follow the rest of the instructions for Windows.

macOS Users

macOS users may need to complete some setup work before installing Google Cloud. First, open your Terminal.app and run the following code:

xcode-select --install

Once the Xcode developer tools are installed, you need to install the macOS Homebrew package manager. To do so, follow the instructions here:

Homebrew

After Homebrew is installed use the brew command to install pyenv.

brew install pyenv

And then use pyenv to install the latest version of Python. For example, to install the latest release of Python (as of August 2024):

penv install 3.12.5

Finally, you can install the Google Cloud application using the steps outlined below. Or you can use the steps outlined in the Google Cloud Interactive installation.

See also:

Setting up a Python development environment

macOS users will download a .tar.gz file and extract the .tar.gz using the tar command. The .tar.gz file may have been downloaded to your Downloads folder. First, in your Terminal.app, move that file to your home directory and extract it there. Once extracted, change to home directory with the cd command. For example, if you are running macOS and downloaded the x86_64 version of the gcloud CLI, then open your Terminal.app and run the following commands:

mv ~/Downloads/google-cloud-cli-darwin-x86_64.tar.gz $HOME
cd $HOME
tar -xzf google-cloud-cli-444.0.0-darwin-x86_64.tar.gz 
cd google-cloud-sdk

Modify the file names in the commands above, as appropriate, if you're using the M1 version of the gcloud CLI.

Initializing the gcloud CLI

Regardless if you're using macOS or Windows, you will now initialize your Google Cloud installation the same way. First, scroll down the install page to the section titled Initializing the gcloud CLI. In your terminal, run the initialization command. Per the instructions at the above page, it should be something like so:

gcloud init

And continue to follow the instructions in the documentation.

Connect to our VM

Using gcloud CLI

After the new VM machine has been created, you connect to it via the command line. macOS users will connect to it via their Terminal.app. Windows users can connect to it via their Command shell or Powershell.

If you have used ssh before, note that we use a slightly different ssh command to connect to our VMs. The syntax follows this pattern:

gcloud compute ssh --zone "zone-info" "name-info" --project "project-id"

You need to replace the values in the double quotes in the above command with the values located in your Google Cloud console and in your VM instances section. You can select the SSH drop down box to copy the exact gcloud command to connect to your server.

Using the Web Interface

If you did not install gcloud CLI, then you can connect through the Google Cloud Console website. To do so:

  1. Click on the drop down arrow next to the SSH button for your VM instance.
  2. Select, Open in browser window.
  3. Authorize it to allow SSH-in-browser to connect to VMs.
  4. A terminal in a browser window should appear.

Update our Ubuntu VM

Once you have access to the command line on your remote Linux virtual instance, you will need to update your OS. The VM will include a recently updated version of Ubuntu 22.04, but it may not be completely updated. Thus the first thing we need to do is update our machines. Since this is an Ubuntu machine, use the following two commands to update your machines. Type the first command in, wait for it to complete, and then type the second command. READ the OUTPUT closely. It will ask you to continue. Press Y to continue (lowercase y is fine).

sudo apt update
sudo apt -y upgrade

You are done! To exit the VM, type the exit command:

exit

Typing exit at the prompt will always close our connection to our remote servers.

Snapshots

You have installed a pristine version of Ubuntu, but mistakes will happen while learning how to use your machines. If you want, you can backup this pristine version of the operating system. This will allow you to restore the server if something goes wrong later.

NOTE: It's pretty easy to create a new VM instance. Therefore, it's okay to create snapshots later when you are working on your projects. This will reduce costs until needed.

To get started:

  1. In the left hand navigation panel, click Compute Engine and then Snapshots.
  2. At the top of the page, click on Create Snapshot.
  3. Provide a name for your snapshot: e.g., ubuntu-1.
  4. Provide a description of your snapshot: e.g., This is a new install of Ubuntu 22.04.
  5. Choose your Source disk.
  6. Choose a Location to store your snapshot.
    • To avoid extra charges, choose Regional.
    • From the drop down box, select the same location (zone-info) your VM has
  7. Click on Create

Please monitor your billing for this to avoid costs that you do not want to incur.

Conclusion

Congrats! You have successfully completed your first Linux server installation. In this section, you learned about virtual machines (VMs) and created one using Google Cloud. Specifically, you created a Ubuntu 22.04 LTS Linux virtual machine. While this may seem like a lot of information, by the end of this book you will be able to effortlessly deploy and manage VMs for various tasks. Such tasks may include setting up websites and more.