Activate conda environment & open your Jupyter notebook from a bash script

Camelia D. Brumar
3 min readSep 22, 2020

Aaaah, the school season is back, and so are the new programming courses. I am taking a Machine Learning course where we are using Jupyter notebooks most of the time. Setting up a conda virtual environment is strongly recommended over using an online notebook such as Google Colab since we might be dealing with large amounts of data, however the RAM and storage offered by those services are just not enough.

Since I like cooking, I will talk in terms of food. The ingredients you’ll need for this recipe are:

  1. A bash shell.
  2. A text editor (I used vim for this tutorial).
  3. A conda environment ready to use.
  4. A pinch of patience.

Just kidding. You can skip the last ingredient. Salt and pepper will do.

Once you gathered all these, we’ll create a bash script called notebook.bash in the directory of your choice. Feel free to name it however you want, just be consistent with the upcoming commands. To create a new file in bash, we use this command:

Then you open this script in the editor of your choice. For vim, I used:

Next, to modify in vim, use the “i” letter key on your keyboard and insert this code, which I am going to explain after:

The first line just says that we’re using bash for this script. The following line is just an alternative to conda init, which is used in bash, but it cannot be used in a bash script. If you want to find more about it, this is a handy link: https://stackoverflow.com/questions/34534513/calling-conda-source-activate-from-bash-script

This makes conda ready to use and so we activate our conda environment on line 4. In my case, the environment is called “conda_env”. Once this is done, we can practically use commands using whatever package we have installed in our environment, including the jupyter notebook. I have stored all my notebooks under the “labs” directory, so I navigate there by using the cd command and then I can use the command to open the jupyter notebook, i.e. jupyter notebook.

To close the vim editor, press “esc” on your keyboard, and type the command wq. w saves the changes in your document, and q closes the editor.

Now, the last thing to do before being able to execute your bash script is to change its permissions. To do so, run in your bash shell the following command:

The “755” will give us read, write, and execute permission. Everybody else will get only read and execute permission.

Now we’re ready to use our script by just typing the following command:

This should have displayed a list of the notebooks you have in the directory or your choice (in my case, in the labs directory):

Ta-daaaah! You just wrote a bash script that will save you some time activating your conda environment and opening jupyter notebooks for you.

Cheers!

--

--

Camelia D. Brumar

Ph.D. Candidate in Computer Science at Tufts University working with Dr. Remco Chang in the Visual Analytics Lab ([v]alt).