How To Install Python 3 and Set Up a Programming Environment on Ubuntu 18.04

How To Install Python 3 and Set Up a Programming Environment on Ubuntu 18.04

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 18.04 server.

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 18.04 server.

Introduction

Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 18.04 server.

Step 1 — Update and Upgrade

Logged into your Ubuntu 18.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

sudo apt update
sudo apt -y upgrade


Confirm installation if prompted to do so.

Step 2 — Check Version of Python

Check which version of Python 3 is installed by typing:

python3 -V


You’ll receive output similar to the following, depending on when you have updated your system.

OutputPython 3.6.5


Step 3 — Install pip

To manage software packages for Python, install pip, a tool that will install and manage libraries or modules to use in your projects.

sudo apt install -y python3-pip


Python packages can be installed by typing:

pip3 install package_name


Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip3 install numpy.

Step 4 — Install Additional Tools

There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

sudo apt install build-essential libssl-dev libffi-dev python3-dev


Step 5 — Install venv

Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

sudo apt install -y python3-venv


Step 6 — Create a Virtual Environment

You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you can call yours whatever you want.

python3.6 -m venv my_env


Step 7 — Activate Virtual Environment

Activate the environment using the command below, where my_env is the name of your programming environment.

source my_env/bin/activate


Your command prompt will now be prefixed with the name of your environment:

Step 8 — Test Virtual Environment

Open the Python interpreter:

python


Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

You’ll know you’re in the interpreter when you receive the following output:

Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 


Now, use the print() function to create the traditional Hello, World program:

print("Hello, World!")

OutputHello, World!


Step 9 — Deactivate Virtual Environment

Quit the Python interpreter:

quit()


Then exit the virtual environment:

deactivate


Further Reading

An A-Z of useful Python tricks

A Complete Machine Learning Project Walk-Through in Python

Machine Learning: how to go from Zero to Hero

Learning Python: From Zero to Hero

Automated Machine Learning on the Cloud in Python

Introduction to PyTorch and Machine Learning

Python Tutorial for Beginners (2019) - Learn Python for Machine Learning and Web Development

How to get started with Python for Deep Learning and Data Science

How To Use Vuls as a Vulnerability Scanner on Ubuntu 18.04

Complete Python Bootcamp: Go from zero to hero in Python 3

Machine Learning A-Z™: Hands-On Python & R In Data Science

Complete Python Masterclass

Python and Django Full Stack Web Developer Bootcamp

How to Install Python 3.8 on Ubuntu 18.04?

How to Install Python 3.8 on Ubuntu 18.04?

In this tutorial we'll cover two different ways to install Python 3.8 on Ubuntu 18.04. The first option is to install the deb package from the deadsnakes PPA, and the second one is by building from the source code.

Python is one of the most widely used programming languages in the world. With its simple and easy to learn syntax, Python is a popular choice for beginners and experienced developers. Python is quite a versatile programming language. It can be used to build all kinds of applications, from simple scrips to complex machine learning algorithms.

Python 3.8 is the latest major release of the Python language. It includes many new features such as assignment expressions, positional-only parameters, f-strings support, and more.

Python 3.8 is not available in Ubuntu’s default repositories. In this tutorial, we’ll cover two different ways to install Python 3.8 on Ubuntu 18.04. The first option is to install the deb package from the deadsnakes PPA, and the second one is by building from the source code.

The same steps apply for Ubuntu 16.04 and any Ubuntu-based distribution, including Kubuntu, Linux Mint, and Elementary OS.

Installing Python 3.8 on Ubuntu with Apt

Installing Python 3.8 on Ubuntu with apt is a relatively straightforward process and takes only a few minutes:

  1. Run the following commands as root or user with sudo access to update the packages list and install the prerequisites:

    sudo apt update
    sudo apt install software-properties-common
    
  2. Add the deadsnakes PPA to your system’s sources list:

    sudo add-apt-repository ppa:deadsnakes/ppa
    

    When prompted press Enter to continue:

    Press [ENTER] to continue or Ctrl-c to cancel adding it.
    
  3. Once the repository is enabled, install Python 3.8 with:

    sudo apt install python3.8
    
  4. Verify that the installation was successful by typing:

    python3.8 --version
    
    Python 3.8.0
    

At this point, Python 3.8 is installed on your Ubuntu system, and you can start using it.

