Introduction
Anaconda is a powerful open-source distribution of Python and R programming languages for data science and machine learning projects. It comes with a plethora of pre-installed libraries and tools that streamline the setup process, making it the preferred choice for many data scientists and researchers.
Installing Anaconda on Ubuntu
Follow these steps to install Anaconda on Ubuntu:
- Download the latest version of Anaconda from the official website: Anaconda Distribution. You can also dowload the older version from the Anaconda Archive.
- Open a terminal window.
- Navigate to the directory where the Anaconda installer script was downloaded.
- Run the following command to start the installation:
bash Anaconda3-2023.09-0-Linux-x86_64.sh
Follow the prompts on the screen to complete the installation. Make sure to add Anaconda to your PATH variable during the installation process.
Miniconda
Miniconda is a minimal installer for Anaconda. It includes only conda, Python, and other essential packages, allowing you to install additional packages as needed. Miniconda is a great choice if you want more control over the packages installed on your system.
You can download Miniconda from the official website and follow similar installation steps as Anaconda.
Creating a New Environment
Once Anaconda is installed, you can create a new environment for your machine learning experiment. Environments allow you to manage different sets of libraries and dependencies for different projects.
To create a new environment named "ml_env" with Python 3.8, run the following command:
conda create --name ml_env python=3.8
Activating the Environment
To activate the newly created environment, use the following command:
conda activate ml_env
Installing Packages
With the environment activated, you can install the required packages for your machine learning experiment. For example, to install TensorFlow, run:
conda install tensorflow
Similarly, you can install other packages such as scikit-learn, pandas, matplotlib, etc., using the conda install
command.
Running a Machine Learning Experiment
Now that you have Anaconda set up and your environment configured, you're ready to run your machine learning experiment. Write your Python code using your preferred IDE or text editor, and execute it within the activated environment.
For example, if you have a Python script named experiment.py
, you can run it using the following command:
python experiment.py
Deleting an Environment
If you no longer need an environment, you can delete it to free up space and keep your system clean. To delete the "ml_env" environment, run:
conda remove --name ml_env --all
Jupyter Notebook Server Management Guide
🔍 List Running Jupyter Notebook Servers
jupyter notebook list
This shows all running notebook servers along with access tokens and URLs.
Set or Change Jupyter Notebook Password
jupyter notebook password
This creates or updates your notebook login password.
Add Conda/Virtualenv Environment to Notebook Kernels
python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
- myenv: the environment name
- Python (myenv): name shown in the Jupyter interface
Start Notebook on Custom IP and Port
jupyter notebook --ip=0.0.0.0 --port=8888 --no-browser
For persistent settings, update:
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False
🛠 Generate Jupyter Notebook Config
If not already present, generate it with:
jupyter notebook --generate-config
Enable Notebook Extensions (nbextensions)
pip install jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
jupyter nbextension enable collapsible_headings/main
jupyter nbextension enable toc2/main
Enable other extensions through the Jupyter dashboard interface.
Use JupyterLab Instead of Classic Notebook
pip install jupyterlab
jupyter lab --ip=0.0.0.0 --port=8888 --no-browser
JupyterLab offers a modern UI with file browser, terminals, tabs, etc.
Enable HTTPS for Secure Access
Generate self-signed SSL certificate:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout mykey.key -out mycert.pem
Update config file:
c.NotebookApp.certfile = '/path/to/mycert.pem'
c.NotebookApp.keyfile = '/path/to/mykey.key'
c.NotebookApp.ip = '0.0.0.0'
c.NotebookApp.port = 8888
c.NotebookApp.open_browser = False
Auto-Start Jupyter Using systemd
Create file: /etc/systemd/system/jupyter.service
[Unit]
Description=Jupyter Notebook
[Service]
Type=simple
PIDFile=/run/jupyter.pid
ExecStart=/usr/local/bin/jupyter-notebook --config=/home/youruser/.jupyter/jupyter_notebook_config.py
User=youruser
Group=youruser
WorkingDirectory=/home/youruser
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Enable and start service:
sudo systemctl enable jupyter
sudo systemctl start jupyter
View Logs for Debugging
# For manual startup logs
journalctl -xe
# If running via systemd
sudo journalctl -u jupyter -f
Conclusion
Anaconda provides everything you need to kickstart your machine learning journey on Ubuntu. With its user-friendly package manager, environment management, and extensive library support, Anaconda simplifies the setup process and allows you to focus on building and experimenting with machine learning models.
Install Anaconda today and start exploring the exciting world of data science and machine learning!