DEV Community

Cover image for Install and create virtualenv via terminal
Stan Kukučka
Stan Kukučka

Posted on

Install and create virtualenv via terminal

What virtual environment is? Let's jump into this topic. I'll try to explain it as simplistic as possible with some comparison and plain explanatory style.

Imagine your system is some sort of space you are executing your programs. It's a space where you run some sort of other stuff you would like to work with. There are plenty of other things you can run from a developer's perspective. Each of these things can be installed on your system. But what will happen when you'll install each of this dev stuff on your system. Yes, that can be a quite big mass I would say. And in some cases, there can happen even version conflict someday.

So to avoid this there is a simple solution. That's why the virtual environment comes to play. As naming sayin' it will create some sort of virtual box for dev stuff you would like to install. It can be anything, python3, Django or your next flask project with a specific setup.

Note: If you have already installed some dev tools you can simply skip the "Install Xcode with Homebrew" section for sure cos there is a high probability you have it on your system.

Install Xcode with Homebrew

This step is just easy simple copy-paste work. Simply paste this "looong" command from brew.sh website and hit enter to execute.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Enter fullscreen mode Exit fullscreen mode

Some stuff will flick in front of you in the terminal and then you'll be asked for the next step.

Press RETURN to continue or any other key to abort
Enter fullscreen mode Exit fullscreen mode

After this, you'll be asked for a system password to enter into the terminal to continue. At this stage be patient. There will be a quite long time to download all stuff and you'll be notified about it with this simple info text.

Downloading Command Line Tools for Xcode
Enter fullscreen mode Exit fullscreen mode

Then you'll be notified with this pop-up tiny window to install some other Command-Line for Xcode.

Alt Text

Installing Command Line Tools for Xcode
Enter fullscreen mode Exit fullscreen mode

And then after all process when it's done you'll see this.

Done with Command Line Tools for Xcode
Done.
Enter fullscreen mode Exit fullscreen mode

And be asked for the password again. Then after waiting please for Homebrew's instalment of its core stuff.

The last thing you'll be notified of is this terminal output. And it's all done. I hardly recommend restarting the whole system cos I have recognized virtual environment installation issues without having passed such.

==> Next steps:
- Run `brew help` to get started
- Further documentation: 
    https://docs.brew.sh
Enter fullscreen mode Exit fullscreen mode

Install Virtual Environtment

This is simple straight forward. Just execute pip3 install virtualenv with sudo command.

sudo pip3 install virtualenv
Enter fullscreen mode Exit fullscreen mode

Create First Environment

Let's start to create the first general main directory with the name "virtualenvs" and enter it in the directory.

mkdir virtualenvs
cd virtualenvs
Enter fullscreen mode Exit fullscreen mode

In this example, I'll create a virtual environment for Flask which is simply a Python-based system for quick web app development. But it can be anything else you would like to have in a separate work environment and you can name it whatever you want to.

Because of this purpose, we going to install the Flask directory name will be the same.

virtualenv flask
Enter fullscreen mode Exit fullscreen mode

This will create flask directory as space for virtual environment. We are going to activate this virtual environment for the further installation process. In other words, we will activate this environment to be able to install inside directory other python dependencies and libraries for further steps.

In this case for activation of the environment just type this command.

source flask/bin/activate
Enter fullscreen mode Exit fullscreen mode

You'll recognize your command prompt starting with (flask) it means your virtual environment has been activated.

To deactivate virtual environment just type deactivate.

deactivate
Enter fullscreen mode Exit fullscreen mode

At this point, tut could end. Activated flask environment is at its final. And now in the next step, you can install Flask, Python, Scrapy or whatever you would like to into your separated virtual environment.

Thanks to Michał Parzuchowski for the cover image from Unsplash.

Top comments (0)