Installing Python 3.8 on Ubuntu from Source

In this section, we’ll explain how to compile Python 3.8 from the source.

  1. Update the packages list and install the packages necessary to build Python:

    sudo apt update
    sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
    
  2. Download the latest release’s source code from the Python download page using wget:

    wget https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tgz
    

At the time of writing this article, the latest release is 3.8.0.

  1. When the download finishes, extract the gzipped archive:

    tar -xf Python-3.8.0.tgz
    
  2. Switch to the Python source directory and execute the configure script which performs a number of checks to make sure all of the dependencies on your system are present:

    cd Python-3.8.0
    ./configure --enable-optimizations
    

The --enable-optimizations option optimizes the Python binary by running multiple tests. This makes the build process slower.

  1. Start the Python 3.8 build process:

    make -j 8
    

For faster build time, modify the -j to correspond to the number of cores in your processor. You can find the number by typing nproc.

  1. When the build process is complete, install the Python binaries by typing:

    sudo make altinstall
    

Do not use the standard make install as it will overwrite the default system python3 binary.

  1. That’s it. Python 3.8 has been installed and ready to be used. Verify it by typing:

    python3.8 --version
    

    The output should show the Python version:

    Python 3.8.0
    
Conclusion

You have installed Python 3.8 on your Ubuntu 18.04 machine, and you can start developing your Python 3 project.

If you have any questions or feedback, feel free to comment below.

How To Set Up Jupyter Notebook with Python 3 on Ubuntu 18.04

How To Set Up Jupyter Notebook with Python 3 on Ubuntu 18.04

This tutorial will walk you through setting up Jupyter Notebook to run from an Ubuntu 18.04 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.


Introduction

An open-source web application, Jupyter Notebook lets you create and share interactive code, visualizations, and more. This tool can be used with several programming 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 an Ubuntu 18.04 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 Ubuntu 18.04 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 tutorial.


Step 1 — Set Up Python

To begin the process, we’ll install the dependencies we need for our Python programming environment from the Ubuntu repositories. Ubuntu 18.04 comes preinstalled with Python 3.6. 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

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 to manage our projects. We will install Jupyter into this virtual environment.

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

Upgrade pip and install the package by typing:

sudo -H pip3 install --upgrade pip
sudo -H pip3 install virtualenv

The -H flag ensures that the security policy sets the home environment variable to the home directory of the target user.

With virtualenv installed, we can start forming our environment. Create and move into a directory where we can keep our project files. We’ll call this my_project_dir, but you should use a name that is meaningful for you and what you’re working on.

mkdir ~/my_project_dir
cd ~/my_project_dir

Within the project directory, we’ll create a Python virtual environment. For the purpose of this tutorial, we’ll call it my_project_env but you should call it something that is relevant to your project.

virtualenv my_project_env

This will create a directory called my_project_env within your my_project_dir 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 my_project_env/bin/activate

Your prompt should change to indicate that you are now operating within a Python virtual environment. It will look something like this: (my_project_env)[email protected]:~/my_project_dir$

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 (my_project_env) 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/my_project_dir
[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 computer (not on a server), you can navigate to the displayed URL to connect to Jupyter Notebook. If you are running Jupyter Notebook on a server, 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 you can exit the app and re-run it once you set up SSH tunneling. Let’s choose to stop the Jupyter Notebook process. We will run it again once we have SSH tunneling set up. To stop the Jupyter Notebook process, press CTRL+C, type Y, and then ENTER to confirm. The following output 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 [email protected]_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 [email protected]

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

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

Next, click SSH on the bottom of the left pane to expand the menu, and then click Tunnels. Enter the local port number you want 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:8000` where 8000 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:

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 a very powerful tool with 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:

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:

# First 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 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:

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.


Learn More

Python Core and Advanced

Creating Web Sites using Python and Flask

Complete Python: Go from zero to hero in Python

Build a Simple CRUD App with Python, Flask, and React

An A-Z of useful Python tricks

A Feature Selection Tool for Machine Learning in Python

Learning Python: From Zero to Hero

Complete Python Bootcamp: Go from zero to hero in Python 3

Complete Python Masterclass

Python and Django Full Stack Web Developer Bootcamp

The Python Bible™ | Everything You Need to Program in Python

Originally published by Lisa Tagliaferri at https://www.digitalocean.com