How To Set Up a Jupyter Notebook with Python 3 on Debian 9

Introduction

Jupyter Notebookoffers a command shell for interactive computing as a web application. The tool can be used with several languages, including Python, Julia, R, Haskell, and Ruby. It is often used for working with data, statistical modeling, and machine learning.

This tutorial will walk you through setting up Jupyter Notebook to run from a Debian 9 server, as well as teach you how to connect to and use the notebook. Jupyter notebooks (or simply notebooks) are documents produced by the Jupyter Notebook app which contain both computer code and rich text elements (paragraph, equations, figures, links, etc.) which aid in presenting and sharing reproducible research.

By the end of this guide, you will be able to run Python 3 code using Jupyter Notebook running on a remote server.

Prerequisites

In order to complete this guide, you should have a fresh Debian 9 server instance with a basic firewall and a non-root user with sudo privileges configured. You can learn how to set this up by running through ourInitial Server Setup with Debian 9guide.

Step 1 — Install Pip and Python Headers

To begin the process, we'll download and install all of the items we need from the Debian repositories. We will use the Python package manager?pip?to install additional components a bit later.

We first need to update the local?apt?package index and then download and install the packages:

sudo apt update

Next, install?pip?and the Python header files, which are used by some of Jupyter’s dependencies:

sudo apt install python3-pip python3-dev

Debian 9 (“Stretch”) comes preinstalled with Python 3.5.

We can now move on to setting up a Python virtual environment into which we’ll install Jupyter.

Step 2 — Create a Python Virtual Environment for Jupyter

Now that we have Python 3, its header files, and?pip?ready to go, we can create a Python virtual environment for easier management. We will install Jupyter into this virtual environment.

To do this, we first need access to the?virtualenv?command. We can install this with?pip.

Upgrade?pip?and install the package by typing:

sudo -H pip3 install --upgrade pip

sudo -H pip3 install virtualenv

With?virtualenv?installed, we can start forming our environment. Create and move into a directory where we can keep our project files:

mkdir ~/myprojectdir

cd ~/myprojectdir

Within the project directory, create a Python virtual environment by typing:

virtualenvmyprojectenv

This will create a directory called?myprojectenv?within your?myprojectdir?directory. Inside, it will install a local version of Python and a local version of?pip. We can use this to install and configure an isolated Python environment for Jupyter.

Before we install Jupyter, we need to activate the virtual environment. You can do that by typing:

sourcemyprojectenv/bin/activate

Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this:?(myprojectenv)user@host:~/myprojectdir$.

You’re now ready to install Jupyter into this virtual environment.

Step 3 — Install Jupyter

With your virtual environment active, install Jupyter with the local instance of?pip:

Note:When the virtual environment is activated (when your prompt has?(myprojectenv)?preceding it), use?pip?instead of?pip3, even if you are using Python 3. The virtual environment's copy of the tool is always named?pip, regardless of the Python version.

pip install jupyter

At this point, you’ve successfully installed all the software needed to run Jupyter. We can now start the notebook server.

Step 4 — Run Jupyter Notebook

You now have everything you need to run Jupyter Notebook! To run it, execute the following command:

jupyter notebook

A log of the activities of the Jupyter Notebook will be printed to the terminal. When you run Jupyter Notebook, it runs on a specific port number. The first notebook you run will usually use port?8888. To check the specific port number Jupyter Notebook is running on, refer to the output of the command used to start it:

Output

[I 21:23:21.198 NotebookApp] Writing notebook server cookie secret to /run/user/1001/jupyter/notebook_cookie_secret[I 21:23:21.361 NotebookApp] Serving notebooks from local directory: /home/sammy/myprojectdir[I 21:23:21.361 NotebookApp] The Jupyter Notebook is running at:[I 21:23:21.361 NotebookApp] http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72[I 21:23:21.361 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).[W 21:23:21.361 NotebookApp] No web browser found: could not locate runnable browser.[C 21:23:21.361 NotebookApp]? ? Copy/paste this URL into your browser when you connect for the first time,? ? to login with a token:? ? ? ? http://localhost:8888/?token=1fefa6ab49a498a3f37c959404f7baf16b9a2eda3eaa6d72

