{"id":340,"date":"2018-05-06T22:28:34","date_gmt":"2018-05-06T14:28:34","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=340"},"modified":"2018-11-18T17:47:42","modified_gmt":"2018-11-18T09:47:42","slug":"how-to-setup-jupyter-notebook-on-raspberry-pi-3-with-raspbian-stretch-lite-supervisor-and-virtualenv-to-run-python-3-codes","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-setup-jupyter-notebook-on-raspberry-pi-3-with-raspbian-stretch-lite-supervisor-and-virtualenv-to-run-python-3-codes\/","title":{"rendered":"How to setup Jupyter Notebook on Raspberry Pi 3 with Raspbian Stretch Lite, Supervisor and Virtualenv to run Python 3 codes"},"content":{"rendered":"<p>By taking <a href=\"https:\/\/click.linksynergy.com\/deeplink?id=qGQokmUY1xA&mid=40328&murl=https%3A%2F%2Fwww.coursera.org%2Fspecializations%2Fdeep-learning\" rel=\"noopener\" target=\"_blank\">Andrew Ng's Deep Learning Coursera course<\/a>, you can get a better understanding of deep learning. In addition, you will also get many <a href=\"http:\/\/jupyter.org\/\" rel=\"noopener\" target=\"_blank\">Jupyter Notebook<\/a> documents that you can reference to build models that can solve interesting problems. <\/p>\n<p>In order to readily access these documents, you may want to setup Jupyter Notebook on a <a href=\"https:\/\/www.techcoil.com\/glossary\/single-board-computer\/\">single board computer<\/a> like the <a href=\"https:\/\/www.amazon.com\/Element14-RPI-Model-PLUS-Motherboard\/dp\/B07BDR5PDW\/ref=as_li_ss_tl?s=pc&ie=UTF8&qid=1525488147&sr=1-4&tag=clivsperswebs-20\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3<\/a>. With this in mind, this post shows how you can setup Jupyter Notebook on Raspberry Pi 3 with Raspbian Stretch Lite, Supervisor and Virtualenv to run Python 3 codes.<\/p>\n<h2>Recommended hardware<\/h2>\n<p>So <a href=\"https:\/\/www.techcoil.com\/raspberry-pi-3-buying-guide\" rel=\"noopener\" target=\"_blank\">what can you buy for this Raspberry Pi 3 project<\/a>? <\/p>\n<p>For the purpose of setting up Jupyter Notebook to readily access your documents, your can consider getting the following items:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.amazon.com\/CanaKit-Raspberry-Micro-Supply-Listed\/dp\/B01C6FFNY4\/ref=as_li_ss_tl?s=pc&ie=UTF8&qid=1517212574&sr=1-4&keywords=raspberry+pi+3&linkCode=ll1&tag=clivsperswebs-20&linkId=76113a630916ac804456aa1f5dd76c85\" rel=\"noopener\" target=\"_blank\">CanaKit Raspberry Pi 3 with 2.5A Micro USB Power Supply<\/a> or <a href=\"https:\/\/www.amazon.com\/CanaKit-Raspberry-Power-Supply-Listed\/dp\/B07BC6WH7V\/\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=facc24accf03fbe5cd45b1893a1e9ab1\" rel=\"noopener\" target=\"_blank\">CanaKit Raspberry Pi 3 B+ (B Plus) with 2.5A Power Supply (UL Listed)<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/dp\/B01F1PSFY6\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=d68c56d79a1d84f89f596fa715743a9e\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 2\/3 official case Black\/Grey<\/a> or <a href=\"https:\/\/www.amazon.com\/dp\/B01CCPKCSK\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=1e69ae5649504fb86c6597e0774eb3d8\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 2\/3 official case Red\/White<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/Sandisk-Ultra-Micro-UHS-I-Adapter\/dp\/B073JWXGNT\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=20db403fc5e8bbc15e46ec21868a1488\" rel=\"noopener\" target=\"_blank\"> Sandisk Ultra 32GB Micro SDHC UHS-I Card with Adapter<\/a><\/li>\n<li><a href=\"https:\/\/www.amazon.com\/gp\/product\/B00N2VISLW\/ref=as_li_tl?ie=UTF8&tag=clivsperswebs-20&camp=1789&creative=9325&linkCode=as2&creativeASIN=B00N2VISLW&linkId=132403a064ff21634835c1deee4bc983\" target=\"_blank\">AmazonBasics RJ45 Cat-6 Ethernet Patch Cable - 3 Feet (0.9 Meters)<\/a><\/li>\n<\/ul>\n<h2>Setting up Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications<\/h2>\n<p>Once you got the hardware, proceed to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-stretch-lite-on-raspberry-pi-3-to-run-python-3-applications\/\" rel=\"noopener\" target=\"_blank\">setup Raspbian Stretch Lite on Raspberry Pi 3 for running Python 3 applications<\/a>.<\/p>\n<p>After that, you will get your Raspberry Pi 3 running the Raspbian Stretch Lite operating system with the following software pieces:<\/p>\n<ul>\n<li>SSH server that enables you to configure Raspbian Stretch Lite from another computer in your home network.<\/li>\n<li>Python 3 for running Jupyter Notebook.<\/li>\n<li>Python 3 package manager (pip3) for installing Jupyter Notebook.<\/li>\n<li>Virtualenv for you to create an isolated environment for running Jupyter Notebook.<\/li>\n<\/ul>\n<p>Keep your remote session to your Raspbian Stretch Lite to continue with the rest of the setup. <\/p>\n<h2>Installing Supervisor<\/h2>\n<p><a href=\"http:\/\/supervisord.org\/\">Supervisor<\/a> is a convenient tool for running an application as a server daemon. Therefore, we can use it to keep an instance of Jupyter Notebook running as a server daemon. With this in mind, let's install Supervisor on your Raspbian Stretch Lite with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get install supervisor -y\r\n<\/pre>\n<p>After that, run the following command to verify your installation:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl status supervisor\r\n<\/pre>\n<p>Once you had run the command, you should see output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n\u25cf supervisor.service - Supervisor process control system for UNIX\r\n   Loaded: loaded (\/lib\/systemd\/system\/supervisor.service; enabled; vendor preset: enabled)\r\n   Active: active (running) since Sun 2018-05-06 14:41:12 +08; 3s ago\r\n     Docs: http:\/\/supervisord.org\r\n Main PID: 18293 (supervisord)\r\n   CGroup: \/system.slice\/supervisor.service\r\n           \u2514\u250018293 \/usr\/bin\/python \/usr\/bin\/supervisord -n -c \/etc\/supervisor\/supervisord.conf\r\n \r\nMay 06 14:41:12 raspberrypi systemd&#x5B;1]: Started Supervisor process control system for UNIX.\r\nMay 06 14:41:14 raspberrypi supervisord&#x5B;18293]: 2018-03-03 13:52:14,741 CRIT Supervisor running as root (no user in config file)\r\nMay 06 14:41:14 raspberrypi supervisord&#x5B;18293]: 2018-03-03 13:52:14,743 WARN No file matches via include &quot;\/etc\/supervisor\/conf.d\/*.conf&quot;\r\nMay 06 14:41:14 raspberrypi supervisord&#x5B;18293]: 2018-03-03 13:52:14,794 INFO RPC interface 'supervisor' initialized\r\nMay 06 14:41:14 raspberrypi supervisord&#x5B;18293]: 2018-03-03 13:52:14,796 CRIT Server 'unix_http_server' running without any HTTP authentication checking\r\nMay 06 14:41:14 raspberrypi supervisord&#x5B;18293]: 2018-03-03 13:52:14,797 INFO supervisord started with pid 18293\r\n<\/pre>\n<h2>Creating a directory to keep your Jupyter Notebook documents<\/h2>\n<p>Once you had installed Supervisor, you can proceed with creating a directory to keep your Jupyter Notebook documents. For example, you can run the following command to create the <code>jupyter-notebook<\/code> directory inside the home directory:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nmkdir ~\/jupyter-notebook\r\n<\/pre>\n<h2>Creating a virtual environment and install Jupyter Notebook<\/h2>\n<p>Once you had created the directory to keep your Jupyter Notebook, proceed to create the virtual environment for Jupyter Notebook:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvirtualenv -p python3 ~\/jupyter-env\r\n<\/pre>\n<p>After that, run the following commands to install Jupyter Notebook into the virtual environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource ~\/jupyter-env\/bin\/activate\r\npip install jupyter\r\ndeactivate\r\n<\/pre>\n<p>After all these commands had completed, you will be able to run Jupyter Notebook from the virtual environment.<\/p>\n<h2>Creating a shell script to run Jupyter Notebook within the virtual environment<\/h2>\n<p>Once you had created the virtual environment with Jupyter Notebook installed, proceed to create a shell script to run Jupyter Notebook. To do so, run the following command to create a shell script at <code>~\/run-jupyter-notebook.sh<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano ~\/jupyter-notebook\/run-jupyter-notebook.sh\r\n<\/pre>\n<p>And inside the editor, create the following content:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#!\/bin\/bash\r\nsource \/home\/pi\/jupyter-env\/bin\/activate\r\njupyter notebook --ip 0.0.0.0 --port 9999 --no-browser\r\ndeactivate\r\n<\/pre>\n<p>Once you had saved the file, make the file executable by running the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo chmod +x ~\/jupyter-notebook\/run-jupyter-notebook.sh\r\n<\/pre>\n<h2>Creating the Supervisor configuration file to run Jupyter Notebook<\/h2>\n<p>In order to get Supervisor to run Jupyter Notebook, you need to create a Supervisor configuration file. <\/p>\n<p>Run the following command to create a configuration file at <code>\/etc\/supervisor\/conf.d\/jupyter-notebook.conf<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano \/etc\/supervisor\/conf.d\/jupyter-notebook.conf\r\n<\/pre>\n<p>And inside the editor, create the following content:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;program:jupyter-notebook]\r\ndirectory=\/home\/pi\/jupyter-notebook\r\ncommand=\/bin\/bash -E -c .\/run-jupyter-notebook.sh\r\nautostart=true\r\nautorestart=true\r\nstopsignal=INT\r\nstopasgroup=true\r\nkillasgroup=true\r\nuser=pi\r\n<\/pre>\n<p>Once you had saved <code>\/etc\/supervisor\/conf.d\/jupyter-notebook.conf<\/code>, run the following command to restart Supervisor:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo systemctl restart supervisor.service\r\n<\/pre>\n<h2>Accessing Jupyter Notebook for the first time<\/h2>\n<p>When you access Jupyter Notebook for the first time, you need to provide a token that Jupyter Notebook had generated. <\/p>\n<p>In order to get the token, get into <code>supervisorctl<\/code> with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo supervisorctl\r\n<\/pre>\n<p>And inside <code>supervisorctl<\/code>, print the log generated by Jupyter Notebook by running the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\ntail jupyter-notebook stdout\r\n<\/pre>\n<p>This will print output similar to the following:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n&#x5B;I 21:15:48.344 NotebookApp] Serving notebooks from local directory: \/home\/pi\/jupyter-notebook\r\n&#x5B;I 21:15:48.344 NotebookApp] 0 active kernels\r\n&#x5B;I 21:15:48.345 NotebookApp] The Jupyter Notebook is running at:\r\n&#x5B;I 21:15:48.345 NotebookApp] http:\/\/0.0.0.0:9999\/?token=8f819c94ddbb9286271735fc9ab78838c6c81748d206ce37\r\n&#x5B;I 21:15:48.346 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).\r\n&#x5B;C 21:15:48.348 NotebookApp] \r\n<\/pre>\n<p>The fourth line contains the token that you can use to access Jupyter Notebook for the first time.<\/p>\n<p>Enter <strong>http:\/\/[your-raspberry-pi-ip]:9999\/?token=[token]<\/strong> in your browser. For example, if your Raspberry Pi 3 IP address is 192.168.1.109, you will enter <strong>http:\/\/192.168.1.109:9999\/?token=8f819c94ddbb9286271735fc9ab78838c6c81748d206ce37<\/strong> based on the above output.<\/p>\n<p>Once you had enter the URL into your browser, you will see a similar screen as the following:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jupyter-notebook-page-with-coursera-deep-learning-folder.gif\" alt=\"Jupyter Notebook page with coursera deep learning folder\"\/><\/p>\n<p>With that, you will be able to readily access your Jupyter Notebook documents whenever you need to.<\/p>\n<h2>Buying the Raspberry Pi 3 hardware to host your own Jupyter Notebook<\/h2>\n<p>If you do not have the Raspberry Pi 3 hardware mentioned in this post yet, you may want to purchase them from Amazon. Simply click on the button below to add the Raspberry Pi 3 hardware to your cart. You may remove anything that you already have or replace some of the items with other items.<\/p>\n<p><center><\/p>\n<form action=\"https:\/\/www.amazon.com\/gp\/aws\/cart\/add.html\" method=\"GET\" target=\"_blank\"><input name=\"AssociateTag\" type=\"hidden\" value=\"clivsperswebs-20\"><input name=\"SubscriptionId\" type=\"hidden\" value=\"[AKIAIMND5UBCQU2HRSUA]\"><input name=\"ASIN.1\" type=\"hidden\" value=\"B07BC6WH7V\"><input name=\"Quantity.1\" type=\"hidden\" value=\"1\"><input name=\"ASIN.2\" type=\"hidden\" value=\"B01F1PSFY6\"><input name=\"Quantity.2\" type=\"hidden\" value=\"1\"><input name=\"ASIN.3\" type=\"hidden\" value=\"B073JWXGNT\"><input name=\"Quantity.3\" type=\"hidden\" value=\"1\"><input name=\"ASIN.4\" type=\"hidden\" value=\"B00N2VISLW\"><input name=\"Quantity.4\" type=\"hidden\" value=\"1\"><br \/>\n<input alt=\"Buy from Amazon.com\" name=\"add\" type=\"image\" src=\"https:\/\/images-na.ssl-images-amazon.com\/images\/G\/01\/associates\/remote-buy-box\/buy4.gif\" value=\"Buy from Amazon.com\"><\/form>\n<\/p>\n<p><\/center><\/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-5u\" 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-5u&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-5u&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%2F340&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>By taking <a href=\"https:\/\/click.linksynergy.com\/deeplink?id=qGQokmUY1xA&#038;mid=40328&#038;murl=https%3A%2F%2Fwww.coursera.org%2Fspecializations%2Fdeep-learning\" rel=\"noopener\" target=\"_blank\">Andrew Ng&#8217;s Deep Learning Coursera course<\/a>, you can get a better understanding of deep learning. In addition, you will also get many <a href=\"http:\/\/jupyter.org\/\" rel=\"noopener\" target=\"_blank\">Jupyter Notebook<\/a> documents that you can reference to build models that can solve interesting problems. <\/p>\n<p>In order to readily access these documents, you may want to setup Jupyter Notebook on a <a href=\"https:\/\/www.techcoil.com\/glossary\/single-board-computer\/\">single board computer<\/a> like the <a href=\"https:\/\/www.amazon.com\/Element14-RPI-Model-PLUS-Motherboard\/dp\/B07BDR5PDW\/ref=as_li_ss_tl?s=pc&#038;ie=UTF8&#038;qid=1525488147&#038;sr=1-4&#038;tag=clivsperswebs-20\" rel=\"noopener\" target=\"_blank\">Raspberry Pi 3<\/a>. With this in mind, this post shows how you can setup Jupyter Notebook on Raspberry Pi 3 with Raspbian Stretch Lite, Supervisor and Virtualenv to run Python 3 codes.<\/p>\n","protected":false},"author":1,"featured_media":999,"comment_status":"open","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":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[528,226,233,240,308,412,445,195,426],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/jupyter-notebook-page-with-coursera-deep-learning-folder.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-5u","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/340"}],"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=340"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/340\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/999"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}