One place for hosting & domains

      Debian

      How To Install R on Debian 9


      Introduction

      R is an open-source programming language that specializes in statistical computing and graphics. Supported by the R Foundation for Statistical Computing, it is widely used for developing statistical software and performing data analysis. An increasingly popular and extensible language with an active community, R offers many user-generated packages for specific areas of study, which makes it applicable to many fields.

      In this tutorial, we will install R and show how to add packages from the official Comprehensive R Archive Network (CRAN).

      Prerequisites

      To follow along with this tutorial, you will need a Debian 9 server with:

      • at least 1GB of RAM
      • a non-root user with sudo privileges

      To learn how to achieve this setup, follow our Debian 9 initial server setup guide.

      Once these prerequisites are in place, you’re ready to begin.

      Step 1 — Installing Dependencies

      Because R is a fast-moving project, the latest stable version isn’t always available from Debian’s repositories, so we’ll need to add the external repository maintained by CRAN. In order to do this, we’ll need to install some dependencies for the Debian 9 cloud image.

      To perform network operations that manage and download certificates, we need to install dirmngr so that we can add the external repository.

      • sudo apt install dirmngr --install-recommends

      To add a PPA reference to Debian, we’ll need to use the add-apt-repository command. For installations where this command may not available, you can add this utility to your system by installing software-properties-common:

      • sudo apt install software-properties-common

      Finally, to ensure that we have HTTPS support for secure protocols, we’ll install the following tool:

      • sudo apt install apt-transport-https

      With these dependencies in place, we’re ready to install R.

      Step 2 — Installing R

      For the most recent version of R, we’ll be installing from the CRAN repositories.

      Note: CRAN maintains the repositories within their network, but not all external repositories are reliable. Be sure to install only from trusted sources.

      Let’s first add the relevant GPG key.

      • sudo apt-key adv --keyserver keys.gnupg.net --recv-key 'E19F5F87128899B192B1A2C2AD5F960A256A04AF'

      When we run the command, we’ll receive the following output:

      Output

      Executing: /tmp/apt-key-gpghome.k3UoM7WQGq/gpg.1.sh --keyserver keys.gnupg.net --recv-key E19F5F87128899B192B1A2C2AD5F960A256A04AF gpg: key AD5F960A256A04AF: public key "Johannes Ranke (Wissenschaftlicher Berater) <[email protected]>" imported gpg: Total number processed: 1 gpg: imported: 1

      Once we have the trusted key, we can add the repository. Note that if you’re not using Debian 9 (Stretch), you can look at the supported R Project Debian branches, named for each release.

      • sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/debian stretch-cran35/'

      Now, we’ll need to run update after this in order to include package manifests from the new repository.

      Among the output should be a line similar to the following:

      Among the output that displays, you should identify lines similar to the following:

      Output

      ... Get:6 https://cloud.r-project.org/bin/linux/debian stretch-cran35/ InRelease [4,371 B] Get:7 https://cloud.r-project.org/bin/linux/debian stretch-cran35/ Packages [50.1 kB] ...

      If the lines above appear in the output from the update command, we’ve successfully added the repository. We can be sure we won’t accidentally install an older version.

      At this point, we’re ready to install R with the following command.

      If prompted to confirm installation, press y to continue.

      As of the time of writing, the latest stable version of R from CRAN is 3.5.1, which is displayed when you start R.

      Since we’re planning to install an example package for every user on the system, we’ll start R as root so that the libraries will be available to all users automatically. Alternatively, if you run the R command without sudo, a personal library can be set up for your user.

      Output

      R version 3.5.1 (2018-07-02) -- "Feather Spray" Copyright (C) 2018 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) ... Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. >

      This confirms that we’ve successfully installed R and entered its interactive shell.

      Step 3 — Installing R Packages from CRAN

      Part of R’s strength is its available abundance of add-on packages. For demonstration purposes, we’ll install txtplot, a library that outputs ASCII graphs that include scatterplot, line plot, density plot, acf and bar charts:

      • install.packages('txtplot')

      Note: The following output shows where the package will be installed.

      Output

      ... Installing package into ‘/usr/local/lib/R/site-library’ (as ‘lib’ is unspecified) . . .

      This site-wide path is available because we ran R as root. This is the correct location to make the package available to all users.

      When the installation is complete, we can load txtplot:

      If there are no error messages, the library has successfully loaded. Let’s put it in action now with an example which demonstrates a basic plotting function with axis labels. The example data, supplied by R's datasets package, contains the speed of cars and the distance required to stop based on data from the 1920s:

      • txtplot(cars[,1], cars[,2], xlab = 'speed', ylab = 'distance')

      Output

      +----+-----------+------------+-----------+-----------+--+ 120 + * + | | d 100 + * + i | * * | s 80 + * * + t | * * * * | a 60 + * * * * * + n | * * * * * | c 40 + * * * * * * * + e | * * * * * * * | 20 + * * * * * + | * * * | 0 +----+-----------+------------+-----------+-----------+--+ 5 10 15 20 25 speed

      If you are interested to learn more about txtplot, use help(txtplot) from within the R interpreter.

      Any precompiled package can be installed from CRAN with install.packages(). To learn more about what’s available, you can find a listing of official packages organized by name via the Available CRAN Packages By Name list.

      To exit R, you can type q(). Unless you want to save the workspace image, you can press n.

      Conclusion

      With R successfully installed on your server, you may be interested in this guide on installing the RStudio Server to bring an IDE to the server-based deployment you just completed. You can also learn how to set up a Shiny server to convert your R code into interactive web pages.

      For more information on how to install R packages by leveraging different tools, you can read about how to install directly from GitHub, BitBucket or other locations. This will allow you to take advantage of the very latest work from the active community.



      Source link

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


      Introduction

      Jupyter Notebook offers 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 our Initial Server Setup with Debian 9 guide.

      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:

      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:

      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:

      • source myprojectenv/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.

      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:

      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 -L 8888:localhost:8888 your_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 -L 8888:localhost:8888 sammy@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:

      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 using Putty.

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

      Set Hostname for SSH Tunnel

      Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. 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 the Add button, and the ports should appear in the Forwarded ports list:

      Forwarded ports list

      Finally, click the Open button 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, select New > Python 3 from the top right pull-down menu:

      Create a new Python 3 notebook

      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 clicking Cell > Cell Type > Markdown from the top navigation bar. We can now write notes using Markdown and even include equations written in LaTeX by 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:

      results of markdown

      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 = 2
      y = x**2
      print(y)
      

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

      simple equation results

      You now have the ability to import modules and 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, select Help > User Interface Tour from the top navigation menu to learn more.

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



      Source link

      How To Install Git on Debian 9


      Introduction

      Software version control systems enable you to keep track of your software at the source level. With versioning tools, you can track changes, revert to previous stages, and branch to create alternate versions of files and directories.

      Git is one of the most popular version control systems currently available. Many projects’ files are maintained in a Git repository, and sites like GitHub, GitLab, and Bitbucket help to facilitate software development project sharing and collaboration.

      In this tutorial, we’ll install and configure Git on a Debian 9 server. We will cover how to install the software in two different ways, each of which have their own benefits depending on your specific needs.

      Prerequisites

      In order to complete this tutorial, you should have a non-root user with sudo privileges on an Debian 9 server. To learn how to achieve this setup, follow our Debian 9 initial server setup guide.

      With your server and user set up, you are ready to begin.

      Installing Git with Default Packages

      Debian’s default repositories provide you with a fast method to install Git. Note that the version you install via these repositories may be older than the newest version currently available. If you need the latest release, consider moving to the next section of this tutorial to learn how to install and compile Git from source.

      First, use the apt package management tools to update your local package index. With the update complete, you can download and install Git:

      • sudo apt update
      • sudo apt install git

      You can confirm that you have installed Git correctly by running the following command:

      Output

      git version 2.11.0

      With Git successfully installed, you can now move on to the Setting Up Git section of this tutorial to complete your setup.

      Installing Git from Source

      A more flexible method of installing Git is to compile the software from source. This takes longer and will not be maintained through your package manager, but it will allow you to download the latest release and will give you some control over the options you include if you wish to customize.

      Before you begin, you need to install the software that Git depends on. This is all available in the default repositories, so we can update our local package index and then install the packages.

      • sudo apt update
      • sudo apt install make libssl-dev libghc-zlib-dev libcurl4-gnutls-dev libexpat1-dev gettext unzip

      After you have installed the necessary dependencies, you can go ahead and get the version of Git you want by visiting the Git project’s mirror on GitHub, available via the following URL:

      https://github.com/git/git
      

      From here, be sure that you are on the master branch. Click on the Tags link and select your desired Git version. Unless you have a reason for downloading a release candidate (marked as rc) version, try to avoid these as they may be unstable.

      git change branch select tags

      Next, on the right side of the page, click on the Clone or download button, then right-click on Download ZIP and copy the link address that ends in .zip.

      right-click on download zip to copy url

      Back on your Debian 9 server, move into the tmp directory to download temporary files.

      From there, you can use the wget command to install the copied zip file link. We’ll specify a new name for the file: git.zip.

      • wget https://github.com/git/git/archive/v2.18.0.zip -O git.zip

      Unzip the file that you downloaded and move into the resulting directory by typing:

      Now, you can make the package and install it by typing these two commands:

      • make prefix=/usr/local all
      • sudo make prefix=/usr/local install

      To ensure that the install was successful, you can type git --version and you should receive relevant output that specifies the current installed version of Git.

      Now that you have Git installed, if you want to upgrade to a later version, you can clone the repository, and then build and install. To find the URL to use for the clone operation, navigate to the branch or tag that you want on the project's GitHub page and then copy the clone URL on the right side:

      git copy URL

      At the time of writing, the relevant URL is:

      https://github.com/git/git.git
      

      Change to your home directory, and use git clone on the URL you just copied:

      • cd ~
      • git clone https://github.com/git/git.git

      This will create a new directory within your current directory where you can rebuild the package and reinstall the newer version, just like you did above. This will overwrite your older version with the new version:

      • cd git
      • make prefix=/usr/local all
      • sudo make prefix=/usr/local install

      With this complete, you can be sure that your version of Git is up to date.

      Setting Up Git

      Now that you have Git installed, you should configure it so that the generated commit messages will contain your correct information.

      This can be achieved by using the git config command. Specifically, we need to provide our name and email address because Git embeds this information into each commit we do. We can go ahead and add this information by typing:

      • git config --global user.name "Sammy"
      • git config --global user.email "[email protected]"

      We can see all of the configuration items that have been set by typing:

      Output

      user.name=Sammy user.email=[email protected] ...

      The information you enter is stored in your Git configuration file, which you can optionally edit by hand with a text editor like this:

      ~/.gitconfig contents

      [user]
        name = Sammy
        email = [email protected]
      

      There are many other options that you can set, but these are the two essential ones needed. If you skip this step, you’ll likely see warnings when you commit to Git. This makes more work for you because you will then have to revise the commits you have done with the corrected information.

      Conclusion

      You should now have Git installed and ready to use on your system.

      To learn more about how to use Git, check out these articles and series:



      Source link