HOW TO INSTALL IPYTHON FROM SCRATCH ON DEBIAN 7
################################################

Links that I used to get this information and install ipython with:

Installation and uninstallation:
http://www.installion.co.uk/debian/wheezy/main/i/ipython-notebook/install.html
http://www.installion.co.uk/debian/wheezy/main/i/ipython-notebook/uninstall.html

Configuration & General:
http://askubuntu.com/questions/335883/how-to-use-the-newest-ipython-in-ubuntu12-04
http://ipython.org/ipython-doc/rel-1.1.0/interactive/public_server.html
http://nbviewer.ipython.org/github/ipython/ipython-in-depth/blob/master/notebooks/Running%20a%20Secure%20Public%20Notebook.ipynb

Side Note about SSL file types (since we use either a single pem, or crt & key file – i recommend just to get a single pem file which will include the key and crt, thats how below config will instruct you):
http://serverfault.com/questions/9708/what-is-a-pem-file-and-how-does-it-differ-from-other-openssl-generated-key-file

Before Installation
####################

apt-get update

apt-get upgrade

 

NOTE: my sources.list looks like this (so make sure yours has it):

# cat /etc/apt/sources.list

deb http://ftp.us.debian.org/debian/ wheezy main contrib
deb-src http://ftp.us.debian.org/debian/ wheezy main contrib

deb http://security.debian.org/ wheezy/updates main contrib
deb-src http://security.debian.org/ wheezy/updates main contrib

deb http://ftp.us.debian.org/debian/ wheezy-updates main contrib
deb-src http://ftp.us.debian.org/debian/ wheezy-updates main contrib

 

Installation
#############

I will show you 2 installation methods, the one that I did and the much much easier one (that you should use)

Method1 – Short – from link
==============================

apt-get install ipython-notebook

 

Method2 – Long – the one I did (using pip, thus insures latest version of ipython)
===================================================================

First I wanted to start fresh so I “apt-get removed” everything python related on my system. This in the processes uninstalled GNOME (thats a big warning, I never used gnome so it was okay for me, im a CLI guy that likes console and putty+ssh)

PYTHON MEGA UNINSTALL:

Find every python module and delete it (if you dont have the paste command):

DO THIS:
——–

cd /root
dpkg -l | grep python | awk '{print $2;}' > python.list.newline
x=0; tot=`wc -l python.list.space | awk '{print $1;}'`; for i in `cat python.list.newline`; do x=$((x+1)); echo "===${x}/${tot}: $i==="; apt-get purge -y $i; done;

 

Find every python module and delete it (if you do have the paste command):

OR THIS:
——–

cd /root
dpkg -l | grep python | awk '{print $2;}' | paste -s -d' ' > python.list.space
x=0; tot=`wc -w python.list.space | awk '{print $1;}'`; for i in `cat python.list.space`; do x=$((x+1)); echo "===${x}/${tot}: $i==="; apt-get purge -y $i; done;

 

THEN AFTER EITHER OF THOSE DO THESE:
————————————-

Fix any dependencies after the uninstall:

apt-get autoremove;
apt-get install -f;

 

Confirm “dpkg -l” is now missing everything python related:

This should show every debian package, python should be missing:
dpkg -l | grep python

PYTHON INSTALL:

apt-get install python

cd /root

 

Lets get the latest pip:

wget https://raw.github.com/pypa/pip/master/contrib/get-pip.py

python get-pip.py

 

NOTE: instead you can just do “apt-get install python-pip”

Lets install any dependencies that might come up

apt-get install gcc
apt-get install g++
apt-get install libpyside-dev

apt-get install python2.7-dev
apt-get install python2.6-dev

apt-get install python-numpy
apt-get install python-matplotlib
apt-get install python-sympy

apt-get install libncurses5 libncurses5-dev

 

SUGGESTED MOVE: you can just install all of the possibile dependencies:

apt-get build-dep ipython-notebook

 

Now lets install some pip stuff (this installs python modules, just a bunch of .py and .pyc files that go into python folders across the system, and thus you can then “import <module>” in your code)

NOTE: if some fail to install or give errors, try to look for the error that says missing header file (.h file) and find it perhaps using this method: http://www.infotinks.com/debian-missing-file-compiling/ and fix the problem by installing the right module (thats why above I had so many apt-gets, so that we would get every possible program that would be needed to install all of these pip modules no problem)

