{"id":550,"date":"2021-03-28T10:19:32","date_gmt":"2021-03-28T08:19:32","guid":{"rendered":"https:\/\/nubisoft.io\/blog\/?p=550"},"modified":"2021-04-02T12:54:54","modified_gmt":"2021-04-02T10:54:54","slug":"setting-ubuntu-gpu-ec2-aws-ml-pytorch-1","status":"publish","type":"post","link":"https:\/\/nubisoft.io\/blog\/setting-ubuntu-gpu-ec2-aws-ml-pytorch-1\/","title":{"rendered":"Setting up Ubuntu on GPU EC2  in AWS for ML with PyTorch &#8211; part 1"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">In this post, we will show you how to configure your Ubuntu GPU EC2 instance in Amazon Web Services from start to finish so that you can perform Deep Learning tasks on it. At <a href=\"http:\/\/nubisoft.io\">NubiSoft<\/a>, we usually implement machine learning with less popular cloud service providers, because AWS is not the cheapest, and the learning process itself usually only requires a lot of computing power, but the availability of the system or the geographic location of calculations are of less importance here. Only when we obtain a trained model that we want to deploy to production (i.e. carry out inferencing), the latter factors become more important. But it so happened that the customer who uses AWS exclusively asked us to deploy his Deep Learning stack there. So here we go!<\/h2>\n\n\n\n<p>First, the new EC2 instance should be provisioned. This can be done using <a rel=\"noreferrer noopener\" href=\"https:\/\/aws.amazon.com\/cli\/\" target=\"_blank\">awscli<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/aws.amazon.com\/cloudformation\/\" target=\"_blank\">CloudFormation<\/a>, <a rel=\"noreferrer noopener\" href=\"https:\/\/learn.hashicorp.com\/tutorials\/terraform\/aws-build\" target=\"_blank\">Terraform<\/a>, and others but the most illustrative will be to do that with AWS console using the <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.aws.amazon.com\/AWSEC2\/latest\/UserGuide\/launching-instance.html\" target=\"_blank\">lunch instance<\/a> option, where we choose the plain Ubuntu 18.04 LTS <a href=\"https:\/\/docs.aws.amazon.com\/AWSEC2\/latest\/UserGuide\/AMIs.html\" target=\"_blank\" rel=\"noreferrer noopener\">AMI<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"261\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-1024x261.png\" alt=\"\" class=\"wp-image-556\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-1024x261.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-300x76.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-768x196.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-1536x391.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1-1200x306.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-AMI_1.png 1716w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Choosing Ubuntu Server 18.04 LTS 64-bit (x86)<\/figcaption><\/figure>\n\n\n\n<p>This tutorial is based on the <a rel=\"noreferrer noopener\" href=\"https:\/\/aws.amazon.com\/ec2\/instance-types\/p2\/\" target=\"_blank\">P2 GPU instance type<\/a>, so it has to be chosen as follow (if you prefer you can even choose a p2 instance with more computing capacity):<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"207\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-1024x207.png\" alt=\"\" class=\"wp-image-557\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-1024x207.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-300x61.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-768x156.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-1536x311.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2-1200x243.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-p2.xlarge_2.png 1767w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Choosing the p2.xlarge instance type<\/figcaption><\/figure>\n\n\n\n<p>Adjust the disk volume to the needs of your computation profile &#8211; e.g. Deep Learning related to image classification would probably require a lot of free disk space:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"325\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-1024x325.png\" alt=\"\" class=\"wp-image-558\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-1024x325.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-300x95.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-768x244.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-1536x488.png 1536w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3-1200x381.png 1200w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-storage_3.png 1627w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Adding storage<\/figcaption><\/figure>\n\n\n\n<p>Finally, create the EC2 instance by choosing the SSH access key and pressing the Launch Instances button:<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"639\" src=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-KEY_4-opt-1024x639.png\" alt=\"\" class=\"wp-image-559\" srcset=\"https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-KEY_4-opt-1024x639.png 1024w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-KEY_4-opt-300x187.png 300w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-KEY_4-opt-768x479.png 768w, https:\/\/nubisoft.io\/blog\/wp-content\/uploads\/2021\/03\/EC2-GPU-KEY_4-opt.png 1118w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Choosing the SSH access key pair<\/figcaption><\/figure>\n\n\n\n<p>Having SSH access to newly provisioned EC2 instance let&#8217;s start with the prayer of every Ubuntu lover \ud83d\ude09<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\nsudo apt upgrade<\/pre>\n\n\n\n<p>To install the Tesla device driver we will also need access to gcc:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install gcc<\/pre>\n\n\n\n<p>Next, make sure that the kernel headers and development packages for the currently running kernel are up-to-date:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt install linux-headers-$(uname -r)<\/pre>\n\n\n\n<p>Ensure packages on the CUDA network repository have priority over the standard Canonical repository:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">distribution=$(. \/etc\/os-release;echo $ID$VERSION_ID | sed -e 's\/\\.\/\/g')\nwget https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$distribution\/x86_64\/cuda-$distribution.pin\nsudo mv cuda-$distribution.pin \/etc\/apt\/preferences.d\/cuda-repository-pin-600<\/pre>\n\n\n\n<p>Install the CUDA repository public GPG key:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt-key adv --fetch-keys https:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$distribution\/x86_64\/7fa2af80.pub<\/pre>\n\n\n\n<p>Setup the CUDA network repository:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">echo \"deb http:\/\/developer.download.nvidia.com\/compute\/cuda\/repos\/$distribution\/x86_64 \/\" | sudo tee \/etc\/apt\/sources.list.d\/cuda.list<\/pre>\n\n\n\n<p>Update the APT repository cache and install the driver using the cuda-drivers meta-package. The <samp>&#8211;no-install-recommends<\/samp> option can be used for a lean driver install without any dependencies on X packages. This is particularly useful for headless installations on cloud instances with modest HDD resources.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo apt update\nsudo apt -y install cuda-drivers<\/pre>\n\n\n\n<p>.. and five minutes later we are ready to start post-installation steps. First, some paths need to be added &#8211; we can do that permanently modifying <strong>.bashrc<\/strong> file by extending it with two lines:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">nano ~\/.bashrc\n\nexport PATH=\/usr\/local\/cuda-11.2\/bin${PATH:+:NULL}\nexport LD_LIBRARY_PATH=\/usr\/local\/cuda-11.2\/lib64${LD_LIBRARY_PATH:+:NULL}<\/pre>\n\n\n\n<p>Save the file and load the new path into the current shell session using the source command:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">source ~\/.bashrc<\/pre>\n\n\n\n<p>Verify that the driver is loaded and check its version:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">cat \/proc\/driver\/nvidia\/version<\/pre>\n\n\n\n<p>Now, we are ready to deploy the rest of the PyTorch deep learning stack &#8211; we will cover it in the subsequent post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will show you how to configure your Ubuntu GPU EC2 instance in Amazon Web Services from start to finish so that you can perform Deep Learning tasks on it. At NubiSoft, we usually implement machine learning with less popular cloud service providers, because AWS is not the cheapest, and the learning [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":551,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_case_study_excerpt":"","footnotes":""},"categories":[2,104,55,5],"tags":[32,106,107,11,105,54],"class_list":["post-550","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","category-deep-learning","category-devops","category-infrastructure","tag-aws","tag-google-colab","tag-gpu","tag-jupyter","tag-pytorch","tag-ubuntu"],"_links":{"self":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/550","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/comments?post=550"}],"version-history":[{"count":9,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/550\/revisions"}],"predecessor-version":[{"id":575,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/posts\/550\/revisions\/575"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media\/551"}],"wp:attachment":[{"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/media?parent=550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/categories?post=550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nubisoft.io\/blog\/wp-json\/wp\/v2\/tags?post=550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}