Install PySpark in Jupyter on Mac using Homebrew

There are multiple ways to install PySpark on Mac and run using Jupyter Notebook. Below I have explained the step-by-step of PySpark and Jupyter installation on Mac OS using Homebrew.

Steps to install PySpark & Jupyter on Mac OS

  • Step 1 – Install Homebrew
  • Step 2 – Install Java
  • Step 3 – Install Scala (Optional)
  • Step 4 – Install Python
  • Step 5 – Install PySpark
  • Step 6 – Install Jupyter
  • Step 7 – Run Example in Jupyter

Related: PySpark installation on Windows

Step 1. Install PySpark on Mac using Homebrew

Homebrew is a Missing Package Manager for macOS (or Linux) that is used to install third-party packages like Java, PySpark on Mac OS. In order to use this, first, you need to install it by using the below command.


# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

This prompts for the root password. You will need to type your root password to run this command. On a personal laptop, this is the same password you enter when you log into your Mac. If you don’t have root access, contact your system admin. You should see something like this below after the successful installation of homebrew.

homebrew install

Post-installation, you may need to run the below command to set the brew to your $PATH.


# Set brew to Path
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> /Users/admin/.zprofile
eval "$(/opt/homebrew/bin/brew shellenv)"

If the above command has issues, you can find the latest command from Homebrew.

Step 2. Install Java Version

PySpark uses Java underlying hence you need to have Java on your Mac. Since Java is a third party, you can install it using the Homebrew command brew. Since Oracle Java is not open source anymore, I am using the OpenJDK version 11. Run the below command in the terminal to install it.


# Install OpenJDK 11
brew install [email protected]

Step 3. Install Scala

Since Spark is written in Scala language it is obvious you would need Scala to run Spark programs however to run PySpark this is optional.


# Install Scala (optional)
brew install scala

Step 4. Install Python

As you would know, PySpark is used to run Spark jobs in Python hence you also need Python to install on Mac OS. let’s install it by using Homebrew. If you already have Python 2.7 or the latest then ignore this step.


# install Python
brew install python

Step 5. Install PySpark on Mac

PySpark is a Spark library written in Python to run Python applications using Apache Spark capabilities. Spark was basically written in Scala and later on due to its industry adaptation its API PySpark was released for Python using Py4J. Py4J is a Java library that is integrated within PySpark and allows python to dynamically interface with JVM objects, hence to run PySpark you also need Java to be installed along with Python, and Apache Spark. So to use PySpark, let’s install PySpark on Mac.


# Install Apache Spark
brew install apache-spark

This installs the latest version of Apache Spark which ideally includes PySpark.

pyspark install mac

After successful installation of Apache Spark run pyspark from the command line to launch PySpark shell.

pyspark shell command
PySpark Shell

Note that it displays Spark and Python versions to the terminal.

Step 6. Install Jupyter

In real-time when you are working on data analysis or machine learning, you would be required to run the PySpark application in the Jupyter notebook hence let’s learn how to use Jupyter after installing.


brew install jupyter

This installs Jupyterlab on your Mac OS.

pyspark install jupyter

Now let’s start the Jupyter notebook and run the PySpark example. This opens up Jupyter in a default web browser.


jupyter notebook

Step 7. Run PySpark Example in Jupyter Notebook

Now select New -> PythonX and enter the below lines and select Run. On Jupyter, each cell is a statement, so you can run each cell independently when there are no dependencies on previous cells.

pyspark install jupyter

Conclusion

In this PySpark installation article, you have learned the step-by-step installation of PySpark and Jupyter on Mac OS. Steps include installing Java, Scala, Python, PySpark, and Jupyter by using Homebrew.

Happy Learning !!

install pyspark jupyter

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply

You are currently viewing Install PySpark in Jupyter on Mac using Homebrew