If you are running Jupyter Notebook on a local Debian computer (not on a Droplet), you can simply navigate to the displayed URL to connect to Jupyter Notebook. If you are running Jupyter Notebook on a Droplet, you will need to connect to the server using SSH tunneling as outlined in the next section.

At this point, you can keep the SSH connection open and keep Jupyter Notebook running or can exit the app and re-run it once you set up SSH tunneling. Let's keep it simple and stop the Jupyter Notebook process. We will run it again once we have SSH tunneling working. To stop the Jupyter Notebook process, press?CTRL+C, type?Y, and hit?ENTER?to confirm. The following will be displayed:

Output

[C 21:28:28.512 NotebookApp] Shutdown confirmed[I 21:28:28.512 NotebookApp] Shutting down 0 kernels

We’ll now set up an SSH tunnel so that we can access the notebook.

Step 5 — Connect to the Server Using SSH Tunneling

In this section we will learn how to connect to the Jupyter Notebook web interface using SSH tunneling. Since Jupyter Notebook will run on a specific port on the server (such as?:8888,?:8889?etc.), SSH tunneling enables you to connect to the server’s port securely.

The next two subsections describe how to create an SSH tunnel from 1) a Mac or Linux and 2) Windows. Please refer to the subsection for your local computer.

SSH Tunneling with a Mac or Linux

If you are using a Mac or Linux, the steps for creating an SSH tunnel are similar to using SSH to log in to your remote server, except that there are additional parameters in the?ssh?command. This subsection will outline the additional parameters needed in the?ssh?command to tunnel successfully.

SSH tunneling can be done by running the following SSH command in a new local terminal window:

ssh -L8888:localhost:8888your_server_username@your_server_ip

The?ssh?command opens an SSH connection, but?-L?specifies that the given port on the local (client) host is to be forwarded to the given host and port on the remote side (server). This means that whatever is running on the second port number (e.g.?8888) on the server will appear on the first port number (e.g.?8888) on your local computer.

Optionally change port?8888?to one of your choosing to avoid using a port already in use by another process.

server_username?is your username (e.g.?sammy) on the server which you created and?your_server_ip?is the IP address of your server.

For example, for the username?sammy?and the server address?203.0.113.0, the command would be:

ssh -L8888:localhost:8888sammy@203.0.113.0

If no error shows up after running the?ssh -L?command, you can move into your programming environment and run Jupyter Notebook:

jupyter notebook

You’ll receive output with a URL. From a web browser on your local machine, open the Jupyter Notebook web interface with the URL that starts with?http://localhost:8888. Ensure that the token number is included, or enter the token number string when prompted at?http://localhost:8888.

SSH Tunneling with Windows and Putty

If you are using Windows, you can create an SSH tunnel usingPutty.

First, enter the server URL or IP address as the hostname as shown:

Next, clickSSHon the bottom of the left pane to expand the menu, and then clickTunnels. Enter the local port number to use to access Jupyter on your local machine. Choose?8000?or greater to avoid ports used by other services, and set the destination as?localhost:8888?where?:8888?is the number of the port that Jupyter Notebook is running on.

Now click theAddbutton, and the ports should appear in theForwarded portslist:

Finally, click theOpenbutton to connect to the server via SSH and tunnel the desired ports. Navigate to?http://localhost:8000?(or whatever port you chose) in a web browser to connect to Jupyter Notebook running on the server. Ensure that the token number is included, or enter the token number string when prompted at?http://localhost:8000.

Step 6 — Using Jupyter Notebook

This section goes over the basics of using Jupyter Notebook. If you don’t currently have Jupyter Notebook running, start it with the?jupyter notebook?command.

You should now be connected to it using a web browser. Jupyter Notebook is very powerful and has many features. This section will outline a few of the basic features to get you started using the notebook. Jupyter Notebook will show all of the files and folders in the directory it is run from, so when you’re working on a project make sure to start it from the project directory.

To create a new notebook file, selectNew>Python 3from the top right pull-down menu:

This will open a notebook. We can now run Python code in the cell or change the cell to markdown. For example, change the first cell to accept Markdown by clickingCell>Cell Type>Markdownfrom the top navigation bar. We can now write notes using Markdown and even include equations written inLaTeXby putting them between the?$$?symbols. For example, type the following into the cell after changing it to markdown:

# Simple Equation

Let us now implement the following equation:

