{"id":488,"date":"2016-05-03T22:58:00","date_gmt":"2016-05-03T14:58:00","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=488"},"modified":"2018-09-05T23:23:17","modified_gmt":"2018-09-05T15:23:17","slug":"adding-swap-space-for-my-ubuntu-server-15-10-3-image-running-on-my-raspberry-pi-3","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/adding-swap-space-for-my-ubuntu-server-15-10-3-image-running-on-my-raspberry-pi-3\/","title":{"rendered":"Adding swap space for my Ubuntu Server 15.10.3 image running on my Raspberry Pi 3"},"content":{"rendered":"<p>A swap space. also known as virtual memory, is a dedicated area on a writable medium that acts like the RAM for Linux processes to remember things while they are running. For laptop and desktop computers, that writable medium is usually a hard disk. For my Raspberry Pi 3, it is the <a href=\"http:\/\/www.amazon.com\/gp\/product\/B010Q57T02?ie=UTF8&camp=1789&creativeASIN=B010Q57T02&linkCode=xm2&tag=clivsperswebs-20\" title=\"Amazon page for SanDisk Ultra 32GB microSDHC UHS-I Card with Adapter \" target=\"_blank\">microSD card<\/a> which I had written my Ubuntu 15.10.3 image on. <\/p>\n<p>While running the Let's Encrypt application to <a href=\"https:\/\/www.techcoil.com\/blog\/setting-up-a-free-ca-signed-ssl-certificate-from-lets-encrypt-for-my-lemp-server-on-my-raspberry-pi-3-with-an-ubuntu-server-15-10-3-image-to-secure-my-wordpress-site\/\" title=\"Setting up a free CA signed SSL certificate from Let\u2019s Encrypt for my LEMP server on my Raspberry Pi 3 with an Ubuntu Server 15.10.3 image to secure my WordPress site\" target=\"_blank\">set up a free CA signed SSL certificate for my LEMP server on my Raspberry Pi 3 to secure connections to my WordPress site<\/a>, the Let's Encrypt application hanged while it was trying to install the Python dependencies that it needed. This prompted me to turn to swap space for increasing the total memory that processes on my Ubuntu 15.10.3 image can utilize.<\/p>\n<p>This post documents the steps that I took to add some swap space on my Ubuntu 15.10.3 image running on my Raspberry Pi 3 so that processes can use more than 1GB of memory.<\/p>\n<h3>Getting the file system of my Ubuntu 15.10.3 image to use the entire microSD card space that I am slotted into my Raspberry Pi 3<\/h3>\n<p>Since the <a href=\"https:\/\/ubuntu-pi-flavour-maker.org\/wily\/ubuntu-standard-15.10.3-server-armhf-raspberry-pi-2.img.xz.torrent\" title=\"Torrent link for Ubuntu Pi Flavour Maker's Ubuntu Server Standard 15.10.3\" target=\"_blank\">Ubuntu Server Standard 15.10.3 image<\/a> that I had downloaded from <a href=\"https:\/\/ubuntu-pi-flavour-maker.org\" title=\"Ubuntu pi flavour maker home page\" target=\"_blank\">Ubuntu Pi Flavour Maker<\/a> only allocated around 3.5 GB of space to my root directory, I was not able to create more than 1.5 GB of swap space.<\/p>\n<p>Hence, before continuing the creation of swap space for my Ubuntu Server 15.10.3 image, I had to first <a href=\"https:\/\/www.techcoil.com\/blog\/how-i-resized-the-file-system-of-my-ubuntu-server-15-10-3-image-to-utilize-the-entire-microsd-card-space-on-my-raspberry-pi-3\/\" title=\"How I resized the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space on my Raspberry Pi 3\" target=\"_blank\">resize the file system of my Ubuntu Server 15.10.3 image to utilize the entire microSD card space on my Raspberry Pi 3<\/a>.<\/p>\n<h3>Checking whether there were any swap space created for my Ubuntu Server 15.10.3 image<\/h3>\n<p>To confirm that there was no swap space configured for my Ubuntu Server 15.10.3, I first ran the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo swapon -s\r\n<\/pre>\n<p>which returned me nothing. This meant that there was no swap space configured for my Ubuntu Server 15.10.3 image; in another words, there was no additional virtual memory for processes to use if the 1GB ram is fully occupied.<\/p>\n<p>Running the following command also gave me some useful statistic of the total amount of memory, RAM plus virtual memory, that was available for my Ubuntu Server 15.10.3 image to use:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nfree -m\r\n<\/pre>\n<p>which gave me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n             total       used       free     shared    buffers     cached\r\nMem:           925        417        508         22         22        134\r\n-\/+ buffers\/cache:        260        665\r\nSwap:            0          0          0\r\n<\/pre>\n<p>From the table, I could see that there was no swap space configured on my Ubuntu Server 15.10.3 image.<\/p>\n<h3>Creating a swap file for my Ubuntu Server 15.10.3 image to use for swap space<\/h3>\n<p>The first step that I took to add swap space for my Ubuntu Server 15.10.3 was to create a physical file on my root directory that will be used for storing volatile data in the event that the 1GB ram is fully occupied. To do so, I ran the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo fallocate -l 4G \/swapfile\r\n<\/pre>\n<p>After the command completed, I verified the swap file creation with a run of the <code>ls<\/code> command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nls -lh \/swapfile\r\n<\/pre>\n<p>which gave me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n-rw-r--r-- 1 root root 4.0G May  2 15:54 \/swapfile\r\n<\/pre>\n<p>Which that, I was certain that my swap file was created successfully. <\/p>\n<p>I also ran the <code>chmod<\/code> command to ensure that the swap file can only be read and written by the root user:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo chmod 600 \/swapfile\r\n<\/pre>\n<h3>Pointing my Ubuntu Server 15.10.3 image to use \/swapfile as the swap space<\/h3>\n<p>Once I had my swap file ready, the next step would be to get my Ubuntu Server 15.10.3 image to use that swap file as virtual memory. <\/p>\n<p>I first declare <code>\/swapfile<\/code> to be used as swap space:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo mkswap \/swapfile\r\n<\/pre>\n<p>which gave me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nSetting up swapspace version 1, size = 4 GiB (4294963200 bytes)\r\nno label, UUID=35c25aa6-d335-4a78-beb6-a00447aee877\r\n<\/pre>\n<p>With that, I could be sure that my Ubuntu Server 15.10.3 will treat <code>\/swapfile<\/code> as virtual memory.<\/p>\n<p>After declaring <code>\/swapfile<\/code> as virtual memory, I continued on to enable it for use by my Ubuntu Server 15.10.3 when it ran out of memory space in the 1GB ram on my Raspberry Pi 3:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo swapon \/swapfile\r\n<\/pre>\n<p>This command adhered to the Unix philosophy: silent means ok.<\/p>\n<p>To verify that my Ubuntu Server 15.10.3 image is now able to use \/swapfile as virtual memory, I ran the <code>swapon<\/code> command from earlier again:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo swapon -s\r\n<\/pre>\n<p>which returned me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nFilename\t\t\t\tType\t\tSize\tUsed\tPriority\r\n\/swapfile                              \tfile    \t4194300\t0\t-1\r\n<\/pre>\n<p>I also used the free command to see my swap space in action alongside the 1GB ram:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nfree -m\r\n<\/pre>\n<p>Doing so gave me the following output:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n             total       used       free     shared    buffers     cached\r\nMem:           925        477        448         22         24        136\r\n-\/+ buffers\/cache:        317        608\r\nSwap:         4095          0       4095\r\n<\/pre>\n<p>In this output, I could see that there were around 4GB of available swap space for my Ubuntu Server 15.10.3 processes to utilize. No swap space was utilized as there were still enough memory on the 1GB RAM.<\/p>\n<h3>Persisting the swap space configurations<\/h3>\n<p>At this moment in time, the swap space that I had created was temporary: a restart of my Ubuntu Server would cause it to forgot all my virtual memory settings.<\/p>\n<p>To ensure that the swap space that I had created will always be treated as virtual memory: I added the following line in <strong><code>\/etc\/fstab<\/code><\/strong>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n\/swapfile   none    swap    sw    0   0\r\n<\/pre>\n<p>I then saved my changes and closed the file.<\/p>\n<h3>Setting the frequency of memory swapping<\/h3>\n<p>Memory swapping is the process where contents on the RAM is copied to the swap space. This process can cause slowness and this is not desirable for a web server. Hence, in order to tell my Ubuntu Server not to perform memory swapping so frequency, I opened up <code>\/etc\/sysctl.conf<\/code> and added the following line at the bottom of the file:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvm.swappiness=10\r\n<\/pre>\n<p>I then saved my changes and closed the file.<\/p>\n<h3>Setting the cache pressure controls the tendency of the kernel to reclaim<br \/>\nthe memory which is used for caching of directory and inode objects <\/h3>\n<p>The cache pressure of my Ubuntu Server was set at a default value of 100, which had caused it to reclaim memory that is used for caching directory and inode objects too quickly. This is another factor that could cause slowness as a web server often access the same directory and file information frequently, especially when serving popular web pages.<\/p>\n<p>Hence, I opened up <code>\/etc\/sysctl.conf<\/code> again and added the following line at the bottom of the file:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nvm.vfs_cache_pressure = 50\r\n<\/pre>\n<p>I saved my changes and closed the file. This setting would make my Ubuntu Server retain caches of directory and inode objects for longer period of time, hence improving performance in serving web pages.<\/p>\n<h3>Restarting my Ubuntu Server 15.10.3 image for the virtual memory settings to take effect<\/h3>\n<p>Once, I was satisfied with my virtual memory settings, I ran the <code>reboot<\/code> command to restart my Ubuntu Server:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo reboot\r\n<\/pre>\n<p>After the reboot, my Let's Encrypt application was able to proceed beyond the installation of my Python dependencies.<\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-7S\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7S&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7S&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F488&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>A swap space. also known as virtual memory, is a dedicated area on a writable medium that acts like the RAM for Linux processes to remember things while they are running. For laptop and desktop computers, that writable medium is usually a hard disk. For my Raspberry Pi 3, it is the <a href=\"http:\/\/www.amazon.com\/gp\/product\/B010Q57T02?ie=UTF8&#038;camp=1789&#038;creativeASIN=B010Q57T02&#038;linkCode=xm2&#038;tag=clivsperswebs-20\" title=\"Amazon page for SanDisk Ultra 32GB microSDHC UHS-I Card with Adapter \" target=\"_blank\">microSD card<\/a> which I had written my Ubuntu 15.10.3 image on. <\/p>\n<p>While running the Let&#8217;s Encrypt application to <a href=\"https:\/\/www.techcoil.com\/blog\/setting-up-a-free-ca-signed-ssl-certificate-from-lets-encrypt-for-my-lemp-server-on-my-raspberry-pi-3-with-an-ubuntu-server-15-10-3-image-to-secure-my-wordpress-site\/\" title=\"Setting up a free CA signed SSL certificate from Let\u2019s Encrypt for my LEMP server on my Raspberry Pi 3 with an Ubuntu Server 15.10.3 image to secure my WordPress site\" target=\"_blank\">set up a free CA signed SSL certificate for my LEMP server on my Raspberry Pi 3 to secure connections to my WordPress site<\/a>, the Let&#8217;s Encrypt application hanged while it was trying to install the Python dependencies that it needed. This prompted me to turn to swap space for increasing the total memory that processes on my Ubuntu 15.10.3 image can utilize.<\/p>\n<p>This post documents the steps that I took to add some swap space on my Ubuntu 15.10.3 image running on my Raspberry Pi 3 so that processes can use more than 1GB of memory.<\/p>\n","protected":false},"author":1,"featured_media":1282,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[240,308,423,195,262,241,263],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Ubuntu-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-7S","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/488"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=488"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/488\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1282"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}