---
# System prepended metadata

title: Create VM on ComputeEngine
tags: [' CoderSchool', ' GCP', tensorflow]

---

---
title: Create VM on ComputeEngine
tags: tensorflow, CoderSchool, GCP
---

# How to create VM on GCE with Deep learning platform

![](https://course.fast.ai/images/gcp/logo.jpg)

## Step 1: Creating your account
Cloud computing allows users access to virtual CPU or GPU resources on an hourly rate, depending on the hardware configuration. Find more information in the [Google Cloud Platform documentation](https://cloud.google.com/compute/). In case you don’t have a GCP account yet, you can create one here, which comes with $300 worth of usage credits for free.

> Potential roadblock: Even though GCP provides a $300 initial credit, you must enable [billing](https://console.cloud.google.com/billing/) to use it. You can put a credit card or a bank account but the latter will take several days for the activation.

The project on which you are going to run the image needs to be linked with your billing account. For this navigate to the [billing dashboard](https://console.cloud.google.com/billing/projects), click the ‘…’ menu and choose ‘change billing account’.

## Step 2: Install Google CLI
To create then be able to connect to your instance, you’ll need to install Google Cloud’s command line interface (CLI) software from Google. 

**For Windows user:** 

*Option 1*: Install Google SDK on Windows so you can have the most compatible experience with GCP services: 

1) Download Google Cloud SDK installer: https://dl.google.com/dl/cloudsdk/channels/rapid/GoogleCloudSDKInstaller.exe

2) Follow the instructions how to install that SDK: https://cloud.google.com/sdk/docs/quickstart-windows

3) After those two steps, you should be able to find Google Cloud SDK and open it

*Option 2*: You can use the [Ubuntu terminal](https://course.fast.ai/terminal_tutorial.html) and follow the same instructions as Ubuntu or MacOS users (see the link to learn how to paste into your terminal).

**For Ubuntu/MacOS user:** follow these four steps to install SDK:

```
# Create environment variable for correct distribution
export CLOUD_SDK_REPO="cloud-sdk-$(lsb_release -c -s)"

# Add the Cloud SDK distribution URI as a package source
echo "deb http://packages.cloud.google.com/apt $CLOUD_SDK_REPO main" | sudo tee -a /etc/apt/sources.list.d/google-cloud-sdk.list

# Import the Google Cloud Platform public key
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

# Update the package list and install the Cloud SDK
sudo apt-get update && sudo apt-get install google-cloud-sdk
```

You can find more details on the installation process [here](https://cloud.google.com/sdk/docs/quickstart-debian-ubuntu)

To install Google CLI on MacOS, in the terminal run

```
curl https://sdk.cloud.google.com | bash
exec -l $SHELL
```

**For Windows/MacOS/Ubuntu user:** After installing SDK, once the installation is done run this line
```
gcloud init
```

You should then be prompted with this message:

```
To continue, you must log in. Would you like to log in (Y/n)?
```

Type Y then copy the link and paste it to your browser. Choose the google account you used during step 1, click ‘Allow’ and you will get a confirmation code to copy and paste to your terminal.

Then, if you have more than one project (if already created on your GCP account), you’ll be prompted to choose one:

```
Pick cloud project to use:
 [1] [my-project-1]
 [2] [my-project-2]
 ...
 Please enter your numeric choice:
 ```

Just enter the number next to the project you created on step 1. If you select the choice “Create a new project”, you will be reminded you also have to run “gcloud projects create my-project-3”.

Lastly, you’ll be asked if you want to choose a default region, choose `us-west1-b` if you don’t have any particular preference (I chosen `asia-east1-c`), as it will make the command to connect to this server easier.

Once this is done, you should see this message on your terminal:

```
Your Google Cloud SDK is configured and ready to use!

* Commands that require authentication will use your.email@gmail.com by default
* Commands will reference project `my-project-1` by default
Run `gcloud help config` to learn how to change individual settings

This gcloud configuration is called [default].
```

## Step 3: Create an instance

Follow this instructions:

https://cloud.google.com/deep-learning-vm/docs/tensorflow_start_instance

This is my config:

**For Windows:**
```
set IMAGE_FAMILY="tf2-latest-gpu"
set ZONE="asia-east1-c"
set INSTANCE_NAME="deeplearning-instance-tf2"
  
gcloud compute instances create %INSTANCE_NAME% --zone=%ZONE% --image-family=%IMAGE_FAMILY% --image-project=deeplearning-platform-release --maintenance-policy=TERMINATE --accelerator="type=nvidia-tesla-v100,count=1" --metadata="install-nvidia-driver=True" --boot-disk-size=200GB --machine-type=n1-highmem-8 --tags=http-server,https-server --preemptible
```


**For MacOS/Ubuntu:**

```
export IMAGE_FAMILY="tf2-latest-gpu"
export ZONE="asia-east1-c"
export INSTANCE_NAME="deeplearning-instance-tf2"
  
gcloud compute instances create $INSTANCE_NAME \
  --zone=$ZONE \
  --image-family=$IMAGE_FAMILY \
  --image-project=deeplearning-platform-release \
  --maintenance-policy=TERMINATE \
  --accelerator="type=nvidia-tesla-v100,count=1" \
  --metadata="install-nvidia-driver=True" \
  --boot-disk-size=200GB \
  --machine-type=n1-highmem-8 \
  --tags=http-server,https-server \
  --preemptible
```

If you get an error saying:
```
ERROR: (gcloud.compute.instances.create) Could not fetch resource:
 - Quota 'GPUS_ALL_REGIONS' exceeded. Limit: 0.0 globally.
 ```

You need to adjust your GPU quotas.

* Go to [Google Cloud Quotas Page](https://console.cloud.google.com/iam-admin/quotas).
* If you signed up with a free tier account, you first need to upgrade to a paid account; do so by clicking the “Upgrade account” button at the top right of the page. This won’t affect your $300 credit.
* In filter type, select metric to be GPUs (all regions) and Location as Global.
* Click edit quotas and select the quota to edit (GPUs All Regions). Set the new quota limit to 1 or more. 

Your request may require confirmation, which Google claims typically takes two business days to get.
You will have to wait a little bit until you see the text informing you the instance has been created. You can see the instance online [here](https://console.cloud.google.com/compute/) in your list of instances (note that this will be the page you have to go to later to stop your instance).

Once this is done, you can connect to your instance from the terminal by typing:

**For Windows**:
```
set IMAGE_FAMILY="tf2-latest-gpu"
set ZONE="asia-east1-c"
set INSTANCE_NAME="deeplearning-instance-tf2"
gcloud compute ssh --zone=%ZONE% jupyter@%INSTANCE_NAME% -- -L 8080:localhost:8080
```

**For Ubuntu/MacOS**:
```
export IMAGE_FAMILY="tf2-latest-gpu"
export ZONE="asia-east1-c"
export INSTANCE_NAME="deeplearning-instance-tf2"
gcloud compute ssh --zone=$ZONE jupyter@$INSTANCE_NAME -- -L 8080:localhost:8080
```

Before you are able to connect, Google Cloud may ask you to create an SSH key. Just follow the prompts (the passphrase is optional, if you aren’t going to be using this key for anything too secure).
If everything went ok, you should now be connected to your GCP instance! To use it, simply go to localhost:8080/tree and you will find yourself in a jupyter notebook environment. Note that this only works while you maintain the ssh connection in your terminal. Note: GCP also installs a system called “JupyterLab”, which is still in beta, and doesn’t support everything in the courses; therefore, you need to use “Jupyter Notebook”, which is what the link above will take you to.

### Preemptible instances:

Please note we are running a preemptible instance (notice the ‘–preemptible’ parameter in our command). A preemptible GCP instance is cheaper than traditional instances but it has two main disadvantages:
1. It can be preempted (stopped) with a 30 second notice at any time due to high demand.
2. It will always be stopped after 24 hours of continuous running.
If your instance is stopped, your saved data will be kept safe but if you are running a model, the progress will be lost.
These characteristics make preemptible instances a nice option for beginners, since you will not be running very deep models that take days to run. If however, you do need to run models without interruption for long periods of time, you can always call the same command but skip the ‘–preemptible’ parameter. Take into account this will increase your costs to about $1.29 an hour.

## Step 5: Stop an instance

You will be charged if you don’t stop the instance while it’s ‘idle’ (e.g. not training a network). To stop an instance out of Google Cloud’s online interface go here, click the ‘…’ icon to the right of the instance and choose ‘Stop’ or click the stop button at the top of the screen.

## DEBUG

### (gcloud.compute.ssh) could not parse resource []

You may need to permanently store those environment variables in your `~/.bash_profile`. Copy your config
```
export IMAGE_FAMILY="tf2-latest-gpu"
export ZONE="asia-east1-c"
export INSTANCE_NAME="deeplearning-instance-tf2"
```
Now paste the environment variables that you copied above at the end of the file `~/.bash_profile` and save it.

To make sure the changes take effect immediately, run the command `source ~/.bash_profile`.

Run your `gcloud compute ssh --zone=$ZONE jupyter@$INSTANCE_NAME -- -L 8080:localhost:8080` and now you should be good to go.