NOTE: some of these are not needed for ipython like pattern or path.py, I just threw them in for fun. Some are needed like readline and jinja and pyzmq and jinja2 and a few others.

NOTE: installing ipython with pip instead of apt-get ensures latest version of ipython

pip install ipython
pip install path.py
pip install --allow-external pattern
pip install watchdog
pip install sh
pip install fuzzywuzzy
pip install python-dateutil
pip install nose
pip install pexpect
pip install tornado
pip install jinja
pip install readline
pip install pyzmq
pip install jinja2

 

In the end when my ipython worked it looked like this (try to match it, if your having problems):

# pip list
argh (0.24.1)
argparse (1.2.1)
backports.ssl-match-hostname (3.4.0.2)
configobj (4.7.2)
decorator (3.3.3)
fuzzywuzzy (0.2)
ipython (1.1.0)
Jinja (1.2)
Jinja2 (2.7.2)
MarkupSafe (0.18)
matplotlib (1.1.1rc2)
nose (1.3.0)
numpy (1.6.2)
path.py (5.0)
pathtools (0.1.2)
pexpect (2.4)
PIL (1.1.7)
pip (1.5.1)
pyparsing (1.5.6)
python-dateutil (2.2)
pytz (2012c)
PyYAML (3.10)
pyzmq (14.0.1)
readline (6.2.4.1)
Scrapy (0.22.0)
setuptools (2.1)
sh (1.09)
simplegeneric (0.8.1)
six (1.5.2)
sympy (0.7.1.rc1)
tornado (3.2)
watchdog (0.7.0)
wsgiref (0.1.2)

 

And of course here is my python related debian packages (other important packages are not showing on here like g++ and gcc but they are there)

# dpkg -l | egrep python | awk '{print $2}'
ipython
libpython2.6
libpython2.7
libpython3.2
python
python-cairo
python-configobj
python-dateutil
python-decorator
python-gi
python-glade2
python-gobject
python-gobject-2
python-gtk2
python-imaging
python-matplotlib
python-matplotlib-data
python-minimal
python-numpy
python-pexpect
python-pyparsing
python-simplegeneric
python-support
python-sympy
python-tk
python-tz
python2.6
python2.6-dev
python2.6-minimal
python2.7
python2.7-dev
python2.7-minimal
python3
python3-minimal
python3.2
python3.2-minimal

 

CONFIGURATION
###############