$$ y = x^2$$

where $x = 2$

To turn the markdown into rich text, press?CTRL+ENTER, and the following should be the results:

You can use the markdown cells to make notes and document your code. Let's implement that simple equation and print the result. Click on the top cell, then press?ALT+ENTER?to add a cell below it. Enter the following code in the new cell.

x =2y = x**2print(y)

To run the code, press?CTRL+ENTER. You’ll receive the following results:

You now have the ability toimport modulesand use the notebook as you would with any other Python development environment!

Conclusion

Congratulations! You should now be able to write reproducible Python code and notes in Markdown using Jupyter Notebook. To get a quick tour of Jupyter Notebook from within the interface, selectHelp>User Interface Tourfrom the top navigation menu to learn more.

From here, you may be interested to read our series onTime Series Visualization and Forecasting.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌启泣,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,509評(píng)論 6 504
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件斜姥,死亡現(xiàn)場(chǎng)離奇詭異蜈亩,居然都是意外死亡蒸眠,警方通過(guò)查閱死者的電腦和手機(jī)袄琳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,806評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門(mén)询件,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人唆樊,你說(shuō)我怎么就攤上這事宛琅。” “怎么了逗旁?”我有些...
    開(kāi)封第一講書(shū)人閱讀 163,875評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵夯秃,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我痢艺,道長(zhǎng),這世上最難降的妖魔是什么介陶? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,441評(píng)論 1 293
  • 正文 為了忘掉前任堤舒,我火速辦了婚禮,結(jié)果婚禮上哺呜,老公的妹妹穿的比我還像新娘舌缤。我一直安慰自己,他們只是感情好某残,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,488評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布国撵。 她就那樣靜靜地躺著,像睡著了一般玻墅。 火紅的嫁衣襯著肌膚如雪介牙。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,365評(píng)論 1 302
  • 那天澳厢,我揣著相機(jī)與錄音环础,去河邊找鬼。 笑死剩拢,一個(gè)胖子當(dāng)著我的面吹牛线得,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播徐伐,決...
    沈念sama閱讀 40,190評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼贯钩,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起角雷,我...
    開(kāi)封第一講書(shū)人閱讀 39,062評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤祸穷,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后谓罗,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體粱哼,經(jīng)...
    沈念sama閱讀 45,500評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,706評(píng)論 3 335
  • 正文 我和宋清朗相戀三年檩咱,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了揭措。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,834評(píng)論 1 347
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡刻蚯,死狀恐怖绊含,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情炊汹,我是刑警寧澤躬充,帶...
    沈念sama閱讀 35,559評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站讨便,受9級(jí)特大地震影響充甚,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜霸褒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,167評(píng)論 3 328
  • 文/蒙蒙 一伴找、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧废菱,春花似錦技矮、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,779評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至旁理,卻和暖如春樊零,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背孽文。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,912評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工淹接, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人叛溢。 一個(gè)月前我還...
    沈念sama閱讀 47,958評(píng)論 2 370
  • 正文 我出身青樓塑悼,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親楷掉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子厢蒜,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,779評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容

  • Introduction Jupyter Notebookis an open-source, interacti...
    ???木?燚??閱讀 459評(píng)論 0 0
  • rljs by sennchi Timeline of History Part One The Cognitiv...
    sennchi閱讀 7,325評(píng)論 0 10
  • 晚上通過(guò)平臺(tái)預(yù)約了附院的一個(gè)專(zhuān)家號(hào)霞势。 早晨八點(diǎn)出發(fā),和老三一起斑鸦,開(kāi)車(chē)?yán)习帚倒薄⒗蠇尅? 附院里人...
    夕陽(yáng)在山閱讀 132評(píng)論 2 1
  • 雙調(diào)排序介紹 - 炒飯的小站 雙調(diào)排序解答 - CSDN 有一定了解的可以直接看最后一部分的代碼。 雙調(diào)排序是一種...
    謝小帥閱讀 8,972評(píng)論 3 8
  • 尚俊平焦點(diǎn)網(wǎng)絡(luò)中級(jí),堅(jiān)持分享509天嘱巾,2017年8月20日周日 有很大一部分人辛苦工作憨琳,勤奮努力,取得驕人...
    32598db751bb閱讀 1,262評(píng)論 0 1