After using installation method1 or method2. Lets setup the module. The setup for our netbook would be:
– available on all interfaces
– ssl (so uses https) so need a cert file (a pem file would be easiest, a chain of crt and key files)
– a password for added security (generated with IPython.lib.passwd

Make Password
==============

Type in:

ipython2.7
# or
python
# or
python2
# or
python2.6
# or
python2.7

 

You can launch python or ipython with several executables, I use either the one without the version number or the latest one. You might have python3 and python3.2, I wouldnt use that for this.

After inside a python program type in these commands @ the prompts:

from IPython.lib import passwd
password=passwd("PUT-PASSWORD-HERE-OF-YOUR-CHOOSING")
password

OUTPUT:
'sha1:cb3e1440003f:fbb1d1c123f34b2b19bee101123ab1e12e932043'

 

The first line imports the passwd module that can make a password for us. On the second line we set our password and we should remember it and note it, and that password gets turned into an encrypted formated and saved into the variable “password”. Then typing “password” without anything will show the value its holding

Make Cert
===========

I like to keep my certs

cd /etc/ssl
mkdir local
cd local
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

 

Fill out the information (its important to fill out the server FQDN correctly, type it in as it would appear when your accessing your webserver from far away/remotely):

Country Name (2 letter code) [AU]: US
State or Province Name (full name) [Some-State]: Alaska
Locality Name (eg, city) []: Cold Place
Organization Name (eg, company) [Internet Widgits Pty Ltd]: Kossboss
Organizational Unit Name (eg, section) []: infotinks
Common Name (e.g. server FQDN or YOUR name) []: ipython.infotinks.com
Email Address []: ipython@infotinks.com

 

Make the notebook file and configure it
=========================================

cd /

We are going to make a notebook called “nbserver”, this is also called a ‘profile’

ipython profile create nbserver

 

NOTE: you can probably use the other versions of ipython (which 1.1), ipython2.6, and ipython2.7. Just replace the word “ipython” in the previous version with either “ipython2.6” or “ipython2.7” depending on what you have. I have all 3, if you used the long method of install you will probably get all those too.

NOTE: the location where the notebook was made and the config file went (it should tell you when you make the notebook)

cd /root/.config/ipython/profile_nbserver/
vi /root/.config/ipython/profile_nbserver/ipython_config.py

At the top you will see “c = get_config()”, you will add your config in at the top (the rest of the config file is commented out, at least in my case – if its not commented out for you, I would just exit out of vi, copy that config file to ipython_config.py.bak and then edit ipython_config.py again with vi, delete everything in there, so that your config just has the following in it config in it)

So make sure your config file just has this (not includeing the “CONFIG FILE” line and the “====” line below it):

Config File (https+password+every interface, short url)
========================================================

# THIS CONFIG WILL ALLOW YOU TO ACCESS THE NOTEBOOK LIKE THIS: "https://ipython.infotinks.com:50000/"
# IT WILL REQUIRE ENTERING THE PASSWORD TO GET IN

# Configuration file for ipython.
c = get_config()

# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always

# Notebook config
# The below will make it so that you have to access the notebook with https: https://ipython.infotinks.com
# If you hash out or remove the next line you will access it with http (your cert wont be used, your TCP packets wont be encrypted & secured with SSL), and you would access it like this: http://ipython.infotinks.com
c.NotebookApp.certfile = u'/etc/ssl/local/mycert.pem'
# We are going to listen on every adapter using the below method
# to listen on other interfaces check out google for "c.NotebookApp.ip"
c.NotebookApp.ip = '*' 
c.NotebookApp.open_browser = False
# That password we got, put it here (yes there is a u before the quotes - thats not a typo)
c.NotebookApp.password = u'sha1:cb3e1440003f:fbb1d1c123f34b2b19bee101123ab1e12e932043'
# Select the port to access your server, You CANNOT use the same port as you already use
# So if you have apache2 or any webserver or anything using that address you cant use it
# So it wont play with your webserver, your webserver will continue to run on it own port, it wont hurt it
# However your ipython will run on this defined port
# it will use http if you didnt set a certfile, and it will use https if you did set a valid certfile
c.NotebookApp.port = 50000
# That means I will access my box either like this: https://ipython.infotinks.com:50000
# Or like this: http://ipython.infotinks.com:50000

# if you want a subfolder on the URL to access it then the below 3 lines need to be unhashed (and change the word "/ipython/" to "/WHATEVER-YOU-WANT/"
# Example, with below 3 lines enabled, you would access your notebook like this:
# https://ipython.infotinks.com:50000/ipython/
# http://ipython.infotinks.com:50000/ipython/
# NOTE: dont forget that final / slash, because "http[s]://ipython.infotinks.com:50000/ipython" wont work
# NOTE CONTINUATION: Not using the ending / will result in a 404 error page
# NOTE: Dont use below if you think it will play with your webserver 
# NOTE CONTINUATION: it wont play with the webserver, it will complain about a used port,
# NOTE CONTINUATION: it needs a new port... 
# extra config so that runs off www.abc.com:50000/ipython/ instead of www.abc.com:50000
# c.NotebookApp.base_project_url = '/ipython/'
# c.NotebookApp.base_kernel_url = '/ipython/'
# c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}

 

Config File (https+password+every interface, short url) – Same config as above without comments
===================================================================

# ACCESS: "https://ipython.infotinks.com:50000/"
c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always
c.NotebookApp.certfile = u'/etc/ssl/local/mycert.pem'
c.NotebookApp.ip = '*' 
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:cb3e1440003f:fbb1d1c123f34b2b19bee101123ab1e12e932043'
c.NotebookApp.port = 50000

 

Config File (http+password+every interface, long url)
========================================================

# ACCESS: "http://ipython.infotinks.com:50000/ipython/"
c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always
c.NotebookApp.ip = '*' 
c.NotebookApp.open_browser = False
c.NotebookApp.password = u'sha1:cb3e1440003f:fbb1d1c123f34b2b19bee101123ab1e12e932043'
c.NotebookApp.port = 50000
c.NotebookApp.base_project_url = '/ipython/'
c.NotebookApp.base_kernel_url = '/ipython/'
c.NotebookApp.webapp_settings = {'static_url_prefix':'/ipython/static/'}

Config File(No password+http+every interace, short url)
==========================================================

# ACCESS: "http://ipython.infotinks.com:50000/"
c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always
c.NotebookApp.ip = '*' 
c.NotebookApp.open_browser = False
c.NotebookApp.port = 50000

 

Config File (No password+https+every interace, short url)
==========================================================

# ACCESS: "https://ipython.infotinks.com:50000/"
c = get_config()
c.IPKernelApp.pylab = 'inline' # if you want plotting support always
c.NotebookApp.certfile = u'/etc/ssl/local/mycert.pem'
c.NotebookApp.ip = '*' 
c.NotebookApp.open_browser = False
c.NotebookApp.port = 50000

 

LAUNCHING IPYTHON
##################

To launch it, just tell ipython to pull up your profile you just configured:

ipython notebook --profile=nbserver

SIDENOTE: I was going to say to enable inline graphix and plots like this:

ipython notebook –profile=nbserver –pylab=inline
HOWEVER we dont need to do that, as our config takes care of that with this line: “c.IPKernelApp.pylab = ‘inline’

Now go to your browser and access it with the appropriate link, if you used my commented config file you will access it like this

From the same machine where you installed it, open a browser:
https://localhost:50000/

From a remote machine:
https://ipython.infotinks.com:50000/

Where “ipython.infotinks.com” is your ip address or your hostname

The use of ipython is now discussion for other articles and your own learning….

To get out of ipython
———————-

Control-C, then press y and enter, might have to do that again, Control-C and then y and enter again.

TIP: Launching Ipython in screen (Can do it with dtach as well, check out my dtach and screen articles)

Article on Screen and detach: LINK

Here is a screen example
————————-

Launching ipython like this will make it so it doesnt use up a shell per say and it will run in the background, thus if you close the shell your ipython wont crash

screen /bin/bash
ipython notebook --profile=nbserver

 

Then you can close out the shell and it will still run (or detach with CONTROL-a and then type “:detach” without the quotes and hit ENTER.)

To see if ipython is running
——————————

ps aux | grep ipython

Or:

See what tcp ports are being listened to and by what processes ( you should see ipython listening on your port with this ):
netstat -ntlp

NOTE: -n for no hostnames & no port names, just names. -t for TCP, and -l for listening, and -p to give program names. -u for UDP

NOTE: “netstat -nulp” is for udp. To see all tcp and udp listening ports “netstat -ntulp
To start ipython on boot
————————–

In your /etc/rc.local add in the ipython start command line above the “exit 0”. So first edit the file

vi /etc/rc.local

Then add this

#...snip...

# add this in:
# first make a working directory (this is where ipythons programs will save and it will be the 'current working directory')
mkdir /home/user/ipython-notebook/ 2> /dev/null
(cd /home/user/ipython-notebook/; nohup ipython notebook --profile=nbserver > /var/log/ipython-boot.log 2>&1 && logger "* IPYTHON START: SUCCESS" || logger "* IPYTHON START: FAIL" ) &

# or just this:

# (nohup ipython notebook --profile=nbserver > /var/log/ipython-boot.log 2>&1) &

# NOTE: cant just "ipython notebook --profile=nbserver" because its an interactive environment/daemon, so it needs to be backgrounded with output redirected. Or else the exit 0 will not be reached until ipython is killed or force closed. The exit 0 is needed to be reached with rc.local initialization script

#...snip...

# make sure your rc.local ends with "exit 0"
exit 0

NOTE: the above method works on debian if you have sysvinit or systemd. If you have systemd you can utilize another method as well (but then dont use the rc.local script)

http://echorand.me/2013/09/22/managing-ipython-notebook-server-via-systemd-part-i/

https://gist.github.com/amitsaha/6658720

UNINSTALLING IPYTHON
#####################

If you used method1 of install this will go along with it:

Uninstalling just ipython-notebook:
————————————

apt-get remove ipython-notebook

 

Uninstalling ipythonnotebook and its dependencies:
————————————————–

apt-get remove ipython-notebook
apt-get autoremove

 

Or:

apt-get remove --auto-remove ipython-notebook

 

If you want to get rid of your Configs and data as well:
——————————————————–

apt-get purge ipython-notebook

Or:

apt-get remove --auto-remove ipython-notebook

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *